2023-02-07 19:07:15 +00:00
|
|
|
from django.core.files import File
|
2022-12-11 19:37:28 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2023-02-14 02:40:10 +00:00
|
|
|
from hatchway import ApiError, QueryOrBody, api_view
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
from activities.models import PostAttachment, PostAttachmentStates
|
|
|
|
from api import schemas
|
|
|
|
from core.files import blurhash_image, resize_image
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
from ..decorators import scope_required
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:media")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.post
|
2022-12-11 19:37:28 +00:00
|
|
|
def upload_media(
|
|
|
|
request,
|
2023-02-07 19:07:15 +00:00
|
|
|
file: File,
|
|
|
|
description: QueryOrBody[str] = "",
|
|
|
|
focus: QueryOrBody[str] = "0,0",
|
|
|
|
) -> schemas.MediaAttachment:
|
2022-12-11 19:37:28 +00:00
|
|
|
main_file = resize_image(
|
|
|
|
file,
|
|
|
|
size=(2000, 2000),
|
|
|
|
cover=False,
|
|
|
|
)
|
|
|
|
thumbnail_file = resize_image(
|
|
|
|
file,
|
|
|
|
size=(400, 225),
|
|
|
|
cover=True,
|
|
|
|
)
|
|
|
|
attachment = PostAttachment.objects.create(
|
|
|
|
blurhash=blurhash_image(thumbnail_file),
|
|
|
|
mimetype="image/webp",
|
|
|
|
width=main_file.image.width,
|
|
|
|
height=main_file.image.height,
|
2023-02-07 19:07:15 +00:00
|
|
|
name=description or None,
|
2022-12-11 19:37:28 +00:00
|
|
|
state=PostAttachmentStates.fetched,
|
2023-03-12 22:19:40 +00:00
|
|
|
author=request.identity,
|
2022-12-11 19:37:28 +00:00
|
|
|
)
|
|
|
|
attachment.file.save(
|
|
|
|
main_file.name,
|
|
|
|
main_file,
|
|
|
|
)
|
|
|
|
attachment.thumbnail.save(
|
|
|
|
thumbnail_file.name,
|
|
|
|
thumbnail_file,
|
|
|
|
)
|
|
|
|
attachment.save()
|
2023-02-07 19:07:15 +00:00
|
|
|
return schemas.MediaAttachment.from_post_attachment(attachment)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:media")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-11 19:37:28 +00:00
|
|
|
def get_media(
|
|
|
|
request,
|
|
|
|
id: str,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> schemas.MediaAttachment:
|
2022-12-11 19:37:28 +00:00
|
|
|
attachment = get_object_or_404(PostAttachment, pk=id)
|
2023-03-12 22:19:40 +00:00
|
|
|
if attachment.post:
|
|
|
|
if attachment.post.author != request.identity:
|
|
|
|
raise ApiError(401, "Not the author of this attachment")
|
|
|
|
elif attachment.author and attachment.author != request.identity:
|
2023-02-14 02:40:10 +00:00
|
|
|
raise ApiError(401, "Not the author of this attachment")
|
2023-02-07 19:07:15 +00:00
|
|
|
return schemas.MediaAttachment.from_post_attachment(attachment)
|
2022-12-11 19:37:28 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("write:media")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.put
|
2022-12-11 19:37:28 +00:00
|
|
|
def update_media(
|
|
|
|
request,
|
|
|
|
id: str,
|
2023-02-07 19:07:15 +00:00
|
|
|
description: QueryOrBody[str] = "",
|
|
|
|
focus: QueryOrBody[str] = "0,0",
|
|
|
|
) -> schemas.MediaAttachment:
|
2022-12-11 19:37:28 +00:00
|
|
|
attachment = get_object_or_404(PostAttachment, pk=id)
|
2023-05-02 15:58:32 +00:00
|
|
|
if attachment.post:
|
|
|
|
if attachment.post.author != request.identity:
|
|
|
|
raise ApiError(401, "Not the author of this attachment")
|
|
|
|
elif attachment.author != request.identity:
|
2023-02-14 02:40:10 +00:00
|
|
|
raise ApiError(401, "Not the author of this attachment")
|
2023-02-07 19:07:15 +00:00
|
|
|
attachment.name = description or None
|
2022-12-11 19:37:28 +00:00
|
|
|
attachment.save()
|
2023-02-07 19:07:15 +00:00
|
|
|
return schemas.MediaAttachment.from_post_attachment(attachment)
|