Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import app.core.http_codes as codes
import app.database.controller as dbc
from app.apis import admin_required, item_response, list_response
from app.core.dto import MediaDTO
from app.core.parsers import media_parser_search
from app.database.models import City, Media, MediaType, QuestionType, Role
from flask import request
from flask_jwt_extended import get_jwt_identity, jwt_required
from flask_restx import Resource, reqparse
from flask_uploads import UploadNotAllowed
from PIL import Image
api = MediaDTO.api
image_set = MediaDTO.image_set
schema = MediaDTO.schema
list_schema = MediaDTO.list_schema
def generate_thumbnail(filename):
with Image.open(f"./static/images/{filename}") as im:
im.thumbnail((120, 120))
im.save(f"./static/images/thumbnail_{filename}")
@api.route("/images")
class ImageList(Resource):
@jwt_required
def get(self):
args = media_parser_search.parse_args(strict=True)
items, total = dbc.search.image(**args)
return list_response(list_schema.dump(items), total)
@jwt_required
def post(self):
if "image" not in request.files:
api.abort(codes.BAD_REQUEST, "Missing image in request.files")
try:
filename = image_set.save(request.files["image"])
generate_thumbnail(filename)
print(filename)
item = dbc.add.image(filename, get_jwt_identity())
return item_response(schema.dump(item))
except UploadNotAllowed:
api.abort(codes.BAD_REQUEST, "Could not save the image")
except:
api.abort(codes.INTERNAL_SERVER_ERROR, "Something went wrong when trying to save image")