Explorar o código

Resolve "temp directory for resized files"

Eric Tröbs %!s(int64=4) %!d(string=hai) anos
pai
achega
d08173ed93

+ 3 - 6
pycs/frontend/WebServer.py

@@ -1,5 +1,5 @@
 from glob import glob
 from glob import glob
-from os import path, mkdir
+from os import path, getcwd
 from os.path import exists
 from os.path import exists
 from time import time
 from time import time
 
 
@@ -121,10 +121,6 @@ class WebServer:
                 }
                 }
                 job.value = project['jobs'][file_uuid]
                 job.value = project['jobs'][file_uuid]
 
 
-                # create upload path if not exists
-                if not path.exists(upload_path):
-                    mkdir(upload_path)
-
                 # define progress callback
                 # define progress callback
                 length = content_length if content_length is not None and content_length != 0 else total_content_length
                 length = content_length if content_length is not None and content_length != 0 else total_content_length
 
 
@@ -175,7 +171,8 @@ class WebServer:
                 target_object = target_object.resize(size)
                 target_object = target_object.resize(size)
 
 
             # construct directory and filename
             # construct directory and filename
-            file_directory, file_name = target_object.get_file()
+            file_directory = path.join(getcwd(), target_object.directory)
+            file_name = target_object.full_name
 
 
             # return data
             # return data
             return send_from_directory(file_directory, file_name)
             return send_from_directory(file_directory, file_name)

+ 5 - 7
pycs/projects/ImageFile.py

@@ -6,23 +6,21 @@ from pycs.projects.MediaFile import MediaFile
 
 
 
 
 class ImageFile(MediaFile):
 class ImageFile(MediaFile):
-    def __init__(self, obj, project_id):
+    def __init__(self, obj, project_id, type='data'):
         obj['type'] = 'image'
         obj['type'] = 'image'
-        super().__init__(obj, project_id)
+        super().__init__(obj, project_id, type)
 
 
     def resize(self, maximum_width):
     def resize(self, maximum_width):
         # check if resized file already exists
         # check if resized file already exists
-        resized = MediaFile(self.copy(), self.project_id)
+        resized = ImageFile(self.copy(), self.project_id, 'temp')
         resized['id'] = self['id'] + '-' + maximum_width
         resized['id'] = self['id'] + '-' + maximum_width
 
 
-        target_directory, target_name = self._get_file(resized['id'])
-        target_path = path.join(target_directory, target_name)
-
+        target_path = resized.path
         if path.exists(target_path):
         if path.exists(target_path):
             return resized
             return resized
 
 
         # load full size image
         # load full size image
-        current_directory, current_name = self.get_file()
+        current_directory, current_name = self.directory, self.full_name
         image = Image.open(path.join(current_directory, current_name))
         image = Image.open(path.join(current_directory, current_name))
         image_width, image_height = image.size
         image_width, image_height = image.size
 
 

+ 13 - 8
pycs/projects/MediaFile.py

@@ -1,25 +1,30 @@
-from os import path, getcwd
+from os.path import join
 from uuid import uuid1
 from uuid import uuid1
 
 
 from pycs.observable import ObservableDict
 from pycs.observable import ObservableDict
 
 
 
 
 class MediaFile(ObservableDict):
 class MediaFile(ObservableDict):
-    def __init__(self, obj, project_id):
+    def __init__(self, obj, project_id, type):
         if 'predictionResults' not in obj.keys():
         if 'predictionResults' not in obj.keys():
             obj['predictionResults'] = {}
             obj['predictionResults'] = {}
 
 
         self.project_id = project_id
         self.project_id = project_id
+        self.type = type
+
         super().__init__(obj)
         super().__init__(obj)
 
 
-    def _get_file(self, identifier):
-        file_directory = path.join(getcwd(), 'projects', self.project_id, 'data')
-        file_name = identifier + self['extension']
+    @property
+    def directory(self):
+        return join('projects', self.project_id, self.type)
 
 
-        return file_directory, file_name
+    @property
+    def full_name(self):
+        return self['id'] + self['extension']
 
 
-    def get_file(self):
-        return self._get_file(self['id'])
+    @property
+    def path(self):
+        return join(self.directory, self.full_name)
 
 
     def add_global_result(self, result, origin='user'):
     def add_global_result(self, result, origin='user'):
         self.remove_global_result()
         self.remove_global_result()

+ 10 - 1
pycs/projects/Project.py

@@ -1,5 +1,5 @@
 from json import load
 from json import load
-from os import path
+from os import path, mkdir
 from uuid import uuid1
 from uuid import uuid1
 
 
 from eventlet import spawn_after
 from eventlet import spawn_after
@@ -38,6 +38,15 @@ class Project(ObservableDict):
         # initialize super
         # initialize super
         super().__init__(obj, parent)
         super().__init__(obj, parent)
 
 
+        # create data and temp
+        data_path = path.join('projects', self['id'], 'data')
+        if not path.exists(data_path):
+            mkdir(data_path)
+
+        temp_path = path.join('projects', self['id'], 'temp')
+        if not path.exists(temp_path):
+            mkdir(temp_path)
+
         # subscribe to changes to write to disk afterwards
         # subscribe to changes to write to disk afterwards
         self.subscribe(lambda d, k: self.parent.write_project(self['id']))
         self.subscribe(lambda d, k: self.parent.write_project(self['id']))
 
 

+ 12 - 16
pycs/projects/VideoFile.py

@@ -1,4 +1,4 @@
-from os.path import splitext, join, exists
+from os.path import exists
 
 
 import cv2
 import cv2
 
 
@@ -7,44 +7,40 @@ from pycs.projects.MediaFile import MediaFile
 
 
 
 
 class VideoFile(MediaFile):
 class VideoFile(MediaFile):
-    def __init__(self, obj, project_id):
+    def __init__(self, obj, project_id, type='data'):
         # add type to object
         # add type to object
         obj['type'] = 'video'
         obj['type'] = 'video'
 
 
         # call parent constructor
         # call parent constructor
-        super().__init__(obj, project_id)
+        super().__init__(obj, project_id, type)
 
 
         # generate thumbnail
         # generate thumbnail
         self.__thumbnail = self.__read_video()
         self.__thumbnail = self.__read_video()
 
 
     def __read_video(self):
     def __read_video(self):
-        # determine some properties
-        path, name = self.get_file()
+        # create image file from properties
+        copy = self.copy()
+        copy['extension'] = '.jpg'
 
 
-        file_name, file_ext = splitext(name)
-        video_path = join(path, name)
-        image_path = join(path, f'{file_name}.jpg')
+        resized = ImageFile(copy, self.project_id, 'temp')
 
 
         # open video file
         # open video file
-        video = cv2.VideoCapture(video_path)
+        video = cv2.VideoCapture(self.path)
 
 
         # read fps value
         # read fps value
         self['fps'] = video.get(cv2.CAP_PROP_FPS)
         self['fps'] = video.get(cv2.CAP_PROP_FPS)
         self['frameCount'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
         self['frameCount'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
         # create thumbnail
         # create thumbnail
-        if not exists(image_path):
+        if not exists(resized.path):
             _, image = video.read()
             _, image = video.read()
-            cv2.imwrite(image_path, image)
+            cv2.imwrite(resized.path, image)
 
 
         # close video file
         # close video file
         video.release()
         video.release()
 
 
-        # return ImageFile from thumbnail
-        copy = self.copy()
-        copy['extension'] = '.jpg'
-
-        return ImageFile(copy, self.project_id)
+        # return
+        return resized
 
 
     def resize(self, maximum_width):
     def resize(self, maximum_width):
         return self.__thumbnail.resize(maximum_width)
         return self.__thumbnail.resize(maximum_width)