def add_new_file(request_user: User): """ This method saves a file to the specified section of the document for storing workflow data. Any existing value that matches filename and the metadata is deleted. Before saving a value. GridFS document under the specified key is deleted. File: File is stored under 'request.files["file"]' Metadata: Metadata are stored under 'request.form["Metadata"]' Args: request_user (User): the instance of the started user Returns: int: ObjectId of GridFS File. """ try: file = get_file_in_request('file') filter_metadata = generate_metadata_filter('metadata', request) # Check if file exists is_exist_file = media_file_manager.exist_media_file(file.filename, filter_metadata) if is_exist_file: grid_fs_file = media_file_manager.get_media_file(file.filename, filter_metadata) media_file_manager.delete_media_file(grid_fs_file._file['public_id']) metadata = get_element_from_data_request('metadata', request) metadata['author_id'] = request_user.public_id metadata['mime_type'] = file.mimetype ack = media_file_manager.insert_media_file(data=file, metadata=metadata) except ObjectInsertError: return abort(500) resp = make_response(ack) return resp
def download_file(filename: str): """ This method download a file to the specified section of the document. Any existing value that matches the file name and metadata will be considered. Raises: MediaFileManagerGetError: If the file could not be found. Args: filename (str): name must be unique Returns: File """ try: filter_metadata = generate_metadata_filter('metadata', request) filter_metadata.update({'filename': filename}) result = media_file_manager.get_file(metadata=filter_metadata, blob=True) except MediaFileManagerGetError: return abort(500) return Response( result, mimetype="application/octet-stream", headers={ "Content-Disposition": "attachment; filename=%s" % filename } )
def add_new_file(request_user: UserModel): """ This method saves a file to the specified section of the document for storing workflow data. Any existing value that matches filename and the metadata is deleted. Before saving a value. GridFS document under the specified key is deleted. For Example: Create a unique media file element: - Folders in the same directory are unique. - The Folder-Name can exist in different directories Create sub-folders: - Selected folder is considered as parent This also applies for files File: File is stored under 'request.files.get('files')' Metadata: Metadata are stored under 'request.form["Metadata"]' Raises: MediaFileManagerGetError: If the file could not be found. MediaFileManagerInsertError: If something went wrong during insert. Args: request_user (UserModel): the instance of the started user Returns: New MediaFile. """ try: file = get_file_in_request('file') filter_metadata = generate_metadata_filter('metadata', request) filter_metadata.update({'filename': file.filename}) metadata = get_element_from_data_request('metadata', request) # Check if file exists is_exist_file = media_file_manager.exist_file(filter_metadata) exist = None if is_exist_file: exist = media_file_manager.get_file(filter_metadata) media_file_manager.delete_file(exist['public_id']) # If file exist overwrite the references from previous file if exist: metadata['reference'] = exist['metadata']['reference'] metadata['reference_type'] = exist['metadata']['reference_type'] metadata['author_id'] = request_user.public_id metadata['mime_type'] = file.mimetype result = media_file_manager.insert_file(data=file, metadata=metadata) except (MediaFileManagerInsertError, MediaFileManagerGetError): return abort(500) api_response = InsertSingleResponse(raw=result, result_id=result['public_id'], url=request.url) return api_response.make_response(prefix='library')
def download_media_file(request_user: User, filename: str): try: filter_metadata = generate_metadata_filter('metadata', request) grid_fs_file = media_file_manager.get_media_file( filename, filter_metadata).read() except ObjectInsertError: return abort(500) return Response( grid_fs_file, mimetype="application/octet-stream", headers={"Content-Disposition": "attachment; filename=%s" % filename})
def get_object_list(request_user: User): """ get all objects in database Returns: list of media_files """ try: media_file_list = media_file_manager.get_all_media_files(generate_metadata_filter('metadata', request)) except ObjectManagerGetError as err: LOGGER.error(err.message) return abort(404) if len(media_file_list) < 1: return make_response(media_file_list, 204) return make_response(media_file_list)
def get_file(filename: str): """ This method fetch a file to the specified section of the document. Any existing value that matches the file name and metadata will be considered. Raises: MediaFileManagerGetError: If the file could not be found. Args: filename: name must be unique Returns: MediaFile as JSON """ try: filter_metadata = generate_metadata_filter('metadata', request) filter_metadata.update({'filename': filename}) result = media_file_manager.get_file(metadata=filter_metadata) except MediaFileManagerGetError as err: return abort(404, err.message) return make_response(result)
def get_file_by_name(name: str): """ Validation: Check folder name for uniqueness Create a unique directory: - Folders in the same directory are unique. - The same Folder-Name can exist in different directories Create subfolders: - Selected folder is considered as parent This also applies for files Args: name: folderName must be unique Returns: True if exist else False """ try: filter_metadata = generate_metadata_filter('metadata', request) media_file = media_file_manager.exist_media_file(name, filter_metadata) except ObjectManagerGetError as err: return abort(404, err.message) return make_response(media_file)