forked from robinovsky/preview-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (48 loc) · 1.87 KB
/
app.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
import os
import hashlib
import uvicorn
from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette import status
from starlette.responses import (
FileResponse,
JSONResponse,
PlainTextResponse,
)
from starlette.datastructures import UploadFile
from preview_generator.manager import PreviewManager
UPLOAD_DIR = "/tmp/files/"
CACHE_PATH = "/tmp/cache/"
app = Starlette()
app.mount("/cache", StaticFiles(directory=CACHE_PATH), name="cache")
manager = PreviewManager(CACHE_PATH, create_folder=True)
def error_response(error_message, status=None):
return JSONResponse({"error": error_message}, status_code=status)
async def _store_uploaded_file(file) -> str:
contents = await file.read()
h = hashlib.md5(contents).hexdigest()
upload_dest = os.path.join(UPLOAD_DIR, ".".join([h, file.filename]))
with open(upload_dest, "wb+") as f:
f.write(contents)
return upload_dest
@app.route("/")
async def health_endpoint(request):
return PlainTextResponse("OK")
@app.route("/preview/{width:int}x{height:int}", methods=["POST"])
async def preview_endpoint(request):
width = request.path_params["width"]
height = request.path_params["height"]
form = await request.form()
file = form.get("file", None)
if file is None:
return error_response('"file" is missing', status.HTTP_400_BAD_REQUEST)
if not isinstance(file, UploadFile):
return error_response('"file" must be a file', status.HTTP_400_BAD_REQUEST)
file_path = await _store_uploaded_file(file)
try:
image = manager.get_jpeg_preview(file_path, width=width, height=height)
except Exception as e:
return error_response(str(e), status.HTTP_500_INTERNAL_SERVER_ERROR)
return FileResponse(image)
if __name__ == "__main__":
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)