mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 12:20:38 +04:00
Add file upload
This commit is contained in:
@@ -1 +1,3 @@
|
||||
from .games import router as games_router
|
||||
from .games import router as games_router
|
||||
from .files import router as files_router
|
||||
from .startup import router as startup_router
|
||||
20
routes/files.py
Normal file
20
routes/files.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile
|
||||
|
||||
from database import *
|
||||
from file_handler import *
|
||||
|
||||
router = APIRouter(prefix="/files", tags=["Files"])
|
||||
|
||||
@router.post("/torrent", response_model=str)
|
||||
async def upload_torrent(torrent: UploadFile):
|
||||
try: return await save_torrent_file(torrent)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
raise HTTPException(500)
|
||||
|
||||
@router.post("/cover", response_model=str)
|
||||
async def upload_cover(cover: UploadFile):
|
||||
try: return await save_image(cover, "cover")
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
raise HTTPException(500)
|
||||
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile
|
||||
|
||||
from database import *
|
||||
from file_handler import *
|
||||
|
||||
router = APIRouter(prefix="/games", tags=["Games"])
|
||||
|
||||
@@ -14,7 +15,13 @@ async def get_game(game_id: int, db: AsyncSession = Depends(get_session)):
|
||||
return await crud.get_game(db, game_id)
|
||||
|
||||
@router.post("/", response_model=Game)
|
||||
async def add_game(game: GameCreate, user_id: int, db:AsyncSession = Depends(get_session)):
|
||||
try: return await crud.add_game(db, game, user_id)
|
||||
except Exception as ex: raise HTTPException(500)
|
||||
async def add_game(game: GameCreate,
|
||||
user_id: int,
|
||||
db:AsyncSession = Depends(get_session)):
|
||||
try:
|
||||
torrent_filename = save_torrent_file(torrent, game.title)
|
||||
cover_filename = save_image(cover, game.title, "cover")
|
||||
return await crud.add_game(db, game, user_id)
|
||||
except Exception as ex:
|
||||
raise HTTPException(500)
|
||||
|
||||
16
routes/startup.py
Normal file
16
routes/startup.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from fastapi import APIRouter
|
||||
from pathlib import Path
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.on_event("startup")
|
||||
def startup():
|
||||
need_paths = [
|
||||
Path() / "content" / "images" / "cover" / "full_size",
|
||||
Path() / "content" / "images" / "cover" / "preview",
|
||||
Path() / "content" / "images" / "screenshot" / "full_size",
|
||||
Path() / "content" / "images" / "screenshot" / "preview",
|
||||
Path() / "content" / "torrent"
|
||||
]
|
||||
for path in need_paths:
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
Reference in New Issue
Block a user