comparison video.py @ 44:239a83d46d48

make the server return urls with the (new) correct slashes
author drewp@bigasterisk.com
date Fri, 06 Dec 2024 01:01:05 -0800
parents b5b29f6ef5cb
children 882d0bb0f801
comparison
equal deleted inserted replaced
43:a7b644dc1b4b 44:239a83d46d48
16 from video_file_store import VideoFileStore 16 from video_file_store import VideoFileStore
17 from video_ingest import VideoIngest 17 from video_ingest import VideoIngest
18 from mongo_required import open_mongo_or_die 18 from mongo_required import open_mongo_or_die
19 import pymongo.database 19 import pymongo.database
20 import thumbnail 20 import thumbnail
21 from urllib.parse import unquote
21 22
22 logging.basicConfig(level=logging.DEBUG) 23 logging.basicConfig(level=logging.DEBUG)
23 log = logging.getLogger() 24 log = logging.getLogger()
24 logging.getLogger('sse_starlette').setLevel(logging.WARNING) 25 logging.getLogger('sse_starlette').setLevel(logging.WARNING)
25 logging.getLogger('pymongo').setLevel(logging.INFO) 26 logging.getLogger('pymongo').setLevel(logging.INFO)
28 def root(req): 29 def root(req):
29 return HTMLResponse("api") 30 return HTMLResponse("api")
30 31
31 32
32 async def videos(req: Request) -> JSONResponse: 33 async def videos(req: Request) -> JSONResponse:
34 # either like /dir1/dir2/ or /dir1/dir2/vid1
33 subdir = req.query_params.get('subdir', '/') 35 subdir = req.query_params.get('subdir', '/')
34 if not subdir.endswith('/'):
35 # raise ValueError(f'not a dir {subdir=}')
36 # ok we'll list the parent dir underneath
37 subdir = '/'.join(subdir.split('/')[:-1]) # todo move to FE
38 else:
39 subdir = subdir[:-1]
40 if subdir == "":
41 subdir = "./"
42 if not (subdir.startswith('/') or subdir == './'):
43 raise ValueError(f'not a dir {subdir=}')
44 subdir = subdir[1:]
45 log.debug(f'videos request corrected to: {subdir=}')
46 36
47 vfInDir = store.findInDir(subdir) 37 subdir = unquote(subdir)
48 return JSONResponse({ 38 webDirRelPath = subdir.rsplit('/', 1)[0] + '/'
49 "videos": [{ 39 autoplayRelPath = subdir if not subdir.endswith('/') else None
50 'webRelPath': vf.webRelPath, 40
51 'webDataPath': vf.webDataPath, 41 log.info(f'loading {webDirRelPath=} {autoplayRelPath=}')
52 'label': vf.label, 42
53 } for vf in vfInDir], 43 assert webDirRelPath.startswith('/')
54 "subdirs": 44 assert webDirRelPath.endswith('/')
55 list(store.findSubdirs(subdir)), 45
56 }) 46 resp: dict[str, object] = {
47 'webDirRelPath': webDirRelPath,
48 'dirLabel': webDirRelPath.strip('/').split('/')[-1], #todo
49 }
50
51 d = webDirRelPath[1:-1] or '.'
52 log.info(f'loading {d=}')
53 vfInDir = list(store.findInDir(d))
54 resp["videos"] = [{
55 'webRelPath': '/' + vf.webRelPath,
56 'webDataPath': '/' + vf.webDataPath,
57 'label': vf.label,
58 } for vf in vfInDir]
59
60 if autoplayRelPath:
61 for vf in vfInDir:
62 if '/' + vf.webRelPath == autoplayRelPath:
63 resp['autoplay'] = {
64 'webRelPath': '/' + vf.webRelPath,
65 'webDataPath': '/' + vf.webDataPath,
66 'label': vf.label
67 }
68 break
69 else:
70 raise ValueError(f'{autoplayRelPath=} not in dir')
71
72 log.info(f'{subdir=}')
73 resp['subdirs'] = []
74 for s in store.findSubdirs(subdir.strip('/') or '/'):
75 resp['subdirs'].append({
76 'label': s['label'],
77 'path': '/' + s['path'] + '/',
78 })
79
80 return JSONResponse(resp)
57 81
58 82
59 def folderTree(req: Request) -> JSONResponse: 83 def folderTree(req: Request) -> JSONResponse:
60 return JSONResponse(store.folderTree()) 84 return JSONResponse(store.folderTree())
61 85
89 return doc['diskPath'] 113 return doc['diskPath']
90 114
91 115
92 async def getThumbnail(db: pymongo.database.Database, 116 async def getThumbnail(db: pymongo.database.Database,
93 req: Request) -> Response: 117 req: Request) -> Response:
94 webRelPath = req.query_params['webRelPath'] 118 webRelPath = req.query_params['webRelPath'].lstrip('/')
95 fs = db.get_collection('fs') 119 fs = db.get_collection('fs')
96 diskPath = getDiskPath(fs, webRelPath) 120 diskPath = getDiskPath(fs, webRelPath)
97 th = db.get_collection('thumb') 121 th = db.get_collection('thumb')
98 async with asyncio.timeout(10): 122 async with asyncio.timeout(10):
99 data = await thumbnail.getThumbnailData(th, diskPath) 123 data = await thumbnail.getThumbnailData(th, diskPath)