From 94f788ffe901d42656d35f3f5e0aac4034544424 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 28 Nov 2022 05:49:17 +0200 Subject: [PATCH] use exiv2 to remove metadata instead of imagemagick, much much much faster --- Dockerfile | 1 + files/helpers/media.py | 21 ++++++++++++--------- ubuntu_setup.sh | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 98f913f95..a63c1e725 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ RUN apt install -y imagemagick RUN apt install -y postgresql RUN apt install -y libpq-dev RUN apt install -y nano +RUN apt install -y exiv2 COPY requirements.txt /etc/requirements.txt diff --git a/files/helpers/media.py b/files/helpers/media.py index 8c4eb2e07..33606417e 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -142,7 +142,7 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in try: with Image.open(filename) as i: - params = ["magick", filename, "-strip", "-auto-orient"] + params = ["magick", filename] if i.format.lower() != 'webp': params.extend(["-coalesce", "-quality", "88", "-define", "webp:method=5"]) if trim and len(list(Iterator(i))) == 1: @@ -158,14 +158,15 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in abort(415) return None - params.append(filename) - try: - subprocess.run(params, timeout=MAX_IMAGE_CONVERSION_TIMEOUT) - except subprocess.TimeoutExpired: - if has_request: - abort(413, ("An uploaded image took too long to convert to WEBP. " - "Consider uploading elsewhere.")) - return None + if len(params) > 2: + params.append(filename) + try: + subprocess.run(params, timeout=MAX_IMAGE_CONVERSION_TIMEOUT) + except subprocess.TimeoutExpired: + if has_request: + abort(413, ("An uploaded image took too long to convert to WEBP. " + "Consider uploading elsewhere.")) + return None if resize: if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_KB * 1024: @@ -216,4 +217,6 @@ def process_image(filename:str, v, resize=0, trim=False, uploader_id:Optional[in ) db.add(media) + subprocess.run(["exiv2", "rm", filename], timeout=MAX_IMAGE_CONVERSION_TIMEOUT) + return filename diff --git a/ubuntu_setup.sh b/ubuntu_setup.sh index 7eda76404..25ddaea3a 100644 --- a/ubuntu_setup.sh +++ b/ubuntu_setup.sh @@ -1,6 +1,6 @@ apt -y update apt -y upgrade -apt -y install git redis-server python3-pip ffmpeg tmux nginx snapd ufw gpg-agent htop nano +apt -y install git redis-server python3-pip ffmpeg tmux nginx snapd ufw gpg-agent htop nano exiv2 git config --global credential.helper store cd /rDrama