diff --git a/ayon_api/__init__.py b/ayon_api/__init__.py index 8d3ef554c..c43bfcdb1 100644 --- a/ayon_api/__init__.py +++ b/ayon_api/__init__.py @@ -70,6 +70,8 @@ delete, download_file_to_stream, download_file, + download_project_file, + download_project_file_to_stream, upload_file_from_stream, upload_file, upload_reviewable, @@ -349,6 +351,8 @@ "delete", "download_file_to_stream", "download_file", + "download_project_file", + "download_project_file_to_stream", "upload_file_from_stream", "upload_file", "upload_reviewable", diff --git a/ayon_api/_api.py b/ayon_api/_api.py index b02ba5b73..5681df929 100644 --- a/ayon_api/_api.py +++ b/ayon_api/_api.py @@ -992,6 +992,80 @@ def download_file( ) +def download_project_file( + project_name: str, + file_id: str, + filepath: str, + *, + chunk_size: Optional[int] = None, + progress: Optional[TransferProgress] = None, +) -> TransferProgress: + """Download project file to filepath. + + Project files are usually binary files, such as images, videos, + or other media files that can be accessed via api endpoint + '{server url}/api/projects/{project_name}/files/{file_id}'. + + Args: + project_name (str): Project name. + file_id (str): File id. + filepath (str): Path where file will be downloaded. + chunk_size (Optional[int]): Size of chunks that are received + in single loop. + progress (Optional[TransferProgress]): Object that gives ability + to track download progress. + + Returns: + TransferProgress: Progress object. + + """ + con = get_server_api_connection() + return con.download_project_file( + project_name=project_name, + file_id=file_id, + filepath=filepath, + chunk_size=chunk_size, + progress=progress, + ) + + +def download_project_file_to_stream( + project_name: str, + file_id: str, + stream: StreamType, + *, + chunk_size: Optional[int] = None, + progress: Optional[TransferProgress] = None, +) -> TransferProgress: + """Download project file to a stream. + + Project files are usually binary files, such as images, videos, + or other media files that can be accessed via api endpoint + '{server url}/api/projects/{project_name}/files/{file_id}'. + + Args: + project_name (str): Project name. + file_id (str): File id. + stream (StreamType): Stream where output will be stored. + chunk_size (Optional[int]): Size of chunks that are received + in single loop. + progress (Optional[TransferProgress]): Object that gives ability + to track download progress. + + Returns: + TransferProgress: Progress object. + + """ + con = get_server_api_connection() + return con.download_project_file_to_stream( + project_name=project_name, + file_id=file_id, + stream=stream, + chunk_size=chunk_size, + progress=progress, + ) + + def upload_file_from_stream( endpoint: str, stream: StreamType, diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index f74978d5e..9df64fbb2 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -1498,6 +1498,76 @@ def download_file( return progress + def download_project_file( + self, + project_name: str, + file_id: str, + filepath: str, + *, + chunk_size: Optional[int] = None, + progress: Optional[TransferProgress] = None, + ) -> TransferProgress: + """Download project file to filepath. + + Project files are usually binary files, such as images, videos, + or other media files that can be accessed via api endpoint + '{server url}/api/projects/{project_name}/files/{file_id}'. + + Args: + project_name (str): Project name. + file_id (str): File id. + filepath (str): Path where file will be downloaded. + chunk_size (Optional[int]): Size of chunks that are received + in single loop. + progress (Optional[TransferProgress]): Object that gives ability + to track download progress. + + Returns: + TransferProgress: Progress object. + + """ + return self.download_file( + f"api/projects/{project_name}/files/{file_id}", + filepath, + chunk_size=chunk_size, + progress=progress, + ) + + def download_project_file_to_stream( + self, + project_name: str, + file_id: str, + stream: StreamType, + *, + chunk_size: Optional[int] = None, + progress: Optional[TransferProgress] = None, + ) -> TransferProgress: + """Download project file to a stream. + + Project files are usually binary files, such as images, videos, + or other media files that can be accessed via api endpoint + '{server url}/api/projects/{project_name}/files/{file_id}'. + + Args: + project_name (str): Project name. + file_id (str): File id. + stream (StreamType): Stream where output will be stored. + chunk_size (Optional[int]): Size of chunks that are received + in single loop. + progress (Optional[TransferProgress]): Object that gives ability + to track download progress. + + Returns: + TransferProgress: Progress object. + + """ + return self.download_file_to_stream( + f"api/projects/{project_name}/files/{file_id}", + stream, + chunk_size=chunk_size, + progress=progress, + ) + @staticmethod def _upload_chunks_iter( file_stream: StreamType,