# HG changeset patch # User drewp@bigasterisk.com # Date 1733475665 28800 # Node ID 239a83d46d48daeebeaaab5964b10314f6c3d4de # Parent a7b644dc1b4b2ce22460c6224799bb391e6cc3d3 make the server return urls with the (new) correct slashes diff -r a7b644dc1b4b -r 239a83d46d48 video.py --- a/video.py Fri Dec 06 00:58:46 2024 -0800 +++ b/video.py Fri Dec 06 01:01:05 2024 -0800 @@ -18,6 +18,7 @@ from mongo_required import open_mongo_or_die import pymongo.database import thumbnail +from urllib.parse import unquote logging.basicConfig(level=logging.DEBUG) log = logging.getLogger() @@ -30,30 +31,53 @@ async def videos(req: Request) -> JSONResponse: + # either like /dir1/dir2/ or /dir1/dir2/vid1 subdir = req.query_params.get('subdir', '/') - if not subdir.endswith('/'): - # raise ValueError(f'not a dir {subdir=}') - # ok we'll list the parent dir underneath - subdir = '/'.join(subdir.split('/')[:-1]) # todo move to FE - else: - subdir = subdir[:-1] - if subdir == "": - subdir = "./" - if not (subdir.startswith('/') or subdir == './'): - raise ValueError(f'not a dir {subdir=}') - subdir = subdir[1:] - log.debug(f'videos request corrected to: {subdir=}') + + subdir = unquote(subdir) + webDirRelPath = subdir.rsplit('/', 1)[0] + '/' + autoplayRelPath = subdir if not subdir.endswith('/') else None + + log.info(f'loading {webDirRelPath=} {autoplayRelPath=}') + + assert webDirRelPath.startswith('/') + assert webDirRelPath.endswith('/') + + resp: dict[str, object] = { + 'webDirRelPath': webDirRelPath, + 'dirLabel': webDirRelPath.strip('/').split('/')[-1], #todo + } - vfInDir = store.findInDir(subdir) - return JSONResponse({ - "videos": [{ - 'webRelPath': vf.webRelPath, - 'webDataPath': vf.webDataPath, - 'label': vf.label, - } for vf in vfInDir], - "subdirs": - list(store.findSubdirs(subdir)), - }) + d = webDirRelPath[1:-1] or '.' + log.info(f'loading {d=}') + vfInDir = list(store.findInDir(d)) + resp["videos"] = [{ + 'webRelPath': '/' + vf.webRelPath, + 'webDataPath': '/' + vf.webDataPath, + 'label': vf.label, + } for vf in vfInDir] + + if autoplayRelPath: + for vf in vfInDir: + if '/' + vf.webRelPath == autoplayRelPath: + resp['autoplay'] = { + 'webRelPath': '/' + vf.webRelPath, + 'webDataPath': '/' + vf.webDataPath, + 'label': vf.label + } + break + else: + raise ValueError(f'{autoplayRelPath=} not in dir') + + log.info(f'{subdir=}') + resp['subdirs'] = [] + for s in store.findSubdirs(subdir.strip('/') or '/'): + resp['subdirs'].append({ + 'label': s['label'], + 'path': '/' + s['path'] + '/', + }) + + return JSONResponse(resp) def folderTree(req: Request) -> JSONResponse: @@ -91,7 +115,7 @@ async def getThumbnail(db: pymongo.database.Database, req: Request) -> Response: - webRelPath = req.query_params['webRelPath'] + webRelPath = req.query_params['webRelPath'].lstrip('/') fs = db.get_collection('fs') diskPath = getDiskPath(fs, webRelPath) th = db.get_collection('thumb')