def post(self, instance_id): if not self.is_exist(instance_id): abort(404) self.check_permissions(instance_id) self.prepare_creation(instance_id) tmp_folder = current_app.config["TMP_DIR"] uploaded_file = request.files["file"] thumbnail_path = thumbnail_utils.save_file( tmp_folder, instance_id, uploaded_file ) thumbnail_path = thumbnail_utils.turn_into_thumbnail( thumbnail_path, size=self.size ) file_store.add_picture("thumbnails", instance_id, thumbnail_path) os.remove(thumbnail_path) self.clear_cache_file(instance_id) thumbnail_url_path = thumbnail_utils.url_path( self.data_type, instance_id ) self.emit_event(instance_id) return {"thumbnail_path": thumbnail_url_path}, 201
def test_add_and_open_picture(self): file_path_fixture = self.get_fixture_file_path("thumbnails/th01.png") file_store.add_picture("thumbnails", "63e453f1-9655-49ad-acba-ff7f27c49e9d", file_path_fixture) file_name = "thumbnails-63e453f1-9655-49ad-acba-ff7f27c49e9d" result_path = file_store.path(file_store.pictures, file_name) self.assertTrue(os.path.exists(result_path))
def save_variants(preview_file_id, original_picture_path): """ Build variants of a picture file and save them in the main storage. """ variants = thumbnail_utils.generate_preview_variants( original_picture_path, preview_file_id) variants.append(("original", original_picture_path)) for (name, path) in variants: file_store.add_picture(name, preview_file_id, path) os.remove(path) return [] # variants
def upload_entity_thumbnail(entity): """ Upload thumbnail file for given entity to object storage. """ preview_folder = os.getenv("PREVIEW_FOLDER", "/opt/zou/previews") local = LocalBackend("local", {"root": os.path.join(preview_folder, "pictures")}) file_path = local.path("thumbnails-" + str(entity.id)) if entity.has_avatar: file_store.add_picture("thumbnails", str(entity.id), file_path) print("%s uploaded" % file_path)
def save_thumbnail(person, thumbnail): from zou.app import app with app.app_context(): thumbnail_path = "/tmp/ldap_th.jpg" with open(thumbnail_path, "wb") as th_file: th_file.write(thumbnail) thumbnail_png_path = thumbnail_utils.convert_jpg_to_png( thumbnail_path) thumbnail_utils.turn_into_thumbnail( thumbnail_png_path, size=thumbnail_utils.BIG_SQUARE_SIZE) file_store.add_picture("thumbnails", person["id"], thumbnail_png_path) os.remove(thumbnail_png_path) persons_service.update_person(person["id"], {"has_avatar": True})
def download_thumbnail_from_another_instance(model_name, model_id): """ Download into the local storage the thumbnail for a given model instance. """ from zou.app import app with app.app_context(): file_path = "/tmp/thumbnails-%s.png" % str(model_id) path = "/pictures/thumbnails/%ss/%s.png" % (model_name, model_id) try: gazu.client.download(path, file_path) file_store.add_picture("thumbnails", model_id, file_path) os.remove(file_path) print("%s downloaded" % file_path) except Exception as e: print(e) print("%s download failed" % file_path) return path
def upload_preview(preview_file): """ Upload all files link to preview file entry: orginal file and variants. """ print("upload preview %s (%s)" % (preview_file.id, preview_file.extension)) preview_folder = os.getenv("PREVIEW_FOLDER", "/opt/zou/previews") local_picture = LocalBackend( "local", {"root": os.path.join(preview_folder, "pictures")}) local_movie = LocalBackend( "local", {"root": os.path.join(preview_folder, "movies")}) local_file = LocalBackend("local", {"root": os.path.join(preview_folder, "files")}) is_movie = preview_file.extension == "mp4" is_picture = preview_file.extension == "png" is_file = not is_movie and not is_picture preview_file_id = str(preview_file.id) file_key = "previews-%s" % preview_file_id if is_picture: file_path = local_picture.path(file_key) ul_func = file_store.add_picture exists_func = file_store.exists_picture elif is_movie: file_path = local_movie.path(file_key) ul_func = file_store.add_movie exists_func = file_store.exists_movie elif is_file: file_path = local_file.path(file_key) ul_func = file_store.add_file exists_func = file_store.exists_file if is_movie or is_picture: for prefix in ["thumbnails", "thumbnails-square", "original"]: pic_file_path = local_picture.path("%s-%s" % (prefix, str(preview_file.id))) if os.path.exists(pic_file_path) and not file_store.exists_picture( prefix, preview_file_id): file_store.add_picture(prefix, preview_file_id, pic_file_path) prefix = "previews" if os.path.exists(file_path) and not exists_func(prefix, preview_file_id): ul_func(prefix, preview_file_id, file_path) print("%s uploaded" % file_path)
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))