Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ayon_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@
get_version_thumbnail,
get_workfile_thumbnail,
create_thumbnail,
create_thumbnail_with_stream,
update_thumbnail,
update_thumbnail_from_stream,
)


Expand Down Expand Up @@ -546,5 +548,7 @@
"get_version_thumbnail",
"get_workfile_thumbnail",
"create_thumbnail",
"create_thumbnail_with_stream",
"update_thumbnail",
"update_thumbnail_from_stream",
)
52 changes: 52 additions & 0 deletions ayon_api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7726,6 +7726,34 @@ def create_thumbnail(
)


def create_thumbnail_with_stream(
project_name: str,
stream: StreamType,
thumbnail_id: Optional[str] = None,
) -> str:
"""Create new thumbnail on server from byte stream.

Args:
project_name (str): Project where the thumbnail will be created
and can be used.
stream (StreamType): Thumbnail content stream.
thumbnail_id (Optional[str]): Prepared if of thumbnail.

Returns:
str: Created thumbnail id.

Raises:
ValueError: When a thumbnail source cannot be processed.

"""
con = get_server_api_connection()
return con.create_thumbnail_with_stream(
project_name=project_name,
stream=stream,
thumbnail_id=thumbnail_id,
)


def update_thumbnail(
project_name: str,
thumbnail_id: str,
Expand All @@ -7751,3 +7779,27 @@ def update_thumbnail(
thumbnail_id=thumbnail_id,
src_filepath=src_filepath,
)


def update_thumbnail_from_stream(
project_name: str,
thumbnail_id: str,
stream: StreamType,
) -> None:
"""Change thumbnail content by id.

Update can be also used to create new thumbnail.

Args:
project_name (str): Project where the thumbnail will be created
and can be used.
thumbnail_id (str): Thumbnail id to update.
stream (StreamType): Thumbnail content stream.

"""
con = get_server_api_connection()
return con.update_thumbnail_from_stream(
project_name=project_name,
thumbnail_id=thumbnail_id,
stream=stream,
)
11 changes: 11 additions & 0 deletions ayon_api/_api_helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
AnyEntityDict,
ServerVersion,
ProjectDict,
StreamType,
)

_PLACEHOLDER = object()
Expand Down Expand Up @@ -84,6 +85,16 @@ def upload_file(
) -> requests.Response:
raise NotImplementedError()

def upload_file_from_stream(
self,
endpoint: str,
stream: StreamType,
progress: Optional[TransferProgress] = None,
request_type: Optional[RequestType] = None,
**kwargs
) -> requests.Response:
raise NotImplementedError()

def download_file(
self,
endpoint: str,
Expand Down
70 changes: 70 additions & 0 deletions ayon_api/_api_helpers/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import os
import warnings
import typing
from typing import Optional

from ayon_api.utils import (
get_media_mime_type_for_stream,
get_media_mime_type,
ThumbnailContent,
RequestTypes,
Expand All @@ -13,6 +15,9 @@

from .base import BaseServerAPI

if typing.TYPE_CHECKING:
from .typing import StreamType


class ThumbnailsAPI(BaseServerAPI):
def get_thumbnail_by_id(
Expand Down Expand Up @@ -259,6 +264,45 @@ def create_thumbnail(
response.raise_for_status()
return response.json()["id"]

def create_thumbnail_with_stream(
self,
project_name: str,
stream: StreamType,
thumbnail_id: Optional[str] = None,
) -> str:
"""Create new thumbnail on server from byte stream.

Args:
project_name (str): Project where the thumbnail will be created
and can be used.
stream (StreamType): Thumbnail content stream.
thumbnail_id (Optional[str]): Prepared if of thumbnail.

Returns:
str: Created thumbnail id.

Raises:
ValueError: When a thumbnail source cannot be processed.

"""
if thumbnail_id:
self.update_thumbnail_from_stream(
project_name,
thumbnail_id,
stream
)
return thumbnail_id

mime_type = get_media_mime_type_for_stream(stream)
response = self.upload_file_from_stream(
f"projects/{project_name}/thumbnails",
stream,
request_type=RequestTypes.post,
headers={"Content-Type": mime_type},
)
response.raise_for_status()
return response.json()["id"]

def update_thumbnail(
self, project_name: str, thumbnail_id: str, src_filepath: str
) -> None:
Expand Down Expand Up @@ -288,6 +332,32 @@ def update_thumbnail(
)
response.raise_for_status()

def update_thumbnail_from_stream(
self,
project_name: str,
thumbnail_id: str,
stream: StreamType,
) -> None:
"""Change thumbnail content by id.

Update can be also used to create new thumbnail.

Args:
project_name (str): Project where the thumbnail will be created
and can be used.
thumbnail_id (str): Thumbnail id to update.
stream (StreamType): Thumbnail content stream.

"""
mime_type = get_media_mime_type_for_stream(stream)
response = self.upload_file_from_stream(
f"projects/{project_name}/thumbnails/{thumbnail_id}",
stream,
request_type=RequestTypes.put,
headers={"Content-Type": mime_type},
)
response.raise_for_status()

def _prepare_thumbnail_content(
self,
project_name: str,
Expand Down