comparison video.py @ 39:b5b29f6ef5cb

cleanup and refactor
author drewp@bigasterisk.com
date Thu, 05 Dec 2024 17:01:53 -0800
parents 0aea9e55899b
children 239a83d46d48
comparison
equal deleted inserted replaced
38:0aea9e55899b 39:b5b29f6ef5cb
32 async def videos(req: Request) -> JSONResponse: 32 async def videos(req: Request) -> JSONResponse:
33 subdir = req.query_params.get('subdir', '/') 33 subdir = req.query_params.get('subdir', '/')
34 if not subdir.endswith('/'): 34 if not subdir.endswith('/'):
35 # raise ValueError(f'not a dir {subdir=}') 35 # raise ValueError(f'not a dir {subdir=}')
36 # ok we'll list the parent dir underneath 36 # ok we'll list the parent dir underneath
37 subdir = '/'.join(subdir.split('/')[:-1]) # todo move to FE 37 subdir = '/'.join(subdir.split('/')[:-1]) # todo move to FE
38 else: 38 else:
39 subdir = subdir[:-1] 39 subdir = subdir[:-1]
40 if subdir=="": 40 if subdir == "":
41 subdir = "./" 41 subdir = "./"
42 if not (subdir.startswith('/') or subdir=='./'): 42 if not (subdir.startswith('/') or subdir == './'):
43 raise ValueError(f'not a dir {subdir=}') 43 raise ValueError(f'not a dir {subdir=}')
44 subdir = subdir[1:] 44 subdir = subdir[1:]
45 log.debug(f'videos request corrected to: {subdir=}') 45 log.debug(f'videos request corrected to: {subdir=}')
46 46
47 vfInDir = store.findInDir(subdir) 47 vfInDir = store.findInDir(subdir)
48 return JSONResponse({ 48 return JSONResponse({
49 "videos": [{ 49 "videos": [{
50 'webRelPath': vf.webRelPath, 50 'webRelPath': vf.webRelPath,
51 'webDataPath': vf.webDataPath, 51 'webDataPath': vf.webDataPath,
79 async for ev in svc.events(): 79 async for ev in svc.events():
80 yield json.dumps(ev) 80 yield json.dumps(ev)
81 81
82 return EventSourceResponse(g()) 82 return EventSourceResponse(g())
83 83
84
84 def getDiskPath(fs, webRelPath): 85 def getDiskPath(fs, webRelPath):
85 doc = fs.find_one({'webRelPath': webRelPath}) 86 doc = fs.find_one({'webRelPath': webRelPath})
86 if doc is None: 87 if doc is None:
87 raise ValueError 88 raise ValueError
88 return doc['diskPath'] 89 return doc['diskPath']
89 90
90 async def getThumbnail(db: pymongo.database.Database, req: Request) -> Response: 91
92 async def getThumbnail(db: pymongo.database.Database,
93 req: Request) -> Response:
91 webRelPath = req.query_params['webRelPath'] 94 webRelPath = req.query_params['webRelPath']
92 fs = db.get_collection('fs') 95 fs = db.get_collection('fs')
93 diskPath = getDiskPath(fs, webRelPath) 96 diskPath = getDiskPath(fs, webRelPath)
94 th = db.get_collection('thumb') 97 th = db.get_collection('thumb')
95 async with asyncio.timeout(10): 98 async with asyncio.timeout(10):
96 data = await thumbnail.getThumbnailData(th, diskPath) 99 data = await thumbnail.getThumbnailData(th, diskPath)
97 return Response(content=data, media_type='image/jpeg') 100 return Response(content=data, media_type='image/jpeg')
98
99 101
100 102
101 db = open_mongo_or_die().get_database('video') 103 db = open_mongo_or_die().get_database('video')
102 store = VideoFileStore(db.get_collection('fs')) 104 store = VideoFileStore(db.get_collection('fs'))
103 svc = VideoIngest(store) 105 svc = VideoIngest(store)
127 app.add_route("/metrics", handle_metrics) 129 app.add_route("/metrics", handle_metrics)
128 130
129 app.state.processTask = asyncio.create_task(dl_queue.process()) 131 app.state.processTask = asyncio.create_task(dl_queue.process())
130 return app 132 return app
131 133
134
132 uvicorn.run(main, 135 uvicorn.run(main,
133 host="0.0.0.0", 136 host="0.0.0.0",
134 port=8004, 137 port=8004,
135 log_level=logging.INFO, 138 log_level=logging.INFO,
136 factory=True) 139 factory=True)