Add share endpoint
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tyler Perkins 2023-03-19 19:01:10 -04:00
parent d5687ad289
commit 68a03d5229
2 changed files with 42 additions and 1 deletions

View File

@ -42,7 +42,6 @@ uploadForm.add_argument('nsfw',
type=bool, type=bool,
required=True) required=True)
@api.route('/exact/<string:file_name>') @api.route('/exact/<string:file_name>')
@api.route('/<string:file_name>', doc={ @api.route('/<string:file_name>', doc={
"description" : "Alias for /exact/{query}" "description" : "Alias for /exact/{query}"
@ -138,3 +137,24 @@ class getRandomFile(Resource):
return response return response
@api.route('/share/<string:file_name>')
@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")

View File

@ -170,4 +170,25 @@ class Client:
raise Exception("Requested meme '" + memeName + "' not found") raise Exception("Requested meme '" + memeName + "' not found")
return None 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