def test_get_preview_file_name(self): preview_file = files_service.create_preview_file("main", 3, self.shot_task["id"], self.user["id"], source="webgui") name = names_service.get_preview_file_name(preview_file["id"]) self.assertEqual(name, "cosmos_landromat_e01_s01_p01_animation_v3.mp4") preview_file = files_service.create_preview_file("main", 3, self.asset_task["id"], self.user["id"], source="webgui") name = names_service.get_preview_file_name(preview_file["id"]) self.assertEqual(name, "cosmos_landromat_props_tree_shaders_v3.mp4") preview_file = files_service.create_preview_file( "main", 4, self.asset_task["id"], self.user["id"], source="webgui", position=5, ) name = names_service.get_preview_file_name(preview_file["id"]) self.assertEqual(name, "cosmos_landromat_props_tree_shaders_v4-5.mp4")
def retrieve_playlist_tmp_files(playlist): """ Retrieve all files for a given playlist into the temporary folder. """ preview_file_ids = [] for shot in playlist["shots"]: if ("preview_file_id" in shot and shot["preview_file_id"] is not None and len(shot["preview_file_id"]) > 0): preview_file = files_service.get_preview_file( shot["preview_file_id"]) if preview_file is not None and preview_file["extension"] == "mp4": preview_file_ids.append(preview_file["id"]) file_paths = [] for preview_file_id in preview_file_ids: if config.FS_BACKEND == "local": file_path = file_store.get_local_movie_path( "previews", preview_file_id) else: file_path = os.path.join( config.TMP_DIR, "cache-previews-%s.%s" % (preview_file_id, "mp4"), ) if not os.path.exists(file_path) or os.path.getsize( file_path) == 0: with open(file_path, "wb") as tmp_file: for chunk in file_store.open_movie("previews", preview_file_id): tmp_file.write(chunk) file_name = names_service.get_preview_file_name(preview_file_id) tmp_file_path = os.path.join(config.TMP_DIR, file_name) copyfile(file_path, tmp_file_path) file_paths.append((tmp_file_path, file_name)) return file_paths
def send_storage_file( get_local_path, open_file, prefix, preview_file_id, extension, mimetype="application/octet-stream", as_attachment=False, ): """ Send file from storage. If it's not a local storage, cache the file in a temporary folder before sending it. It accepts conditional headers. """ file_path = fs.get_file_path_and_file( config, get_local_path, open_file, prefix, preview_file_id, extension ) attachment_filename = "" if as_attachment: attachment_filename = names_service.get_preview_file_name( preview_file_id ) try: return flask_send_file( file_path, conditional=True, mimetype=mimetype, as_attachment=as_attachment, attachment_filename=attachment_filename, ) except IOError as e: current_app.logger.error(e) raise FileNotFound
def retrieve_playlist_tmp_file(preview_file): if preview_file["extension"] == "mp4": get_path_func = file_store.get_local_movie_path open_func = file_store.open_movie prefix = "previews" elif preview_file["extension"] == "png": get_path_func = file_store.get_local_picture_path open_func = file_store.open_picture prefix = "original" else: get_path_func = file_store.get_local_file_path open_func = file_store.open_file prefix = "previews" if config.FS_BACKEND == "local": file_path = get_path_func(prefix, preview_file["id"]) else: file_path = os.path.join( config.TMP_DIR, "cache-previews-%s.%s" % (preview_file["id"], preview_file["extension"]), ) if not os.path.exists(file_path) or os.path.getsize(file_path) == 0: with open(file_path, "wb") as tmp_file: for chunk in open_func(prefix, preview_file["id"]): tmp_file.write(chunk) file_name = names_service.get_preview_file_name(preview_file["id"]) tmp_file_path = os.path.join(config.TMP_DIR, file_name) copyfile(file_path, tmp_file_path) return tmp_file_path, file_name
def send_storage_file( get_path, open_file, prefix, preview_file_id, extension, mimetype="application/octet-stream", as_attachment=False ): """ Send file from storage. If it's not a local storage, cache the file in a temporary folder before sending it. It accepts conditional headers. """ if config.FS_BACKEND == "local": file_path = get_path(prefix, preview_file_id) else: file_path = os.path.join( config.TMP_DIR, "cache-%s-%s.%s" % (prefix, preview_file_id, extension) ) if not os.path.exists(file_path): with open(file_path, 'wb') as tmp_file: for chunk in open_file(prefix, preview_file_id): tmp_file.write(chunk) attachment_filename = "" if as_attachment: attachment_filename = \ names_service.get_preview_file_name(preview_file_id) try: return flask_send_file( file_path, conditional=True, mimetype=mimetype, as_attachment=as_attachment, attachment_filename=attachment_filename ) except IOError as e: current_app.logger.error(e) return { "error": True, "message": "File not found for: %s %s" % ( prefix, preview_file_id ) }, 404 except FileNotFound: return { "error": True, "message": "File not found for: %s %s" % ( prefix, preview_file_id ) }, 404
def retrieve_playlist_tmp_files(playlist, only_movies=False): """ Retrieve all files for a given playlist into the temporary folder. """ preview_files = [] for entity in playlist["shots"]: if ("preview_file_id" in entity and entity["preview_file_id"] is not None and len(entity["preview_file_id"]) > 0): preview_file = files_service.get_preview_file( entity["preview_file_id"]) if preview_file is not None and ( (only_movies and preview_file["extension"] == "mp4") or not only_movies): preview_files.append(preview_file) file_paths = [] for preview_file in preview_files: prefix = "original" if preview_file["extension"] == "mp4": get_path_func = file_store.get_local_movie_path open_func = file_store.open_movie prefix = "previews" elif preview_file["extension"] == "png": get_path_func = file_store.get_local_picture_path open_func = file_store.open_picture else: get_path_func = file_store.get_local_file_path open_func = file_store.open_file if config.FS_BACKEND == "local": file_path = get_path_func(prefix, preview_file["id"]) else: file_path = os.path.join( config.TMP_DIR, "cache-previews-%s.%s" % (preview_file["id"], preview_file["extension"]), ) if not os.path.exists(file_path) or os.path.getsize( file_path) == 0: with open(file_path, "wb") as tmp_file: for chunk in open_func(prefix, preview_file["id"]): tmp_file.write(chunk) file_name = names_service.get_preview_file_name(preview_file["id"]) tmp_file_path = os.path.join(config.TMP_DIR, file_name) copyfile(file_path, tmp_file_path) file_paths.append((tmp_file_path, file_name)) return file_paths