add: upload_pictures() prototype

rrr-marble 4 years ago
parent 1ba4ac1bd6
commit 7069af7548

@ -1,7 +1,9 @@
from fastapi import FastAPI
from fastapi import FastAPI, File, UploadFile, Depends
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.middleware.cors import CORSMiddleware # CORS
from secrets import compare_digest
from datetime import datetime
from uuid import uuid4
import sqlite3
@ -9,15 +11,16 @@ import sqlite3
# use database residing here
DB_LOCATION = (
"db/photovoter.dblite" # Q: any allowances for this being not OUR database?
"../testbox/photovoter.dblite" # Q: any allowances for this being not OUR database?
)
app = FastAPI()
security = HTTPBasic()
con = sqlite3.connect(DB_LOCATION)
con.row_factory = sqlite3.Row
cur = con.cursor() # NB! single is enough for now, we might require multiple later
origins = [ # CORS
origins = [ # CORS
"*",
]
@ -144,3 +147,29 @@ async def rate_picture(cookie: str, picture_id: int, mark: int):
return JSONResponse(status_code=406)
return {"status": "success"}
@app.post(
"/upload_pictures/",
responses={
401: {"description": "Authentication is required to access this resource"},
415: {"description": "Cannot process uploaded archive"},
},
)
async def upload_pictures(
credentials: HTTPBasicCredentials = Depends(security), file: UploadFile = File(...)
):
"""Интерфейс для загрузки фотографий"""
"""Условно кладём в браузер zip с фотографиями и он их потихоньку ест.
Доступ к этому интерфейсу, наверное, лучше ограничить паролем или как-нибудь ещё.
Пока исходим из предположения, что только я буду загружать фотографии."""
# check authenticity
correct_username = compare_digest(credentials.username, "1")
correct_password = compare_digest(credentials.password, "1")
if not (correct_username and correct_password):
return JSONResponse(status_code=401)
# slurp the zip
# *detach from the interface, if possible
# unpack zip
# feed the pictures to util/import_photos.py
return {"filename": file.filename}

@ -11,7 +11,9 @@ pathspec==0.8.1
pycodestyle==2.7.0
pydantic==1.8.2
pyflakes==2.3.1
python-multipart==0.0.5
regex==2021.4.4
six==1.16.0
starlette==0.14.2
toml==0.10.2
typing-extensions==3.10.0.0

Loading…
Cancel
Save