This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
115 lines (98 loc) · 3.97 KB
/
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from fastapi import FastAPI, HTTPException, Request, File, UploadFile, Depends, Response
from fastapi.responses import FileResponse
from sqlalchemy.orm import Session
from models.db import SessionLocal, engine
from models import db_models
from models import photo
from controllers import file_controller
from controllers import db_controller
import os
import json
# init
db_models.Base.metadata.create_all(bind=engine)
app = FastAPI()
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
# create file in repo
@app.post('/api/v1/photo/new')
async def create_file(file: UploadFile = File(...), db: Session = Depends(get_db)):
try:
if not await file_controller.is_image(file):
raise Exception('is not image')
id = await file_controller.calculate_id(file)
if await db_controller.get_photo(db=db, id=id) != None:
return { "id": id, "path": path }
path = await file_controller.save_upload_file(file, id)
p = photo.PhotoBase()
p.id = id
p.path = path
await db_controller.create_photo(db=db, photo=p)
return { "id": id, "path": path }
except Exception as e:
raise HTTPException(400, detail=str(e))
# get photo info
@app.get('/api/v1/photo/info/{id}')
async def create_file(id: str, db: Session = Depends(get_db)):
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
return p
# change photo annotation
@app.post('/api/v1/photo/anno/{id}')
async def create_file(id: str, anno: photo.PhotoAnnotate, db: Session = Depends(get_db)):
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
if anno.annotation == None and anno.label == None:
raise HTTPException(400, "invalid annotation")
anno.id = id
if anno.annotation == None:
anno.annotation = p.annotation
if anno.label == None:
anno.label = p.label
return await db_controller.annotate_photo(db=db, photo=anno)
# get photo annotation in json format
@app.get('/api/v1/photo/anno/{id}')
async def create_file(id: str, db: Session = Depends(get_db)):
try:
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
if p.annotation == None:
raise HTTPException(404, detail="photo has no annotation")
return json.loads(p.annotation)
except HTTPException as e:
raise HTTPException(status_code=e.status_code, detail=e.detail, headers=e.headers)
except Exception as e:
raise HTTPException(500, detail="unable to parse annotation: {}".format(str(e)))
# get photo annotation in voc format
@app.get('/api/v1/photo/anno_voc/{id}')
async def create_file(id: str, db: Session = Depends(get_db)):
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
raise HTTPException(404, detail="no inplement")
# change photo approve_rate
@app.get('/api/v1/photo/rate/{id}/{rate}')
async def create_file(id: str, rate: int, db: Session = Depends(get_db)):
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
return await db_controller.change_photo_rate(db=db, id=id, approve_rate=rate)
# change photo approve status
@app.get('/api/v1/photo/approve/{id}')
async def create_file(id: str, approve: bool = False, db: Session = Depends(get_db)):
p = await db_controller.get_photo(db=db, id=id)
if p == None:
raise HTTPException(404, detail="no such photo")
return await db_controller.change_photo_approve_status(db=db, id=id, is_approved=approve)
# get photo data
@app.get('/api/v1/photo/data/{path}')
async def create_file(path: str):
if not os.path.isfile(path):
raise HTTPException(404, detail="no such photo")
return FileResponse(path)