-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (124 loc) · 4.73 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import hashlib
import logging
import os
from datetime import timedelta
from misc.gen_salt import generate_random_salt
from fastapi import Depends, FastAPI, HTTPException, Request, status
from fastapi.responses import HTMLResponse
from fastapi import UploadFile
from fastapi.templating import Jinja2Templates
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session
from sql_app import auth
from sql_app.yield_db import get_db
from sql_app.database import Base, engine
from sql_app import schema
from misc.os_stuff import create_working_dir
from misc.zipping_file import zip_file
from sql_app import crud
from sql_app.models import User
BLOCK_SIZE = 65536
app = FastAPI()
templates = Jinja2Templates(directory="templates")
Base.metadata.create_all(bind=engine)
@app.post("/token", response_model=schema.Token)
async def token(
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
):
current_user = auth.auth_user(db, form_data.username, form_data.password)
logging.info("current_user auth")
if not current_user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=auth.ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = auth.create_access_token(
data={"sub": current_user.username}, expire_time=access_token_expires
)
return {"access_token": access_token, "token_type": "bearer"}
@app.get("/", response_class=HTMLResponse)
async def root(req: Request):
return templates.TemplateResponse("home.html", {"request": req})
@app.post("/signup", response_model=schema.UserOut)
async def singup(*, db: Session = Depends(get_db), user_in: schema.UserCreate):
current_user = db.query(User).filter(User.username == user_in.username).first()
if current_user:
raise HTTPException(
status_code=400, detail="The current_user with this name already exists"
)
salt = generate_random_salt()
current_user = crud.create_user(db, current_user=user_in, salt=salt)
current_user = schema.UserOut(**current_user.dict())
return current_user
@app.post("/login", response_model=schema.Token, response_class=HTMLResponse)
async def login(
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
):
# auth current_user
current_user = auth.auth_user(
db, username=form_data.username, password=form_data.password
)
if current_user is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Incorrect email or password",
)
return HTMLResponse(content="<h1>Cool, you logged in</h1>")
@app.post("/uploadfile")
async def uploadfile(
file: UploadFile,
current_user: User = Depends(auth.get_current_active_user),
db: Session = Depends(get_db),
):
file_hash = hashlib.sha256()
cwd = os.getcwd()
path = f"{cwd}/files/{current_user.username}/{file.filename}"
# creating dir files
if not os.path.isdir(f"{cwd}/files"):
os.makedirs(f"{cwd}/files")
# creating dir with username if this one doesn't exist
if not os.path.isdir(f"{cwd}/files/{current_user.username}"):
os.makedirs(f"{cwd}/files/{current_user.username}")
# creating file in dir
with open(f"{cwd}/files/{current_user.username}/{file.filename}", "wb") as f:
f.write(await file.read())
# hashing content
with open(path, "rb") as f:
fb = f.read(BLOCK_SIZE)
while len(fb) > 0:
file_hash.update(fb)
fb = f.read(BLOCK_SIZE)
hash_of_file = file_hash.hexdigest()
end_path = cwd + f"/store/{hash_of_file[:2]}"
if not os.path.isdir(end_path) and not os.path.exists(
end_path + f"/{hash_of_file}"
):
os.makedirs(end_path)
with open(f"{end_path}/{hash_of_file}", "w"):
pass
# zipping file
zip_file(
file=UploadFile,
name=file.filename,
user=current_user.username,
cwd=os.getcwd(),
)
file_scheme = schema.File(
name=file.filename, owner=current_user.username, path=path
)
# add file to db
crud.add_file(db, file_scheme)
@app.delete("/delete")
async def delete_file(
name_of_file: str,
db: Session = Depends(get_db),
):
get_path = crud.get_info_about_file(db, name_of_file)
file = schema.File(name=get_path.name, path=get_path.path, owner=get_path.owner)
crud.delete_file(db, file)
os.remove(get_path.path)
@app.get("/users/me/", response_model=schema.UserInDb)
async def read_users_me(current_user: User = Depends(auth.get_current_active_user)):
return schema.UserInDb(username=str(current_user.username))