diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-02-09 11:55:10 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-02-09 11:55:10 +0100 |
commit | 8c4f1318e070eae0dafc35db03b0de7571063c24 (patch) | |
tree | 5152b9be1f412eabacc6de2b5c576f43176677d1 /ishtar_common | |
parent | 60a5d1e368d3233e1f41ff4c9f639dd4c6d95d5a (diff) | |
download | Ishtar-8c4f1318e070eae0dafc35db03b0de7571063c24.tar.bz2 Ishtar-8c4f1318e070eae0dafc35db03b0de7571063c24.zip |
File storage: fix file save on broken link
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/tests.py | 48 | ||||
-rw-r--r-- | ishtar_common/utils.py | 10 |
2 files changed, 58 insertions, 0 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index e535eda4b..90af3ac93 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -2913,6 +2913,54 @@ class PersonQATest(TestCase): ) +class StorageTest(TestCase): + def setUp(self) -> None: + self.st1 = models.SourceType.objects.create(label="Report", code="REP") + + def test_filesystemstorage(self) -> None: + # bug when link to non-existing files + image_path = os.path.join( + settings.ROOT_PATH, "..", "ishtar_common", "tests", "test.png" + ) + doc = models.Document.objects.create( + source_type=self.st1, + title="Operation report", + image=SimpleUploadedFile( + name="test.png", + content=open(image_path, "rb").read(), + content_type="image/png", + ), + ) + p = doc.image.path.split(os.sep) + # current save path + base_path = os.sep.join(p[:-1]) + # clean all files in order to have no random string on save + for f in os.listdir(base_path): + if f.startswith("test"): + os.remove(os.path.join(base_path, f)) + doc = models.Document.objects.get(pk=doc.pk) + doc.image.save( + "test.png", + SimpleUploadedFile( + name="test.png", + content=open(image_path, "rb").read(), + content_type="image/png", + ), + ) + doc = models.Document.objects.get(pk=doc.pk) + os.remove(doc.image.path) + os.symlink("/tmp/ZZZZZZZZZZZZZZZ", doc.image.path) # bad link + doc.image.save( + "test.png", + SimpleUploadedFile( + name="test.png", + content=open(image_path, "rb").read(), + content_type="image/png", + ), + ) + doc.save() + + class DocumentTest(TestCase): def setUp(self): Operation = apps.get_model("archaeological_operations", "Operation") diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index e27b26e70..9b2f38819 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -52,6 +52,7 @@ from django.contrib.sessions.backends.db import SessionStore from django.core.cache import cache from django.core.exceptions import SuspiciousOperation, ObjectDoesNotExist from django.core.files import File +from django.core.files.storage import FileSystemStorage from django.core.validators import EMPTY_VALUES from django.core.urlresolvers import reverse from django.db import models @@ -2072,3 +2073,12 @@ def get_image_path(instance, filename): n = datetime.datetime.now() return "upload/{}/{:02d}/{:02d}/{}".format(n.year, n.month, n.day, filename) return instance._get_image_path(filename) + + +class IshtarFileSystemStorage(FileSystemStorage): + def exists(self, name): + path_name = self.path(name) + if os.path.islink(path_name): + if not os.path.exists(os.readlink(path_name)): + os.remove(path_name) + return os.path.exists(path_name) |