6
0
Ver Fonte

Pre-generation of thumnails

Two default thumbnail sizes are generated automatically if a file is added to a project.
This happens asynchronously for external data and synchronously for manually added images.
blunk há 3 anos atrás
pai
commit
01c77feaf5
2 ficheiros alterados com 30 adições e 12 exclusões
  1. 7 0
      pycs/database/Project.py
  2. 23 12
      tests/client/file_tests.py

+ 7 - 0
pycs/database/Project.py

@@ -3,6 +3,7 @@ import shutil
 import typing as T
 
 from datetime import datetime
+from eventlet import tpool
 
 from pycs import app
 from pycs import db
@@ -12,6 +13,7 @@ from pycs.database.Collection import Collection
 from pycs.database.File import File
 from pycs.database.Label import Label
 from pycs.database.util import commit_on_return
+from pycs.util.FileOperations import resize_file
 
 
 class Project(NamedBaseModel):
@@ -280,6 +282,11 @@ class Project(NamedBaseModel):
         file.frames = frames
         file.fps = fps
 
+        # Pre-load common thumbnail sizes if the given file is an image.
+        if file.type == 'image' and os.path.isfile(path):
+            for max_width, max_height in [(200, 200), (2000, 800)]:
+                tpool.execute(resize_file, file, self.root_folder, max_width, max_height)
+
         return file, is_new
 
     def get_files(self, *filters, offset: int = 0, limit: int = -1) -> T.List[File]:

+ 23 - 12
tests/client/file_tests.py

@@ -25,12 +25,27 @@ class _BaseFileTests(_BaseLabelTests):
         for folder in [data_root, root / "temp"]:
             folder.mkdir(exist_ok=True, parents=True)
 
+    def _get_dummy_image_bytes(self):
+        byteImgIO = io.BytesIO()
+        byteImg = Image.fromarray(np.zeros([1,1,3]).astype(np.uint8))
+        byteImg.save(byteImgIO, "JPEG")
+        byteImgIO.seek(0)
+        file_content = byteImgIO.read()
+
+        return file_content
+
+    def _create_dummy_image(self, file_name):
+        absolute_path = os.path.join(self.project.data_folder, file_name)
+        file_content = self._get_dummy_image_bytes()
+        with open(absolute_path, "wb") as f:
+            f.write(file_content)
+
+        return file_content
 
 class FileCreationTests(_BaseFileTests):
 
     @pаtch_tpool_execute
     def test_file_upload_project_with_external_data(self, mocked_execute=None):
-
         file_content = b"some content+1"
         url = url_for("upload_file", project_id=self.project.id)
 
@@ -53,7 +68,8 @@ class FileCreationTests(_BaseFileTests):
         url = url_for("upload_file", project_id=4242)
         self.post(url, data=dict(), status_code=404)
 
-        file_content = b"some content+1"
+        # Creating a dummy image with proper dummy content.
+        file_content = self._get_dummy_image_bytes()
         url = url_for("upload_file", project_id=self.project.id)
 
         self.assertEqual(0, File.query.count())
@@ -78,6 +94,8 @@ class FileDeletionTests(_BaseFileTests):
 
     def test_file_removal(self):
 
+        self._create_dummy_image("image.jpg")
+
         file_uuid = str(uuid.uuid1())
         file, is_new = self.project.add_file(
             uuid=file_uuid,
@@ -92,9 +110,6 @@ class FileDeletionTests(_BaseFileTests):
 
         self.assertEqual(1, self.project.files.count())
 
-        with open(file.absolute_path, "w"):
-            pass
-
         self.assertTrue(os.path.exists(file.absolute_path))
 
         url = url_for("remove_file", file_id=file.id)
@@ -109,6 +124,8 @@ class FileDeletionTests(_BaseFileTests):
 
     def test_file_removal_from_project_with_external_data(self):
 
+        self._create_dummy_image("image.jpg")
+
         file_uuid = str(uuid.uuid1())
         file, is_new = self.project.add_file(
             uuid=file_uuid,
@@ -121,9 +138,6 @@ class FileDeletionTests(_BaseFileTests):
 
         self.assertTrue(is_new)
 
-        with open(file.absolute_path, "w"):
-            pass
-
         self.project.external_data = True
         self.assertTrue(os.path.exists(file.absolute_path))
         url = url_for("remove_file", file_id=file.id)
@@ -135,7 +149,6 @@ class FileDeletionTests(_BaseFileTests):
 
 class FileGettingTests(_BaseFileTests):
 
-
     def test_get_file_getting(self):
 
         file_uuid = str(uuid.uuid1())
@@ -156,9 +169,7 @@ class FileGettingTests(_BaseFileTests):
         # without an actual file, this GET request returns 404
         self.get(url, status_code=404)
 
-        content = b"some text"
-        with open(file.absolute_path, "wb") as f:
-            f.write(content)
+        content = self._create_dummy_image("image.jpg")
 
         response = self.get(url)