Add ability to upload #2

Merged
tyler merged 10 commits from uploading into main 2022-12-18 02:55:41 +00:00
Showing only changes of commit bb9b21c8f5 - Show all commits

View File

@ -93,14 +93,15 @@ def getCurrentMemeList(force=False):
if (now - last_checked_all_memes).seconds > 300 or force: if (now - last_checked_all_memes).seconds > 300 or force:
print("Enough time has elapsed, refreshing meme cache...") print("Enough time has elapsed, refreshing meme cache...")
last_checked_all_memes = now last_checked_all_memes = now
all_memes.clear() all_memes.clear()
memes_to_md5.clear()
myClient = getClient() myClient = getClient()
for obj in myClient.list_objects(S3_BUCKET): for obj in myClient.list_objects(S3_BUCKET):
if not obj.is_dir: if not obj.is_dir:
#print(f'{obj.object_name}') #print(f'{obj.object_name}')
all_memes.append(obj.object_name) all_memes.append(obj.object_name)
if obj.object_name == 'combat_shotgun_my_beloved1.png':
print("Found! " + obj.etag)
memes_to_md5[obj.etag] = obj.object_name memes_to_md5[obj.etag] = obj.object_name
print(f"Got {len(all_memes)} memes") print(f"Got {len(all_memes)} memes")
return all_memes return all_memes
@ -275,7 +276,6 @@ async def uploadMeme(command, message, client):
uploader_tags = Tags.new_object_tags() uploader_tags = Tags.new_object_tags()
uploader_tags["uploader"] = message.author.name uploader_tags["uploader"] = message.author.name
print(uploader_tags)
all_memes = getCurrentMemeList() all_memes = getCurrentMemeList()
client = getClient() client = getClient()
@ -287,20 +287,24 @@ async def uploadMeme(command, message, client):
await file.save("/tmp/" + file.filename) await file.save("/tmp/" + file.filename)
#upload the file to S3 result = client.fput_object(bucket_name=S3_BUCKET,
file_hash = md5(open("/tmp/" + file.filename, 'rb').read()) object_name=file.filename,
print("Uploaded file has md5 hash of " + file_hash.hexdigest()) file_path="/tmp/" + file.filename,
if file_hash not in memes_to_md5: tags=uploader_tags,
client.fput_object(bucket_name=S3_BUCKET, content_type=None)
object_name=file.filename,
file_path="/tmp/" + file.filename, if result.etag in memes_to_md5:
tags=uploader_tags, print("Already have that meme, etag is (" + result.etag + ")")
content_type=None)
else: client.remove_object(bucket_name=S3_BUCKET,
return "Found that meme already! (aka " + memes_to_md5[file_hash] + ")" object_name=file.filename)
return "That meme is already in the cache (it's called " + memes_to_md5[result.etag] + ")"
os.remove("/tmp/" + file.filename) os.remove("/tmp/" + file.filename)
getCurrentMemeList(force=True) getCurrentMemeList(force=True)
return "Thanks got your memes!" return "Thanks got your memes!"