1
|
|
|
# -*- coding: utf-8 -*- |
2
|
1 |
|
"""Provides Rest API interfaces |
3
|
|
|
""" |
4
|
|
|
|
5
|
1 |
|
import os |
6
|
1 |
|
import base64 |
7
|
1 |
|
import glob |
8
|
1 |
|
import json |
9
|
1 |
|
import datetime |
10
|
1 |
|
import tempfile |
11
|
1 |
|
import responder |
12
|
|
|
|
13
|
1 |
|
from .convert import Image2PDF |
14
|
1 |
|
from .utils.logging import get_logger |
15
|
|
|
|
16
|
1 |
|
api = responder.API() |
17
|
|
|
|
18
|
1 |
|
_logger = get_logger("RestAPI") |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
@api.route("/status") |
22
|
|
|
def status(_, resp): |
23
|
|
|
"""Health Check |
24
|
|
|
""" |
25
|
1 |
|
_logger.debug("health Check") |
26
|
1 |
|
resp.media = {"status": "ok"} |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
@api.route("/data/upload") |
30
|
|
|
async def upload_image_file(req, resp): |
31
|
|
|
"""Endpoint: File uploader |
32
|
|
|
""" |
33
|
1 |
|
data = await req.media() |
34
|
1 |
|
_logger.debug(data) |
35
|
1 |
|
content_type = data["contentType"] |
36
|
1 |
|
extension = convert_content_type_to_extension(content_type) |
37
|
1 |
|
images_b64 = data["images"] |
38
|
1 |
|
tmp_dir = tempfile.mkdtemp() |
39
|
1 |
|
write_image(images_b64, extension, tmp_dir) |
40
|
1 |
|
resp.media = {"upload_id": tmp_dir} |
41
|
|
|
|
42
|
|
|
|
43
|
1 |
|
@api.background.task |
44
|
|
|
def write_image(images_b64, extension, tmp_dir): |
45
|
|
|
"""Images write at tmp_dir |
46
|
|
|
|
47
|
|
|
This API is background task. |
48
|
|
|
|
49
|
|
|
Args: |
50
|
|
|
images_b64: Base64 encoded images list |
51
|
|
|
extension: Image extension |
52
|
|
|
tmp_dir: Temp directory writing images |
53
|
|
|
Returns: |
54
|
|
|
bool: If success return true. |
55
|
|
|
|
56
|
|
|
""" |
57
|
1 |
|
for i, content in enumerate(images_b64): |
58
|
1 |
|
image = base64.b64decode(content) |
59
|
1 |
|
file_name = os.path.join(tmp_dir, str(i) + "." + extension) |
60
|
1 |
|
_logger.debug("file_name: {}".format(file_name)) |
61
|
1 |
|
with open(file_name, "wb") as image_file: |
62
|
1 |
|
image_file.write(image) |
63
|
1 |
|
return True |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
|
@api.route("/convert/pdf") |
67
|
|
|
async def convert_image_to_pdf(req, resp): |
68
|
|
|
"""Endpoint Image converter to PDF |
69
|
|
|
""" |
70
|
1 |
|
data = await req.media() |
71
|
1 |
|
_logger.debug(data) |
72
|
1 |
|
upload_id = data["uploadId"] |
73
|
1 |
|
content_type = data["contentType"] |
74
|
1 |
|
result_meta = os.path.join(upload_id, "result_meta.txt") |
75
|
1 |
|
if os.path.exists(result_meta): |
76
|
1 |
|
os.remove(result_meta) |
77
|
1 |
|
extension = convert_content_type_to_extension(content_type) |
78
|
1 |
|
file_list = sorted( |
79
|
|
|
glob.glob(os.path.join(upload_id, "*." + extension)), reverse=True |
80
|
|
|
) |
81
|
1 |
|
file_base, _ = os.path.splitext(os.path.basename(file_list[0])) |
82
|
1 |
|
digits = len(file_base) |
83
|
1 |
|
_logger.debug(file_list) |
84
|
1 |
|
convert_pdf(digits, extension, upload_id) |
85
|
1 |
|
resp.media = {"upload_id": upload_id} |
86
|
|
|
|
87
|
|
|
|
88
|
1 |
|
@api.background.task |
89
|
|
|
def convert_pdf(digits, extension, upload_id): |
90
|
|
|
"""Convert images to PDF |
91
|
|
|
|
92
|
|
|
This API is background task. |
93
|
|
|
|
94
|
|
|
Args: |
95
|
|
|
digits: file serial number digits |
96
|
|
|
extension: Image extension |
97
|
|
|
upload_id: Request ID |
98
|
|
|
Returns: |
99
|
|
|
bool: If success return true. |
100
|
|
|
|
101
|
|
|
""" |
102
|
1 |
|
converter = Image2PDF(digits=digits, extension=extension, directory_path=upload_id) |
103
|
1 |
|
converter.make_pdf("result.pdf") |
104
|
1 |
|
with open(os.path.join(upload_id, "result_meta.txt"), "w") as result_txt: |
105
|
1 |
|
now = datetime.datetime.now() |
106
|
1 |
|
result = { |
107
|
|
|
"upload_id": upload_id, |
108
|
|
|
"digits": digits, |
109
|
|
|
"extension": extension, |
110
|
|
|
"datetime": now.strftime("%Y/%m/%d %H:%M:%S"), |
111
|
|
|
} |
112
|
1 |
|
result_txt.write(json.dumps(result)) |
113
|
1 |
|
return True |
114
|
|
|
|
115
|
|
|
|
116
|
1 |
|
@api.route("/convert/pdf/download") |
117
|
|
|
async def download_result_pdf(req, resp): |
118
|
|
|
"""Endpoint download result PDF |
119
|
|
|
""" |
120
|
1 |
|
data = await req.media() |
121
|
1 |
|
_logger.debug(data) |
122
|
1 |
|
upload_id = data["uploadId"] |
123
|
1 |
|
result_meta = os.path.join(upload_id, "result_meta.txt") |
124
|
1 |
|
if os.path.exists(result_meta): |
125
|
1 |
|
with open(os.path.join(upload_id, "result.pdf"), "rb") as result_pdf: |
126
|
1 |
|
resp.headers["Content-Type"] = "application/pdf" |
127
|
1 |
|
resp.content = result_pdf.read() |
128
|
|
|
else: |
129
|
1 |
|
resp.status_code = api.status_codes.HTTP_404 |
130
|
|
|
|
131
|
|
|
|
132
|
1 |
|
def convert_content_type_to_extension(content_type): |
133
|
|
|
"""Convert image extension to Content-Type |
134
|
|
|
|
135
|
|
|
Args: |
136
|
|
|
content_type: Content-Type |
137
|
|
|
Returns: |
138
|
|
|
str: extension |
139
|
|
|
|
140
|
|
|
""" |
141
|
1 |
|
if content_type == "image/jpeg": |
142
|
1 |
|
extension = "jpg" |
143
|
1 |
|
elif content_type == "image/png": |
144
|
1 |
|
extension = "png" |
145
|
1 |
|
elif content_type == "image/gif": |
146
|
1 |
|
extension = "gif" |
147
|
|
|
else: |
148
|
1 |
|
extension = False |
149
|
|
|
|
150
|
|
|
return extension |
151
|
|
|
|