From 5ea28b1e7733935d4f6b7641fd8bbf88d1d66987 Mon Sep 17 00:00:00 2001 From: magpiewings Date: Sat, 17 Mar 2018 11:21:42 -0700 Subject: [PATCH] wip converts tiff and psd submissions to jpeg --- OpenOversight/app/config.py | 1 + OpenOversight/app/main/views.py | 8 ++++++-- OpenOversight/app/utils.py | 5 +++-- requirements.txt | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/OpenOversight/app/config.py b/OpenOversight/app/config.py index 8ea38eaa5..03a93505d 100644 --- a/OpenOversight/app/config.py +++ b/OpenOversight/app/config.py @@ -37,6 +37,7 @@ class BaseConfig(object): # Upload Settings MAX_CONTENT_LENGTH = 50 * 1024 * 1024 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) + REQUIRES_CONV = set(['tiff', 'psd']) SEED = 666 diff --git a/OpenOversight/app/main/views.py b/OpenOversight/app/main/views.py index e520d69c9..be9343c47 100644 --- a/OpenOversight/app/main/views.py +++ b/OpenOversight/app/main/views.py @@ -448,11 +448,15 @@ def submit_department_images(department_id=1): @limiter.limit('250/minute') def upload(department_id): file_to_upload = request.files['file'] - if not allowed_file(file_to_upload.filename): + if not allowed_file(file_to_upload.filename, 'ALLOWED_EXTENSIONS'): return jsonify(error="File type not allowed!"), 415 original_filename = secure_filename(file_to_upload.filename) image_data = file_to_upload.read() - + if allowed_file (file_to_upload.filename, 'REQUIRES_CONV'): + changeimage = Image.open(file_to_upload.filename) + basename = os.path.splitext(file_to_upload.filename)[0].split('.') + changeimage.save(basename + '.jpeg') + os.remove(file_to_upload.filename) # See if there is a matching photo already in the db hash_img = compute_hash(image_data) hash_found = Image.query.filter_by(hash_img=hash_img).first() diff --git a/OpenOversight/app/utils.py b/OpenOversight/app/utils.py index a185d1748..b219cef59 100644 --- a/OpenOversight/app/utils.py +++ b/OpenOversight/app/utils.py @@ -2,6 +2,7 @@ import datetime import hashlib import random +from PIL import Image from sqlalchemy import func from sqlalchemy.sql.expression import cast import imghdr as imghdr @@ -90,9 +91,9 @@ def edit_officer_profile(officer, form): return officer -def allowed_file(filename): +def allowed_file(filename, extensions): return '.' in filename and \ - filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS'] + filename.rsplit('.', 1)[1].lower() in current_app.config[extensions] def get_random_image(image_query): diff --git a/requirements.txt b/requirements.txt index d72afc495..3b6bc045e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,5 +13,6 @@ sqlalchemy==1.1.15 flask-sqlalchemy>=2.1 flask-bootstrap gunicorn==17.5 +Pillow fabric itsdangerous