implement temporary file cleanup

This commit is contained in:
Jakob Ketterl 2021-02-10 22:24:43 +01:00
parent 7097dc1cd8
commit 1e72485425
2 changed files with 6 additions and 3 deletions

View File

@ -30,7 +30,6 @@ class ImageUploadController(AuthorizationMixin, AssetsController):
# TODO: limit file size # TODO: limit file size
# TODO: check image mime type, if possible # TODO: check image mime type, if possible
contents = self.get_body() contents = self.get_body()
# TODO: clean up files after timeout or on shutdown
with open(self.getFilePath(), 'wb') as f: with open(self.getFilePath(), 'wb') as f:
f.write(contents) f.write(contents)
self.send_response(json.dumps({"uuid": self.uuid}), content_type="application/json") self.send_response(json.dumps({"uuid": self.uuid}), content_type="application/json")

View File

@ -27,6 +27,7 @@ import json
import logging import logging
import shutil import shutil
import os import os
from glob import glob
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -377,9 +378,12 @@ class GeneralSettingsController(AuthorizationMixin, WebpageController):
if data[image_id] == "restore": if data[image_id] == "restore":
os.unlink(data_file) os.unlink(data_file)
elif data[image_id]: elif data[image_id]:
filename = "{}-{}".format(image_id, data[image_id]) temporary_file = "{}/{}-{}".format(config.get_temporary_directory(), image_id, data[image_id])
shutil.copy(config.get_temporary_directory() + "/" + filename, data_file) shutil.copy(temporary_file, data_file)
del data[image_id] del data[image_id]
# remove any accumulated temporary files on save
for file in glob("{}/{}*".format(config.get_temporary_directory(), image_id)):
os.unlink(file)
def processFormData(self): def processFormData(self):
data = parse_qs(self.get_body().decode("utf-8"), keep_blank_values=True) data = parse_qs(self.get_body().decode("utf-8"), keep_blank_values=True)