-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
12 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
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import hashlib | ||
import logging | ||
import mimetypes | ||
import os | ||
import secrets | ||
|
||
from aiobotocore.session import get_session | ||
|
||
from suggestions.exceptions import InvalidFileType | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
async def upload_file_to_r2( | ||
*, | ||
file_name: str, | ||
file_data: bytes, | ||
guild_id: int, | ||
user_id: int, | ||
) -> str: | ||
"""Upload a file to R2 and get the cdn url back""" | ||
|
||
session = get_session() | ||
async with session.create_client( | ||
"s3", | ||
endpoint_url=os.environ["ENDPOINT"], | ||
aws_access_key_id=os.environ["ACCESS_KEY"], | ||
aws_secret_access_key=os.environ["SECRET_ACCESS_KEY"], | ||
) as client: | ||
mimetype_guessed, _ = mimetypes.guess_type(file_name) | ||
accepted_mimetypes: dict[str, set[str]] = { | ||
"image/jpeg": {".jpeg", ".jpg"}, | ||
"image/png": {".png"}, | ||
"image/gif": {".gif"}, | ||
"video/mp3": {".mp3"}, | ||
"video/mp4": {".mp4"}, | ||
"video/mpeg": {".mpeg"}, | ||
"video/webm": {".webm"}, | ||
"image/webp": {".webp"}, | ||
"audio/webp": {".weba"}, | ||
} | ||
file_names = accepted_mimetypes.get(mimetype_guessed) | ||
if file_names is None: | ||
raise InvalidFileType | ||
|
||
for ext in file_names: | ||
if file_name.endswith(ext): | ||
break | ||
else: | ||
raise InvalidFileType | ||
|
||
file_key = hashlib.sha256(file_data + secrets.token_bytes(16)).hexdigest() | ||
key = "{}/{}.{}".format(guild_id, file_key, ext) | ||
await client.put_object(Bucket=os.environ["BUCKET"], Key=key, Body=file_data) | ||
log.debug("User %s in guild %s uploaded an image", user_id, guild_id) | ||
|
||
return f"https://cdn.suggestions.bot/{key}" |