mirror of
https://github.com/flibusta-apps/fb2converter_server.git
synced 2025-12-06 15:05:37 +01:00
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
import uuid
|
|
import asyncio
|
|
import os
|
|
|
|
from fastapi import FastAPI, APIRouter, File, UploadFile, Form, HTTPException, BackgroundTasks, status
|
|
from fastapi.responses import FileResponse
|
|
from starlette.background import BackgroundTask
|
|
|
|
import aiofiles
|
|
|
|
|
|
router = APIRouter(
|
|
tags=["converter"]
|
|
)
|
|
|
|
|
|
@router.post("/")
|
|
async def convert(background_tasks: BackgroundTasks, file: UploadFile = File({}), format: str = Form({})):
|
|
temp_uuid = uuid.uuid1()
|
|
|
|
temp_filename = str(temp_uuid) + '.fb2'
|
|
converted_temp_filename = str(temp_uuid) + '.' + format
|
|
|
|
async with aiofiles.open(temp_filename, 'wb') as f:
|
|
content = await file.read()
|
|
|
|
if isinstance(content, str):
|
|
content = content.encode()
|
|
|
|
await f.write(content)
|
|
|
|
proc = await asyncio.create_subprocess_exec(
|
|
'./bin/fb2c', 'convert', '--to', format, temp_filename,
|
|
stdout=asyncio.subprocess.PIPE,
|
|
stderr=asyncio.subprocess.PIPE
|
|
)
|
|
|
|
await proc.communicate()
|
|
|
|
background_tasks.add_task(os.remove, temp_filename)
|
|
|
|
if proc.returncode != 0:
|
|
return HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Can't convert!")
|
|
|
|
return FileResponse(
|
|
converted_temp_filename,
|
|
background=BackgroundTask(lambda: os.remove(converted_temp_filename))
|
|
)
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
app.include_router(router)
|