-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (60 loc) · 2.37 KB
/
main.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
import shutil
from typing import Callable
from typing import List
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import HTMLResponse
import pydantic
import db
import dropbox
from db import database_docs, DocInDB
from datetime import date
from typing import Dict
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
today = date.today()
origins = [
"https://ticdrive-front.herokuapp.com",
"https://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8081",
"http://181.131.100.129",
"http://181.131.100.129:8081"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/listfiles")
async def files():
return db.obtener_lista_documentos()
@app.post("/upload-file/")
async def create_upload_file(iddoc:int,fecvencimientodoc:str,
nomdoc:str,
idusuario:int,uploaded_file: UploadFile = File(...)):
#codigo antiguo
# file_location = f"uploadfiles/{uploaded_file.filename}"
# with open(file_location, "wb+") as file_object:
# file_object.write(uploaded_file.file.read())
# fin de codigo antiguo
#nuevo codigo
file_to = '/' + uploaded_file.filename
#conexion con DrpBox
dbx = dropbox.Dropbox('ZLnvyxN_O3oAAAAAAAAAAROUWKg5XPiHwDd4fH-djVUAfupDPYiVJuayBgJJWsxA')
#dbx.files_upload(open(file_from, 'rb').read(), file_to)
dbx.files_upload(uploaded_file.file.read(), file_to)
#fin nuevo codigo
if iddoc in database_docs:
raise HTTPException(status_code=406, detail="El documento ya existe!")
else:
database_docs[iddoc] = DocInDB(**{"iddoc" : iddoc,
"nomdoc" : nomdoc,
"feccarguedoc": today.strftime("%d/%m/%Y"),
"fecvencimientodoc": fecvencimientodoc,
"pathdoc": "/uploadfiles/" + uploaded_file.filename,
"idusuario": idusuario})
return {"info": f"Archivo '{uploaded_file.filename}' ha sido cargado en dropbox y la informacion ha sido grabada con exito"}
# return {"info": f"Archivo '{uploaded_file.filename}' ha sido cargado en '{file_location}' y la informacion ha sido grabada con exito"}