Skip to content

Commit

Permalink
by default don't change the format of images
Browse files Browse the repository at this point in the history
  • Loading branch information
hauxir committed Jul 5, 2019
1 parent 62127d3 commit 9a19967
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN pip install flask
RUN pip install flask-cors
RUN pip install Flask-Limiter
RUN pip install Wand
RUN pip install filetype
RUN pip install gunicorn

RUN mkdir /images
Expand Down
18 changes: 9 additions & 9 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import glob
import os
import random
import string

import filetype
import settings
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
Expand Down Expand Up @@ -39,7 +41,8 @@ def _get_random_filename():
string.ascii_lowercase + string.digits + string.ascii_uppercase, k=5
)
)
if os.path.isfile(f"{settings.IMAGES_DIR}/{random_string}.{settings.OUTPUT_TYPE}"):
file_exists = len(glob.glob(f"{settings.IMAGES_DIR}/{random_string}.*")) > 0
if file_exists:
return _get_random_filename()
return random_string

Expand Down Expand Up @@ -111,16 +114,15 @@ def upload_image():
random_string = _get_random_filename()
tmp_filepath = os.path.join("/tmp/", random_string)
file.save(tmp_filepath)
output_type = settings.OUTPUT_TYPE or filetype.guess_extension(tmp_filepath)
error = None

try:
with Image(filename=tmp_filepath) as img:
with img.convert(settings.OUTPUT_TYPE) as converted:
output_filename = (
os.path.basename(tmp_filepath) + f".{settings.OUTPUT_TYPE}"
)
with img.convert(output_type) as converted:
output_filename = os.path.basename(tmp_filepath) + f".{output_type}"
output_path = os.path.join(settings.IMAGES_DIR, output_filename)
if settings.OUTPUT_TYPE != "gif":
if output_type not in ["gif"]:
converted.sequence = [converted.sequence[0]]
converted.save(filename=output_path)
except MissingDelegateError:
Expand Down Expand Up @@ -155,9 +157,7 @@ def get_image(filename):

filename_without_extension, extension = os.path.splitext(filename)
dimensions = f"{width}x{height}"
resized_filename = (
filename_without_extension + f"_{dimensions}.{settings.OUTPUT_TYPE}"
)
resized_filename = filename_without_extension + f"_{dimensions}.{extension}"

resized_path = os.path.join(settings.CACHE_DIR, resized_filename)

Expand Down
2 changes: 1 addition & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

IMAGES_DIR = "/images/"
CACHE_DIR = "/cache/"
OUTPUT_TYPE = "png"
OUTPUT_TYPE = None
MAX_UPLOADS_PER_DAY = 1000
MAX_UPLOADS_PER_HOUR = 100
MAX_UPLOADS_PER_MINUTE = 20
Expand Down

0 comments on commit 9a19967

Please sign in to comment.