def post(self, working_file_id): working_file = self.check_access(working_file_id) file_path = self.save_uploaded_file_in_temporary_folder( working_file_id) file_store.add_file("working", working_file_id, file_path) os.remove(file_path) return working_file, 201
def store_db_backup(filename): """ Store given file located in the same directory, inside the files bucket using the `dbbackup` prefix. """ from zou.app import app with app.app_context(): file_store.add_file("dbbackup", filename, filename)
def save_file_preview(self, instance_id, uploaded_file, extension): """ Get uploaded file then save it in the file storage. """ tmp_folder = current_app.config["TMP_DIR"] file_name = instance_id + extension file_path = os.path.join(tmp_folder, file_name) uploaded_file.save(file_path) file_store.add_file("previews", instance_id, file_path) os.remove(file_path) return file_path
def download_attachment_file_from_another_instance(attachment_file): attachment_file_id = attachment_file["id"] extension = attachment_file["extension"] path = "/data/attachment-files/%s/file/%s" % (attachment_file_id, attachment_file["name"]) try: file_path = "/tmp/%s.%s" % (attachment_file_id, extension) gazu.client.download(path, file_path) file_store.add_file("attachments", attachment_file_id, file_path) os.remove(file_path) print("%s (%s) downloaded" % (file_path, "attachment")) except Exception as e: print(e) print("%s download failed" % file_path)
def save_file_preview(self, instance_id, uploaded_file, extension): """ Get uploaded file then save it in the file storage. """ tmp_folder = current_app.config["TMP_DIR"] file_name = instance_id + extension file_path = os.path.join(tmp_folder, file_name) uploaded_file.save(file_path) file_store.add_file("previews", instance_id, file_path) file_size = fs.get_file_size(file_path) preview_files_service.update_preview_file(instance_id, {"file_size": file_size}, silent=True) os.remove(file_path) return file_path
def create_attachment(comment, uploaded_file): tmp_folder = current_app.config["TMP_DIR"] filename = uploaded_file.filename mimetype = uploaded_file.mimetype extension = fs.get_file_extension(filename) attachment_file = AttachmentFile.create(name=filename, size=0, extension=extension, mimetype=mimetype, comment_id=comment["id"]) attachment_file_id = str(attachment_file.id) tmp_file_path = fs.save_file(tmp_folder, attachment_file_id, uploaded_file) size = fs.get_file_size(tmp_file_path) attachment_file.update({"size": size}) file_store.add_file("attachments", attachment_file_id, tmp_file_path) return attachment_file.present()
def patch_file_storage(): """ Patch to run after upgrade from 0.7.6 or lower to 0.8.0 or superior. It concerns only files stored on the hard drive. """ from zou import app from zou.app import config from zou.app.stores import file_store from zou.app.services import files_service with app.app.app_context(): if hasattr( config, "THUMBNAIL_FOLDER", ): preview_folder = config.THUMBNAIL_FOLDER else: preview_folder = config.PREVIEW_FOLDER print("Looking for existing file in %s" % preview_folder) originals_folder = os.path.join(preview_folder, "preview-files", "originals") if os.path.exists(originals_folder): for folder in os.listdir(originals_folder): subfolder = os.path.join(".", originals_folder, folder) print("Looking into folder: %s" % subfolder) for filename in os.listdir(subfolder): file_path = os.path.join(subfolder, filename) print("Loading file: %s" % file_path) instance_id = filename.split(".")[0] extension = filename.split(".")[1] if extension == "png": file_store.add_picture("originals", instance_id, file_path) elif extension == "mp4": file_store.add_movie("previews", instance_id, file_path) else: file_store.add_file("previews", instance_id, file_path) print("Original file stored: (%s, %s)" % (instance_id, extension)) try: files_service.update_preview_file( instance_id, {"exstension": extension}) except: print("Warning: preview file entry " "not found: %s." % instance_id) for prefix in ["thumbnails", "thumbnails-square", "previews"]: folder = os.path.join(preview_folder, "preview-files", prefix) if os.path.exists(folder): for subfoldername in os.listdir(folder): subfolder = os.path.join(".", folder, subfoldername) for filename in os.listdir(subfolder): file_path = os.path.join(subfolder, filename) instance_id = filename.split(".")[0] file_store.add_picture(prefix, instance_id, file_path) print("%s file stored: %s" % (prefix, instance_id)) for prefix in ["persons", "projects"]: folder = os.path.join(preview_folder, prefix) if os.path.exists(folder): for filename in os.listdir(folder): file_path = os.path.join(folder, filename) instance_id = filename.split(".")[0] file_store.add_picture("thumbnails", instance_id, file_path) print("%s file stored: %s" % (prefix, instance_id))