-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46bc5b3
commit ad3ab39
Showing
7 changed files
with
123 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from fastapi import HTTPException, status | ||
|
||
server_error = HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail="Internal server error", | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from fastapi import HTTPException, status | ||
|
||
quota_exception = HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="Quota exausted", | ||
) | ||
no_access_exception = HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="Not authorized to access file", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,75 @@ | ||
from app.auth.models import User | ||
from .constants import FILE_PATH | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy import select | ||
from sqlalchemy import select, update | ||
import uuid | ||
from datetime import datetime, timedelta | ||
from fastapi import HTTPException, status, UploadFile | ||
from fastapi import UploadFile, Depends | ||
from .schemas import MetadataFileResponse | ||
from pathlib import Path | ||
from .models import File | ||
|
||
quota_exception = HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="Quota exausted", | ||
) | ||
no_access_exception = HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="Not authorized to access file", | ||
) | ||
from typing import Annotated | ||
from app.auth.dependencies import get_current_user | ||
from .exceptions import quota_exception, no_access_exception | ||
|
||
|
||
async def upload_file_unencrypted( | ||
current_user: User, session: AsyncSession, file: UploadFile | ||
session: AsyncSession, | ||
file: UploadFile, | ||
current_user: Annotated[User, Depends(get_current_user)], | ||
) -> None: | ||
if current_user.quota == 0: | ||
if not current_user.has_remaining_quota(): | ||
raise quota_exception | ||
file_path = str(uuid.uuid4()) + file.filename | ||
path = FILE_PATH + file_path | ||
try: | ||
contents = file.file.read() | ||
|
||
file_path = f"{uuid.uuid4()}{file.filename}" | ||
path = Path(FILE_PATH) / file_path | ||
async with session.begin(): | ||
contents = await file.read() | ||
with Path.open(path, "wb") as f: | ||
f.write(contents) | ||
except Exception as e: | ||
raise e | ||
finally: | ||
file.file.close() | ||
file_db = File( | ||
name=file.filename, | ||
path=file_path, | ||
encrypted=False, | ||
size=file.size, | ||
timestamp=datetime.now(), | ||
expiration=datetime.now() + timedelta(days=14), | ||
user=current_user, | ||
) | ||
|
||
session.add(file_db) | ||
current_user.quota -= 1 | ||
await session.commit() | ||
file_db = File( | ||
name=file.filename, | ||
path=file_path, | ||
encrypted=False, | ||
size=file.size, | ||
timestamp=datetime.now(), | ||
expiration=datetime.now() + timedelta(days=14), | ||
user=current_user, | ||
) | ||
|
||
session.add(file_db) | ||
update_statement = ( | ||
update(User).where(User.id == current_user.id).values(quota=User.quota - 1) | ||
) | ||
await session.execute(update_statement) | ||
|
||
|
||
async def get_all_files_user( | ||
current_user: User, session: AsyncSession | ||
current_user: Annotated[User, Depends(get_current_user)], session: AsyncSession | ||
) -> list[MetadataFileResponse]: | ||
files = await session.execute(select(File).filter(File.user_id == current_user.id)) | ||
|
||
file_responses = [] | ||
for file in files.scalars(): | ||
file_response = MetadataFileResponse( | ||
name=file.name, | ||
path=file.path, | ||
size=file.size, | ||
encrypted=file.encrypted, | ||
return [ | ||
MetadataFileResponse( | ||
name=str(file.name), | ||
path=str(file.path), | ||
size=int(file.size), | ||
encrypted=bool(file.encrypted), | ||
) | ||
file_responses.append(file_response) | ||
|
||
return file_responses | ||
for file in files.scalars() | ||
] | ||
|
||
|
||
async def verify_file(path: str, current_user: User, session: AsyncSession) -> str: | ||
async def verify_file( | ||
path: str, | ||
current_user: Annotated[User, Depends(get_current_user)], | ||
session: AsyncSession, | ||
) -> str: | ||
file = await session.execute(select(File).filter(File.path == path)) | ||
file = file.scalar_one_or_none() | ||
if file == None: | ||
raise | ||
if file.user.id == current_user.id: | ||
return file.name | ||
raise no_access_exception |