Skip to content

Commit

Permalink
refactor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alekjarmov committed Dec 17, 2023
1 parent 46bc5b3 commit ad3ab39
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 50 deletions.
50 changes: 50 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions backend/app/auth/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Annotated
from .service import oauth2_scheme, SECRET_KEY, ALGORITHM, get_user_by_username
from app.database import get_async_session
from .schemas import TokenData

from jose import JWTError, jwt
from .models import User
from sqlalchemy.ext.asyncio import AsyncSession
Expand All @@ -19,13 +19,12 @@ async def get_current_user(
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
username: str | None = payload.get("sub")
if username is None:
raise credentials_exception
token_data = TokenData(username=username)
except JWTError as err:
raise credentials_exception from err
user = get_user_by_username(username=token_data.username, session=session)
user = await get_user_by_username(username=username, session=session)
if user is None:
raise credentials_exception
return await user
return user
3 changes: 3 additions & 0 deletions backend/app/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class User(Base):
files = relationship("File", back_populates="user", lazy="selectin")
webhooks = relationship("Webhook", back_populates="user")

def has_remaining_quota(self) -> bool:
return bool(self.quota != 0)


class Role(Base):
__tablename__ = "role"
Expand Down
7 changes: 7 additions & 0 deletions backend/app/exceptions.py
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",
)

10 changes: 10 additions & 0 deletions backend/app/file_transfer/exceptions.py
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",
)
4 changes: 4 additions & 0 deletions backend/app/file_transfer/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ async def get_file(
filename = await verify_file(path, current_user, session)
# Return the file using FileResponse
return FileResponse(FILE_PATH + path, filename=filename)

@router.get("/test")
async def test() -> str:
return "Endpoint works"
90 changes: 45 additions & 45 deletions backend/app/file_transfer/service.py
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

0 comments on commit ad3ab39

Please sign in to comment.