|
@@ -3,8 +3,10 @@ import subprocess
|
|
from fastapi import UploadFile, File, Form
|
|
from fastapi import UploadFile, File, Form
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.responses import FileResponse
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
+from fastapi import WebSocket, BackgroundTasks
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy.orm import Session
|
|
-
|
|
|
|
|
|
+import asyncio
|
|
|
|
+import shutil
|
|
import app.crud as crud
|
|
import app.crud as crud
|
|
import app.models as models
|
|
import app.models as models
|
|
import app.schemas as schemas
|
|
import app.schemas as schemas
|
|
@@ -24,19 +26,45 @@ LOCAL_ZIP_STORAGE = Path("/").joinpath(settings.LOCAL_ZIP_STORAGE)
|
|
router = APIRouter()
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
@router.post("/sr")
|
|
@router.post("/sr")
|
|
async def supser_resolution(
|
|
async def supser_resolution(
|
|
*,
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
db: Session = Depends(deps.get_db),
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
upload_files: List[UploadFile]=File(description="Multiple files as UploadFile"),
|
|
upload_files: List[UploadFile]=File(description="Multiple files as UploadFile"),
|
|
|
|
+ background_tasks: BackgroundTasks,
|
|
) -> Any:
|
|
) -> Any:
|
|
"""
|
|
"""
|
|
Super Resolution.
|
|
Super Resolution.
|
|
"""
|
|
"""
|
|
- filenames = [random_name(20) for file in upload_files]
|
|
|
|
- return {"filenames": filenames}
|
|
|
|
|
|
+ filenames = [random_name(20)+Path(file.filename).suffix for file in upload_files]
|
|
|
|
+ stemnames = [Path(filename).stem for filename in filenames]
|
|
|
|
+ new_dir = random_name(10)
|
|
|
|
+ new_dir_path = Path(BACKEND_ZIP_STORAGE).joinpath(new_dir)
|
|
|
|
+ new_dir_path.mkdir()
|
|
|
|
+ for i in range(len(upload_files)):
|
|
|
|
+ try:
|
|
|
|
+ with open(str(Path(new_dir_path).joinpath(filenames[i])), 'wb') as f:
|
|
|
|
+ while contents := upload_files[i].file.read(1024 * 1024):
|
|
|
|
+ f.write(contents)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ print(e, type(e))
|
|
|
|
+ return {"error": str(e)}
|
|
|
|
+ finally:
|
|
|
|
+ upload_files[i].file.close()
|
|
|
|
+ source = [f"{str(BACKEND_ZIP_STORAGE/filename)}" for filename in filenames]
|
|
|
|
+ r = subprocess.run(["sshpass", "-p", "choozmo9",
|
|
|
|
+ "scp", "-P", "5722", "-o", "StrictHostKeyChecking=no", "-r", f"{str(new_dir_path)}", f"root@172.104.93.163:{str(LOCAL_ZIP_STORAGE)}"])
|
|
|
|
+
|
|
|
|
+ background_tasks.add_task(wait_finish, new_dir_path, stemnames)
|
|
|
|
+
|
|
|
|
+ print(filenames)
|
|
|
|
+ return {"filenames": stemnames}
|
|
|
|
+
|
|
|
|
+async def wait_finish(dirname, filenames):
|
|
|
|
+ for filename in filenames:
|
|
|
|
+ await asyncio.sleep(3)
|
|
|
|
+ await publish(filename)
|
|
|
|
|
|
@router.get("/sr")
|
|
@router.get("/sr")
|
|
def get_image(
|
|
def get_image(
|
|
@@ -53,3 +81,27 @@ def get_image(
|
|
response_filename = filename.stem + "_hr.png"
|
|
response_filename = filename.stem + "_hr.png"
|
|
return FileResponse(path="test_medias/superman_resolution.png", media_type='image/png', filename=response_filename)
|
|
return FileResponse(path="test_medias/superman_resolution.png", media_type='image/png', filename=response_filename)
|
|
|
|
|
|
|
|
+sr_clients = {}
|
|
|
|
+
|
|
|
|
+@router.websocket("/sr")
|
|
|
|
+async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
+ await websocket.accept()
|
|
|
|
+ key = websocket.headers.get('sec-websocket-key')
|
|
|
|
+ sr_clients[key] = websocket
|
|
|
|
+ try:
|
|
|
|
+ while True:
|
|
|
|
+ data = await websocket.receive_text()
|
|
|
|
+ if not data.startswith("subscribe"):
|
|
|
|
+ del sr_clients[key]
|
|
|
|
+ return
|
|
|
|
+ #for client in sr_clients.values():
|
|
|
|
+ # await client.send_text(f"ID: {key} | Message: {data}")
|
|
|
|
+
|
|
|
|
+ except:
|
|
|
|
+ #await websocket.close()
|
|
|
|
+ # 接続が切れた場合、当該クライアントを削除する
|
|
|
|
+ del sr_clients[key]
|
|
|
|
+
|
|
|
|
+async def publish(data):
|
|
|
|
+ for sr_client in sr_clients.values():
|
|
|
|
+ await sr_client.send_text(f"{data}")
|