diff video_file_store.py @ 36:ed16fdbb3996

rewrite WIP. scan fs separately; store in db. thumbs are broken for now
author drewp@bigasterisk.com
date Tue, 03 Dec 2024 00:08:22 -0800
parents cf6842952e13
children 7cacfae58430
line wrap: on
line diff
--- a/video_file_store.py	Mon Dec 02 23:27:59 2024 -0800
+++ b/video_file_store.py	Tue Dec 03 00:08:22 2024 -0800
@@ -1,58 +1,52 @@
 import asyncio
 import hashlib
-import re
+import logging
 import os
 from dataclasses import dataclass
 from pathlib import Path
-from typing import Iterable, Iterator, NewType
+from typing import Iterable, Iterator
 
-IGNORE = {'_thumb'}
+import pymongo.collection
+
+log = logging.getLogger('vfs')
 
 
 @dataclass
 class VideoFile:
     diskPath: Path
     webRelPath: str
+    webDataPath: str
     label: str
     # perms, playlists, req by/when
 
 
-def vf(p: Path, label: str):
-    return VideoFile(p, './files/' + str(p.relative_to('/data')), label)
-
-
-def thumbWebPath(rel: str) -> str:
-    return './files/' + rel
-
-
 @dataclass
 class VideoFileStore:
-    top: Path
+    fs: pymongo.collection.Collection
 
     def findInDir(self, subdir: str) -> Iterable[VideoFile]:
-        if subdir[0] != '/': raise ValueError
-        here = self.top / subdir[1:]
-        manifests = list(here.glob('*.mpd'))
-        if manifests:
-            p = manifests[0]
-            label = p.parent.name
-            yield vf(p, label)
-            return
-        for p in sorted(list(here.glob('*.mp4')) + list(here.glob('*.webm'))):
-            label = re.sub(r' \[[^\]]+\]\.\w+', '', p.name)
-            yield vf(p, label)
+        webRelParent = '.' if subdir == '/' else subdir
+        for doc in self.fs.find({
+                'type': 'file',
+                'webRelParent': webRelParent
+        }):
+            yield VideoFile(Path(doc['diskPath']), doc['webRelPath'],
+                            doc['webDataPath'], doc['label'])
 
     def findSubdirs(self, subdir: str) -> Iterable:
-        if subdir[0] != '/': raise ValueError
-        here = self.top / subdir[1:]
-        for p in here.iterdir():
-            if p.is_dir() and p.name not in IGNORE:
-                yield {
-                    'label': p.name,
-                    'path': '/' + str(p.relative_to(self.top))
-                }
+        for doc in self.fs.find({
+                'type':
+                'dir',
+                'webRelParent':
+                '.' if subdir == '/' else subdir
+        }):
+            yield {
+                'label': doc['label'],
+                'path': doc['webRelPath'],
+            }
 
     def thumbPath(self, vf: VideoFile) -> str:
+        return '_thumb/' + vf.webRelPath
         sha256 = hashlib.sha256()
         with open(vf.diskPath, 'rb') as f:
             firstMb = f.read(1 << 20)
@@ -61,6 +55,7 @@
         return f'_thumb/{cksum}.jpg'
 
     async def getOrCreateThumb(self, vf: VideoFile) -> str:
+        raise
         p = self.top / self.thumbPath(vf)
         if not p.exists():
             sp = asyncio.create_subprocess_exec('ffmpegthumbnailer',
@@ -70,6 +65,7 @@
         return thumbWebPath(str(p.relative_to(self.top)))
 
     async def save(self, name: str, chunks: Iterator[bytes]):
+        raise
         p = self.top / name
         if p.exists():
             raise ValueError(f'{p} exists')
@@ -92,4 +88,4 @@
                     fill(subNode, subDir)
 
         fill(out, self.top)
-        return out
\ No newline at end of file
+        return out