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