diff --git a/src/api/get.py b/src/api/get.py index edc86da..60e56f1 100644 --- a/src/api/get.py +++ b/src/api/get.py @@ -42,7 +42,6 @@ uploadForm.add_argument('nsfw', type=bool, required=True) - @api.route('/exact/') @api.route('/', doc={ "description" : "Alias for /exact/{query}" @@ -138,3 +137,24 @@ class getRandomFile(Resource): return response +@api.route('/share/') +@api.doc(description="Returns a share URL from the underlying bucket") +class getShareLink(Resource): + @api.response(200, 'Sucess') + @api.response(500, 'S3 Error') + @api.response(404, 'Requested file not found') + def get(self, file_name): + client = getClientSafely() + if client is None: + abort(500, "S3 failed to start") + + if file_name in client.getCurrentMemeList(): + url = client.getShareForMeme(file_name) + return { + "url": url, + } + else: + abort(400, "Requested file '" + file_name + "' not found") + + + diff --git a/src/s3Client.py b/src/s3Client.py index 798d989..77f9026 100644 --- a/src/s3Client.py +++ b/src/s3Client.py @@ -170,4 +170,25 @@ class Client: raise Exception("Requested meme '" + memeName + "' not found") return None + def getShareForMeme(self, memeName: str) -> str: + """ + Returns the S3 bucket's share link for the meme + """ + + if not isinstance(memeName, str): + raise Exception("paramater memeName is of improper type, expected a str") + + if memeName not in self.getCurrentMemeList(): + raise Exception("Requested meme '" + memeName + "' not found") + + reply = self.client.get_presigned_url( + method="GET", + bucket_name=S3_BUCKET, + object_name=memeName) + + return reply + + + +