-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
158 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,75 @@ | ||
from os.path import basename | ||
from typing import Optional | ||
|
||
from PIL import Image as PillowImage | ||
from pythorhead.requestor import Request, Requestor | ||
from io import BytesIO | ||
|
||
|
||
class Image: | ||
def __init__(self, _requestor: Requestor) -> None: | ||
self._requestor = _requestor | ||
|
||
def upload(self, image_path: str) -> Optional[dict]: | ||
def upload(self, image): | ||
""" | ||
Upload an image | ||
Args: | ||
image_path (str) | ||
image (str, PIL.Image): Image ins str or PIL format | ||
Returns: | ||
Optional[dict]: pictrs upload data, if successful | ||
""" | ||
data = None | ||
if isinstance(image, str): | ||
with open(image, "rb") as image_bytes: | ||
data = self._requestor.image( | ||
Request.POST, | ||
files={"images[]": image_bytes}, | ||
) | ||
else: | ||
img: PillowImage = image | ||
img_byte_array = BytesIO() | ||
image_bytes = img.save(img_byte_array, format='webp', quality=95) | ||
img_byte_array.seek(0) | ||
|
||
data = self._requestor.image( | ||
Request.POST, | ||
files={"images[]": img_byte_array}, | ||
) | ||
if data and "files" in data: | ||
for file in data["files"]: | ||
file["image_url"] = "/".join( | ||
( | ||
self._requestor._auth.image_url, | ||
file["file"], | ||
), | ||
) | ||
file["delete_url"] = "/".join( | ||
( | ||
self._requestor._auth.image_url, | ||
"delete", | ||
file["delete_token"], | ||
file["file"], | ||
), | ||
) | ||
del file["file"] | ||
del file["delete_token"] | ||
|
||
return data["files"] | ||
|
||
def delete(self, image_delete_url: str): | ||
""" | ||
Delete an lemmy image via pictrs | ||
Args: | ||
image_delete_url (str): The pictrs delete URL | ||
Returns: | ||
Optional[dict]: image data if successful | ||
bool: True is succesfully deleted | ||
""" | ||
with open(image_path, "rb") as image: | ||
data = self._requestor.image( | ||
Request.POST, | ||
files={"images[]": image}, | ||
) | ||
if data and "files" in data: | ||
for file in data["files"]: | ||
file["image_url"] = "/".join( | ||
( | ||
self._requestor._auth.image_url, | ||
file["file"], | ||
), | ||
) | ||
file["delete_url"] = "/".join( | ||
( | ||
self._requestor._auth.image_url, | ||
"delete", | ||
file["delete_token"], | ||
file["file"], | ||
), | ||
) | ||
del file["file"] | ||
del file["delete_token"] | ||
|
||
return data["files"] | ||
req = self._requestor.image_del( | ||
Request.GET, | ||
image_delete_url, | ||
) | ||
return req.ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters