Skip to content

Commit 921b714

Browse files
authored
Merge branch 'develop' into enhancement/helper-function-for-new-task
2 parents a56d7ea + 464f282 commit 921b714

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

ayon_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@
271271
get_version_thumbnail,
272272
get_workfile_thumbnail,
273273
create_thumbnail,
274+
create_thumbnail_with_stream,
274275
update_thumbnail,
276+
update_thumbnail_from_stream,
275277
)
276278

277279

@@ -546,5 +548,7 @@
546548
"get_version_thumbnail",
547549
"get_workfile_thumbnail",
548550
"create_thumbnail",
551+
"create_thumbnail_with_stream",
549552
"update_thumbnail",
553+
"update_thumbnail_from_stream",
550554
)

ayon_api/_api.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7726,6 +7726,34 @@ def create_thumbnail(
77267726
)
77277727

77287728

7729+
def create_thumbnail_with_stream(
7730+
project_name: str,
7731+
stream: StreamType,
7732+
thumbnail_id: Optional[str] = None,
7733+
) -> str:
7734+
"""Create new thumbnail on server from byte stream.
7735+
7736+
Args:
7737+
project_name (str): Project where the thumbnail will be created
7738+
and can be used.
7739+
stream (StreamType): Thumbnail content stream.
7740+
thumbnail_id (Optional[str]): Prepared if of thumbnail.
7741+
7742+
Returns:
7743+
str: Created thumbnail id.
7744+
7745+
Raises:
7746+
ValueError: When a thumbnail source cannot be processed.
7747+
7748+
"""
7749+
con = get_server_api_connection()
7750+
return con.create_thumbnail_with_stream(
7751+
project_name=project_name,
7752+
stream=stream,
7753+
thumbnail_id=thumbnail_id,
7754+
)
7755+
7756+
77297757
def update_thumbnail(
77307758
project_name: str,
77317759
thumbnail_id: str,
@@ -7751,3 +7779,27 @@ def update_thumbnail(
77517779
thumbnail_id=thumbnail_id,
77527780
src_filepath=src_filepath,
77537781
)
7782+
7783+
7784+
def update_thumbnail_from_stream(
7785+
project_name: str,
7786+
thumbnail_id: str,
7787+
stream: StreamType,
7788+
) -> None:
7789+
"""Change thumbnail content by id.
7790+
7791+
Update can be also used to create new thumbnail.
7792+
7793+
Args:
7794+
project_name (str): Project where the thumbnail will be created
7795+
and can be used.
7796+
thumbnail_id (str): Thumbnail id to update.
7797+
stream (StreamType): Thumbnail content stream.
7798+
7799+
"""
7800+
con = get_server_api_connection()
7801+
return con.update_thumbnail_from_stream(
7802+
project_name=project_name,
7803+
thumbnail_id=thumbnail_id,
7804+
stream=stream,
7805+
)

ayon_api/_api_helpers/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
AnyEntityDict,
1414
ServerVersion,
1515
ProjectDict,
16+
StreamType,
1617
)
1718

1819
_PLACEHOLDER = object()
@@ -84,6 +85,16 @@ def upload_file(
8485
) -> requests.Response:
8586
raise NotImplementedError()
8687

88+
def upload_file_from_stream(
89+
self,
90+
endpoint: str,
91+
stream: StreamType,
92+
progress: Optional[TransferProgress] = None,
93+
request_type: Optional[RequestType] = None,
94+
**kwargs
95+
) -> requests.Response:
96+
raise NotImplementedError()
97+
8798
def download_file(
8899
self,
89100
endpoint: str,

ayon_api/_api_helpers/thumbnails.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import os
44
import warnings
5+
import typing
56
from typing import Optional
67

78
from ayon_api.utils import (
9+
get_media_mime_type_for_stream,
810
get_media_mime_type,
911
ThumbnailContent,
1012
RequestTypes,
@@ -13,6 +15,9 @@
1315

1416
from .base import BaseServerAPI
1517

18+
if typing.TYPE_CHECKING:
19+
from .typing import StreamType
20+
1621

1722
class ThumbnailsAPI(BaseServerAPI):
1823
def get_thumbnail_by_id(
@@ -259,6 +264,45 @@ def create_thumbnail(
259264
response.raise_for_status()
260265
return response.json()["id"]
261266

267+
def create_thumbnail_with_stream(
268+
self,
269+
project_name: str,
270+
stream: StreamType,
271+
thumbnail_id: Optional[str] = None,
272+
) -> str:
273+
"""Create new thumbnail on server from byte stream.
274+
275+
Args:
276+
project_name (str): Project where the thumbnail will be created
277+
and can be used.
278+
stream (StreamType): Thumbnail content stream.
279+
thumbnail_id (Optional[str]): Prepared if of thumbnail.
280+
281+
Returns:
282+
str: Created thumbnail id.
283+
284+
Raises:
285+
ValueError: When a thumbnail source cannot be processed.
286+
287+
"""
288+
if thumbnail_id:
289+
self.update_thumbnail_from_stream(
290+
project_name,
291+
thumbnail_id,
292+
stream
293+
)
294+
return thumbnail_id
295+
296+
mime_type = get_media_mime_type_for_stream(stream)
297+
response = self.upload_file_from_stream(
298+
f"projects/{project_name}/thumbnails",
299+
stream,
300+
request_type=RequestTypes.post,
301+
headers={"Content-Type": mime_type},
302+
)
303+
response.raise_for_status()
304+
return response.json()["id"]
305+
262306
def update_thumbnail(
263307
self, project_name: str, thumbnail_id: str, src_filepath: str
264308
) -> None:
@@ -288,6 +332,32 @@ def update_thumbnail(
288332
)
289333
response.raise_for_status()
290334

335+
def update_thumbnail_from_stream(
336+
self,
337+
project_name: str,
338+
thumbnail_id: str,
339+
stream: StreamType,
340+
) -> None:
341+
"""Change thumbnail content by id.
342+
343+
Update can be also used to create new thumbnail.
344+
345+
Args:
346+
project_name (str): Project where the thumbnail will be created
347+
and can be used.
348+
thumbnail_id (str): Thumbnail id to update.
349+
stream (StreamType): Thumbnail content stream.
350+
351+
"""
352+
mime_type = get_media_mime_type_for_stream(stream)
353+
response = self.upload_file_from_stream(
354+
f"projects/{project_name}/thumbnails/{thumbnail_id}",
355+
stream,
356+
request_type=RequestTypes.put,
357+
headers={"Content-Type": mime_type},
358+
)
359+
response.raise_for_status()
360+
291361
def _prepare_thumbnail_content(
292362
self,
293363
project_name: str,

0 commit comments

Comments
 (0)