videos.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from typing import Any, List, Optional
  2. import subprocess
  3. from fastapi import UploadFile, File, Form
  4. from fastapi.responses import FileResponse
  5. from fastapi import APIRouter, Depends, HTTPException
  6. from sqlalchemy.orm import Session
  7. import app.crud as crud
  8. import app.models as models
  9. import app.schemas as schemas
  10. from app.api import deps
  11. from app.core.celery_app import celery_app
  12. from app.core.config import settings
  13. from pathlib import Path
  14. from app.core.celery_app import celery_app
  15. BACKEND_ZIP_STORAGE = Path("/app").joinpath(settings.BACKEND_ZIP_STORAGE)
  16. LOCAL_ZIP_STORAGE = Path("/").joinpath(settings.LOCAL_ZIP_STORAGE)
  17. router = APIRouter()
  18. @router.get("/", response_model=List[schemas.Video])
  19. def get_video_list(
  20. db: Session = Depends(deps.get_db),
  21. skip: int = 0,
  22. limit: int = 100,
  23. current_user: models.User = Depends(deps.get_current_active_user),
  24. ) -> Any:
  25. """
  26. Retrieve items.
  27. """
  28. if crud.user.is_superuser(current_user):
  29. videos = crud.video.get_multi(db, skip=skip, limit=limit)
  30. else:
  31. videos = crud.video.get_multi_by_owner(
  32. db=db, owner_id=current_user.id, skip=skip, limit=limit
  33. )
  34. return videos
  35. @router.post("/", response_model=schemas.Video)
  36. def upload_plot(
  37. *,
  38. db: Session = Depends(deps.get_db),
  39. title: str=Form(...),
  40. anchor_id: int=Form(...),
  41. lang_id: int=Form(...),
  42. upload_file: UploadFile=File(),
  43. current_user: models.User = Depends(deps.get_current_active_user),
  44. ) -> Any:
  45. """
  46. Create new video.
  47. """
  48. print(title)
  49. print(upload_file.filename)
  50. file_name = crud.video.generate_file_name(db=db, n=20)
  51. video_create = schemas.VideoCreate(title=title, progress_state="waiting", stored_file_name=file_name)
  52. video = crud.video.create_with_owner(db=db, obj_in=video_create, owner_id=current_user.id)
  53. try:
  54. with open(str(Path(BACKEND_ZIP_STORAGE).joinpath(video.stored_file_name+".zip")), 'wb') as f:
  55. while contents := upload_file.file.read(1024 * 1024):
  56. f.write(contents)
  57. except Exception as e:
  58. print(e, type(e))
  59. return {"error": str(e)}
  60. finally:
  61. upload_file.file.close()
  62. zip_filename = video.stored_file_name+".zip"
  63. print(str(BACKEND_ZIP_STORAGE/zip_filename))
  64. r = subprocess.run(["sshpass", "-p", "choozmo9",
  65. "scp", "-P", "5722", "-o", "StrictHostKeyChecking=no", f"{str(BACKEND_ZIP_STORAGE/zip_filename)}", f"root@172.104.93.163:{str(LOCAL_ZIP_STORAGE/zip_filename)}"])
  66. print(r.returncode)
  67. celery_app.send_task("app.worker.make_video", args=[video.id, video.stored_file_name, current_user.id, anchor_id, current_user.membership_status, current_user.available_time])
  68. return video
  69. @router.get("/{id}")
  70. def download_video(
  71. *,
  72. db: Session = Depends(deps.get_db),
  73. id: int,
  74. current_user: models.User = Depends(deps.get_current_active_user),
  75. ) -> Any:
  76. return {"message":"address"}