-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
image_upload.py
100 lines (95 loc) · 3.12 KB
/
image_upload.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests
import json
from flask_restful import Resource, request, current_app
from backend.services.users.authentication_service import token_auth
class SystemImageUploadRestAPI(Resource):
@token_auth.login_required
def post(self):
"""
Uploads an image using the image upload service
---
tags:
- system
produces:
- application/json
parameters:
- in: header
name: Authorization
description: Base64 encoded session token
required: true
type: string
default: Token sessionTokenHere==
- in: body
name: body
required: true
description: JSON object containing image data that will be uploaded
schema:
properties:
data:
type: string
default: base64 encoded image data
mime:
type: string
default: file mime/type
filename:
type: string
default: filename
responses:
200:
description: Image uploaded successfully
400:
description: Input parameter error
403:
description: User is not authorized to upload images
500:
description: A problem occurred
501:
description: Image upload service not defined
"""
if (
current_app.config["IMAGE_UPLOAD_API_URL"] is None
or current_app.config["IMAGE_UPLOAD_API_KEY"] is None
):
return {
"Error": "Image upload service not defined",
"SubCode": "UndefinedImageService",
}, 501
data = request.get_json()
if data.get("filename") is None:
return {
"Error": "Missing filename parameter",
"SubCode": "MissingFilename",
}, 400
if data.get("mime") in [
"image/png",
"image/jpeg",
"image/webp",
"image/gif",
]:
headers = {
"x-api-key": current_app.config["IMAGE_UPLOAD_API_KEY"],
"Content-Type": "application/json",
}
url = "{}?filename={}".format(
current_app.config["IMAGE_UPLOAD_API_URL"], data.get("filename")
)
result = requests.post(
url, headers=headers, data=json.dumps({"image": data})
)
if result.ok:
return result.json(), 201
else:
return result.json(), 400
elif data.get("mime") is None:
return {
"Error": "Missing mime parameter",
"SubCode": "MissingMime",
}, 400
else:
return (
{
"Error": "Mimetype is not allowed. The supported formats are: png, jpeg, webp and gif.",
"SubCode": "UnsupportedFile",
},
400,
)