Skip to content

Commit

Permalink
Refactor & simplifying Name Strategy + update README accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelaby committed Feb 19, 2020
1 parent 7bca06c commit 39bbb99
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ imgpush requires docker
docker run -v <PATH TO STORE IMAGES>:/images -p 5000:5000 hauxir/imgpush:latest
```

### Parameters

imgpush generate the name of the file. You have the choice between 2 name generation strategies :

* randomstr : 5 random chars with numbers (**default option**)
* uuidv4 : uuidv4

To specify a strategy, you can add arguments to docker `-e strategy="uuidv4"` or `-e strategy="randomstr"`

### Kubernetes

> This is fully optional and is only needed if you want to run imgpush in Kubernetes.
Expand Down Expand Up @@ -84,6 +75,7 @@ Handling connection for 5000

7. To expose imgpush to the internet you need to configure an [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/). The exact configuration depends on you cluster but you can find an example in the `kubernetes/deployment-example.yaml` file that you can adapt to your setup.


## Configuration
| Setting | Default value | Description |
| ------------- | ------------- |------------- |
Expand All @@ -94,6 +86,8 @@ Handling connection for 5000
| MAX_UPLOADS_PER_MINUTE | "20" | Integer, max per IP address |
| ALLOWED_ORIGINS | "['*']" | array of domains, e.g ['https://a.com'] |
| VALID_SIZES | Any size | array of integers allowed in the h= and w= parameters, e.g "[100,200,300]". You should set this to protect against being bombarded with requests! |
| ALLOWED_ORIGINS | "['*']" | array of domains, e.g ['https://a.com'] |
| NAME_STRATEGY | "randomstr" | `randomstr` for random 5 chars, `uuidv4` for UUIDv4 |

Setting configuration variables is all set through env variables that get passed to the docker container.
### Example:
Expand All @@ -103,4 +97,4 @@ docker run -e ALLOWED_ORIGINS="['https://a.com', 'https://b.com']" -s -v <PATH T
or to quickly deploy it locally, run
```
docker-compose up -d
```
```
15 changes: 2 additions & 13 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,11 @@ def _get_random_filename():
return random_string

def _generate_random_filename():
strategy = _get_random_filename_strategy()
if strategy == settings.STRATEGY_UUIDV4:
if settings.NAME_STRATEGY == settings.NAME_STRATEGY_UUIDV4:
return str(uuid.uuid4())
if strategy == settings.STRATEGY_RANDOMSTR:
if settings.NAME_STRATEGY == settings.NAME_STRATEGY_RANDOMSTR:
return "".join(random.choices(string.ascii_lowercase + string.digits + string.ascii_uppercase, k=5))

def _get_random_filename_strategy():
try:
strategy = os.environ['strategy']
if strategy == settings.STRATEGY_UUIDV4 or strategy == settings.STRATEGY_RANDOMSTR:
return strategy
else:
return settings.DEFAULT_STRATEGY
except KeyError:
return settings.DEFAULT_STRATEGY

def _resize_image(path, width, height):
filename_without_extension, extension = os.path.splitext(path)

Expand Down
6 changes: 3 additions & 3 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
MAX_UPLOADS_PER_HOUR = 100
MAX_UPLOADS_PER_MINUTE = 20
ALLOWED_ORIGINS = ["*"]
STRATEGY_RANDOMSTR = 'randomstr'
STRATEGY_UUIDV4 = 'uuidv4'
DEFAULT_STRATEGY = STRATEGY_RANDOMSTR
NAME_STRATEGY_RANDOMSTR = 'randomstr'
NAME_STRATEGY_UUIDV4 = 'uuidv4'
NAME_STRATEGY = NAME_STRATEGY_RANDOMSTR

VALID_SIZES = []

Expand Down

0 comments on commit 39bbb99

Please sign in to comment.