Forráskód Böngészése

mod image and video api

tomoya 1 éve
szülő
commit
fac62936ce

+ 14 - 2
backend/app/app/api/api_v1/endpoints/images.py

@@ -80,6 +80,10 @@ async def wait_finish(dirname, filenames):
     for filename in filenames:
         await publish(filename)
 
+    process = await asyncio.create_subprocess_exec("sshpass", "-p", "choozmo9", 
+                     "ssh", "-P", "5722", "-o", "StrictHostKeyChecking=no", "root@172.104.93.163", "rm", "-r", f"{str(LOCAL_ZIP_STORAGE/dirname)}")
+    await process.wait()
+
 
 async def publish(data):
     for sr_client in sr_clients.values():
@@ -92,6 +96,7 @@ def get_image(
     current_user: models.User = Depends(deps.get_current_active_user),
     stored_file_name: str,
     file_name: str,
+    background_tasks: BackgroundTasks
 ) -> Any:
     """
     Download image
@@ -99,10 +104,17 @@ def get_image(
     return_files = list(BACKEND_ZIP_STORAGE.glob(stored_file_name))
     if return_files:
         return_file = return_files[0]
-        return FileResponse(path=str(return_file), filename=file_name+"_hr"+return_file.suffix)
+        background_tasks.add_task(delete_img, return_file)
+        return FileResponse(path=str(return_file), filename=file_name+"_hr"+return_file.suffix, background=background_tasks)
     else:
         print("non")
-    
+
+async def delete_img(filename:str):
+    await asyncio.sleep(1)
+    path = Path(filename)
+    if path.exists():
+        path.unlink()
+  
 
 @router.delete("/sr")
 def del_image():

+ 1 - 1
backend/app/app/api/api_v1/endpoints/videos.py

@@ -134,7 +134,7 @@ async def wait_finish(video_data:dict):
         user = db.query(models.User).get(video_data['owner_id'])
         video.progress_state = "SUCCESS" 
         if time := task.result:
-            user.available_time -= int(time)
+            user.available_time -= int(time) if int(time) <= user.available_time else 0
             video.length = int(time)
         db.commit()
         db.close()