Commit dc91ac4a authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

fix potential high disk usage with temporary files

parent 9d6b148f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/3.1/ref/settings/

from pathlib import Path
import os
import tempfile

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -24,6 +25,9 @@ PARTIAL_UPLOADS_DIR = os.getenv("JAMA_PARTIAL_UPLOADS_DIR") or "{}/{}".format(
MEDIA_FILES_DIR = os.getenv("JAMA_FILES_DIR") or "{}/{}".format(
    BASE_DIR, "media_source_files"
)

TMP_THUMBNAILS_DIR = os.getenv("JAMA_TMP_THUMBNAILS_DIR") or tempfile.gettempdir()

JAMA_SITE = os.getenv("JAMA_SITE") or "http://localhost:8000/"

JAMA_IIIF_UPSCALING_PREFIX = os.getenv("JAMA_IIIF_UPSCALING_PREFIX") or ""
+1 −2
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ from django.http import (
)
from django.shortcuts import render
import pyvips
import tempfile
from resources.models import File
import os.path
from typing import Union
@@ -33,7 +32,7 @@ def thumb(
    except ValueError:
        return HttpResponseBadRequest("Bad Request")
    tmp_pic_path = "{}/thumb-{}-{}-{}-{}-{}-{}-{}.jpg".format(
        tempfile.gettempdir(), sha256, angle, scale, top, right, bottom, left
        settings.TMP_THUMBNAILS_DIR, sha256, angle, scale, top, right, bottom, left
    )
    if not os.path.isfile(tmp_pic_path):
        crop_color = [96]
+2 −1
Original line number Diff line number Diff line
@@ -2792,7 +2792,7 @@ def picture_rotate_crop(
    if rotation + top_crop + right_crop + bottom_crop + left_crop == 0:
        raise ServiceException(NOTHING_TO_DO)
    tmp_pic_path = "{}/resize-{}-{}-{}-{}-{}-{}{}".format(
        tempfile.gettempdir(),
        settings.TMP_THUMBNAILS_DIR,
        file_instance.pk,
        rotation,
        top_crop,
@@ -2816,6 +2816,7 @@ def picture_rotate_crop(
        if not replace_file(user, new_file_id, resource_id):
            raise ServiceException(UNKNOWN_ERROR)
        reloaded_file = File.objects.get(id=resource_id)
        os.remove(tmp_pic_path)
        return serializers.file(reloaded_file, include_metas=False)
    except pyvips.error.Error:
        raise ServiceException(CANT_CREATE_IMAGE)