backend: complete file uploading
This commit is contained in:
parent
da077519fe
commit
23a00d2c06
3 changed files with 51 additions and 15 deletions
|
@ -1,9 +1,8 @@
|
|||
from typing import Annotated
|
||||
from fastapi import FastAPI, File, UploadFile
|
||||
from fastapi import FastAPI, File, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pathlib import Path
|
||||
|
||||
from vntypes import Novel, Mark
|
||||
from vntypes import *
|
||||
from utils import *
|
||||
from db import VNDB
|
||||
|
||||
|
@ -23,24 +22,49 @@ app.add_middleware(
|
|||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
@app.get("/api/queue")
|
||||
def get_post_queue() -> list[FullNovel]:
|
||||
raise HTTPException(status_code=501, detail="Method is not implemented yet!")
|
||||
|
||||
|
||||
@app.get("/api/posts/{post_id}")
|
||||
def get_post(post_id: int):
|
||||
return "NOT IMPLEMENTED!"
|
||||
raise HTTPException(status_code=501, detail="Method is not implemented yet!")
|
||||
|
||||
@app.post("/api/new_post")
|
||||
def new_post(novel: Novel):
|
||||
|
||||
@app.post("/api/posts/{post_id}")
|
||||
def new_post(novel: FullNovel):
|
||||
print(novel)
|
||||
return "yay!"
|
||||
|
||||
@app.post("/api/new_mark")
|
||||
|
||||
@app.patch("/api/posts/{post_id}")
|
||||
def edit_post(novel: FullNovel):
|
||||
print(novel)
|
||||
return "yay!"
|
||||
|
||||
|
||||
@app.post("/api/mark")
|
||||
def new_mark(mark: Mark):
|
||||
database.insert_mark(mark.type, mark.value)
|
||||
return "yay!"
|
||||
|
||||
@app.post("/api/novel_thumb")
|
||||
async def novel_thumb(thumb: Annotated[bytes, File()], filename: str):
|
||||
return {"file_size": len(thumb)}
|
||||
|
||||
@app.post("/api/marks")
|
||||
def search_marks(part: str):
|
||||
raise HTTPException(status_code=501, detail="Method is not implemented yet!")
|
||||
|
||||
|
||||
@app.post("/api/thumb")
|
||||
async def upload_thumb(thumb: Annotated[bytes, File()], filename: str):
|
||||
return {"file_size": save_image(thumb, "thumbs", filename)}
|
||||
|
||||
|
||||
@app.post("/api/screenshot")
|
||||
async def screenshot(scrshot: Annotated[bytes, File()], filename: str):
|
||||
async def upload_screenshot(scrshot: Annotated[bytes, File()], filename: str):
|
||||
return {"file_size": save_image(scrshot, "screens", filename)}
|
||||
|
||||
|
||||
@app.post("/api/file")
|
||||
async def upload_file(file: Annotated[bytes, File()], filename: str):
|
||||
return {"file_size": save_file(file, "files", filename)}
|
|
@ -24,3 +24,11 @@ def save_image(img: bytes, dir: str, name: str) -> int:
|
|||
with open(Path(path, name+'.jpg'), "wb") as file:
|
||||
file.write(img)
|
||||
return len(img)
|
||||
|
||||
|
||||
def save_file(file_b: bytes, dir: str, name: str) -> int:
|
||||
path = asset(dir)
|
||||
path.mkdir(exist_ok=True)
|
||||
with open(Path(path, name+'.jpg'), "wb") as file:
|
||||
file.write(file_b)
|
||||
return len(file_b)
|
|
@ -7,6 +7,10 @@ class Mark(BaseModel):
|
|||
type: Literal["tag", "badge", "genre"]
|
||||
value: str
|
||||
|
||||
class NovelFile(BaseModel):
|
||||
filename: str
|
||||
platform: Literal["android", "ios", "windows", "linux", "macos"]
|
||||
|
||||
class Novel(BaseModel):
|
||||
title: str
|
||||
description: str
|
||||
|
@ -21,9 +25,9 @@ class Novel(BaseModel):
|
|||
tg_post: str | None = None #url::Url
|
||||
post_at: datetime | None = None
|
||||
|
||||
class FullNovel:
|
||||
class FullNovel(BaseModel):
|
||||
data: Novel
|
||||
|
||||
upload_queue: list[str]
|
||||
files: list[str]
|
||||
#upload_queue: list[str]
|
||||
files: list[NovelFile]
|
||||
screenshots: list[str]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue