-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidator-server.py
47 lines (40 loc) · 1.46 KB
/
validator-server.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
import pathlib
import tempfile
import traceback
from fastapi import FastAPI, File, UploadFile, Response, status
import shutil
from curation_validator import validate_curation
from logger import getLogger
l = getLogger("api")
app = FastAPI()
@app.post("/upload/")
async def create_upload_file(response: Response, file: UploadFile = File(...)):
l.debug(f"received file '{file.filename}'")
base_path = tempfile.mkdtemp(prefix="curation_validator_")
new_filepath = base_path + "/file" + pathlib.Path(file.filename).suffix
with open(new_filepath, "wb") as dest:
l.debug(f"copying file '{file.filename}' into '{new_filepath}'.")
shutil.copyfileobj(file.file, dest)
try:
curation_errors, curation_warnings, is_extreme, curation_type, meta, image_dict = validate_curation(new_filepath)
except Exception as e:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return {
"exception": "".join(
traceback.format_exception(
etype=type(e), value=e, tb=e.__traceback__
)
)
}
l.debug(f"removing '{new_filepath}'.")
shutil.rmtree(base_path)
return {
"filename": file.filename,
"path": new_filepath,
"curation_errors": curation_errors,
"curation_warnings": curation_warnings,
"is_extreme": is_extreme,
"curation_type": curation_type,
"meta": meta,
"images": image_dict
}