changeset 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 a7b644dc1b4b
children df51269bcef4
files video.py
diffstat 1 files changed, 47 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- 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')