Browse Source

preview load timeout

master
gsd 7 months ago
parent
commit
586331c013
  1. 16
      backend/config_parser.py
  2. 7
      backend/nvr_core.py
  3. 4
      backend/server.py

16
backend/config_parser.py

@ -294,12 +294,18 @@ class TranscodeTools:
raise NeedNVR raise NeedNVR
if not os.path.exists(raw_file) or os.path.getsize(raw_file) == 0: if not os.path.exists(raw_file) or os.path.getsize(raw_file) == 0:
self.logger.info(f"download new preview {preview_file}") try:
async with aiofiles.open(raw_file, "wb") as raw: async with asyncio.timeout(10):
async for chunk in nvr.stream_file(file, preview_pre_bytes): self.logger.info(f"download new preview {preview_file}")
await raw.write(chunk) async with aiofiles.open(raw_file, "wb") as raw:
async for chunk in nvr.stream_file(file, preview_pre_bytes):
await raw.write(chunk)
except asyncio.TimeoutError as te:
self.logger.info(f"Cancel download file: {te}")
os.remove(raw_file)
finally:
nvr.logout()
nvr.logout()
raw_file_avi = await self.h264toavi(raw_file) raw_file_avi = await self.h264toavi(raw_file)
if self.delete_temporary_files: if self.delete_temporary_files:
self.deleteFile(raw_file) self.deleteFile(raw_file)

7
backend/nvr_core.py

@ -30,8 +30,11 @@ class NVR:
await self.client.login(self.loop) await self.client.login(self.loop)
def logout(self): def logout(self):
self.logger.debug(f"[{self.index}] Logout to {self.client}") try:
self.client.close() self.logger.debug(f"[{self.index}] Logout to {self.client}")
self.client.close()
except:
pass
async def channels(self): async def channels(self):
self.logger.debug(f"[{self.index}] Get channels") self.logger.debug(f"[{self.index}] Get channels")

4
backend/server.py

@ -1,3 +1,5 @@
import asyncio
from fastapi import FastAPI, Response, BackgroundTasks, Header, Request from fastapi import FastAPI, Response, BackgroundTasks, Header, Request
from fastapi.responses import StreamingResponse, FileResponse from fastapi.responses import StreamingResponse, FileResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.security import HTTPBasic, HTTPBasicCredentials
@ -247,7 +249,7 @@ class Server:
headers.update({"Content-Length":str(os.path.getsize(preview))}) headers.update({"Content-Length":str(os.path.getsize(preview))})
headers.update({"Content-Disposition": f'attachment; filename="preview.webp"'}) headers.update({"Content-Disposition": f'attachment; filename="preview.webp"'})
return FileResponse(preview, media_type="application/octet-stream", headers=headers) return FileResponse(preview, media_type="application/octet-stream", headers=headers)
except NeedNVR: except NeedNVR | FileNotFoundError:
background_tasks.add_task(loadPreview, file = file) background_tasks.add_task(loadPreview, file = file)
return FileResponse("./assets/loading.webp") return FileResponse("./assets/loading.webp")

Loading…
Cancel
Save