From 15ec5592fc02c5e5f3401d792d07ad1dc31e86ca Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Sat, 17 Dec 2022 22:16:06 -0500 Subject: [PATCH 1/2] Set name of uploaded file in message --- src/memes.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/memes.py b/src/memes.py index 1ec86ec..8c5f45c 100644 --- a/src/memes.py +++ b/src/memes.py @@ -282,6 +282,24 @@ async def uploadMeme(command, message, client): file_names = [] for file in message.attachments: + + # if we want to override the name + uploaded_file_name = file.filename + if len(command) > 1: + uploaded_file_name = "" + for word in command[1:]: + uploaded_file_name += word + "_" + + # remove the last _ from the name + uploaded_file_name = uploaded_file_name.rstrip(uploaded_file_name[-1]) + + # append the file extension + name, ext = os.path.splitext(file.filename) + uploaded_file_name += ext + + print("File name is (" + uploaded_file_name + ")") + + # no files of the same name if file.filename in all_memes: return "File with name '" + file.filename + "' already exists!" @@ -298,6 +316,7 @@ async def uploadMeme(command, message, client): 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) From 4a7b3a4d9c016f6c8603e3ead78cce532f8d5b0f Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Sat, 17 Dec 2022 22:23:16 -0500 Subject: [PATCH 2/2] Refactor name builder logic into its own method --- src/memes.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/memes.py b/src/memes.py index 8c5f45c..e0413b0 100644 --- a/src/memes.py +++ b/src/memes.py @@ -197,6 +197,18 @@ def getRandomMeme(): recentMemes.insertMeme(choice) return choice +# Build an underscored name from a string +def nameBuilder(command): + uploaded_file_name = "" + for word in command: + uploaded_file_name += word + "_" + + # remove the last _ from the name + uploaded_file_name = uploaded_file_name.rstrip(uploaded_file_name[-1]) + + # append the file extension + return uploaded_file_name + ############################################################################### # Calldict methods @@ -282,18 +294,11 @@ async def uploadMeme(command, message, client): file_names = [] for file in message.attachments: - # if we want to override the name uploaded_file_name = file.filename if len(command) > 1: - uploaded_file_name = "" - for word in command[1:]: - uploaded_file_name += word + "_" - - # remove the last _ from the name - uploaded_file_name = uploaded_file_name.rstrip(uploaded_file_name[-1]) - - # append the file extension + uploaded_file_name = nameBuilder(command[1:]) + # add the extension name, ext = os.path.splitext(file.filename) uploaded_file_name += ext