134 lines
4.6 KiB
Python
134 lines
4.6 KiB
Python
from flask_restx import Namespace, Resource, fields
|
|
from flask_restx import reqparse
|
|
from flask import make_response, abort, request
|
|
from minio.commonconfig import Tags
|
|
from werkzeug.datastructures import FileStorage
|
|
from api.clientGetter import getClientSafely
|
|
import logging
|
|
import random
|
|
|
|
# Exported namespace
|
|
api = Namespace('resource', description='Interact with the raw underlying files. This namespace does NOT speak json, just raw files')
|
|
|
|
uploadFields = {'name' : fields.String(title='Name',
|
|
description='File name of your meme',
|
|
required=True,
|
|
example='Funny.mp4'),
|
|
'uploader' : fields.String(title='Uploader',
|
|
description='Name of the user who uploaded the meme',
|
|
required=True),
|
|
'nsfw': fields.Boolean(title='NSFW',
|
|
description='Is this NSFW/Spoilable?',
|
|
default=False),
|
|
'file': fields.String(title='File',
|
|
description='File as Base64'),
|
|
}
|
|
|
|
uploadForm = api.parser()
|
|
uploadForm.add_argument('file',
|
|
location='files',
|
|
type=FileStorage,
|
|
required=True)
|
|
uploadForm.add_argument('name',
|
|
location='headers',
|
|
type=str,
|
|
required=True)
|
|
uploadForm.add_argument('uploader',
|
|
location='headers',
|
|
type=str,
|
|
required=True)
|
|
uploadForm.add_argument('nsfw',
|
|
location='headers',
|
|
type=bool,
|
|
required=True)
|
|
|
|
|
|
@api.route('/exact/<string:file_name>')
|
|
@api.route('/<string:file_name>', doc={
|
|
"description" : "Alias for /exact/{query}"
|
|
})
|
|
@api.doc(description="Interact with exact raw files.")
|
|
class getExactFile(Resource):
|
|
@api.doc('get')
|
|
@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():
|
|
return make_response(client.getMeme(file_name))
|
|
else:
|
|
abort(400, "Requested file '" + file_name + "' not found")
|
|
|
|
@api.route('/')
|
|
class addFile(Resource):
|
|
@api.response(200, 'Sucess')
|
|
@api.response(500, 'S3 Error')
|
|
@api.response(400, 'Bad request')
|
|
@api.expect(uploadForm)
|
|
def post(self):
|
|
client = getClientSafely()
|
|
if client is None:
|
|
abort(500, "S3 failed to start")
|
|
|
|
args = uploadForm.parse_args()
|
|
file = args['file']
|
|
fileName = args['name']
|
|
uploader = args['uploader']
|
|
nsfw = args['nsfw']
|
|
|
|
tags = Tags.new_object_tags()
|
|
tags["uploader"] = uploader
|
|
tags["nsfw"] = str(nsfw)
|
|
|
|
if client.addMeme(fileContents=file,
|
|
name=fileName,
|
|
tags=tags):
|
|
return {"message" : "success", "sucess" : True}
|
|
else:
|
|
return {"message" : "failure", "success" : False}, 500
|
|
|
|
|
|
|
|
@api.route('/random')
|
|
@api.doc(description="Returns a random meme")
|
|
class getRandomFile(Resource):
|
|
@api.doc('get')
|
|
@api.response(200, 'Sucess')
|
|
@api.response(500, 'S3 Error')
|
|
def get(self):
|
|
client = getClientSafely()
|
|
if client is None:
|
|
abort(500, "S3 failed to start")
|
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
|
return make_response(client.getMeme(choice))
|
|
|
|
@api.route('/psuedorandom')
|
|
@api.doc(description="Returns a psuedorandom meme. Will not return the same meme for a set number of requests")
|
|
class getRandomFile(Resource):
|
|
cache = []
|
|
maxSize = 100
|
|
@api.doc('get')
|
|
@api.response(200, 'Sucess')
|
|
@api.response(500, 'S3 Error')
|
|
def get(self):
|
|
client = getClientSafely()
|
|
if client is None:
|
|
abort(500, "S3 failed to start")
|
|
|
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
|
while choice in self.cache:
|
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
|
|
|
self.cache.append(choice)
|
|
|
|
if len(self.cache) > self.maxSize:
|
|
self.cache.pop()
|
|
|
|
logging.debug("Contents of cache : " + str(self.cache))
|
|
|
|
return make_response(client.getMeme(choice))
|
|
|