diff video_file_store.py @ 20:cf6842952e13

serve api/folderTree
author drewp@bigasterisk.com
date Mon, 17 Apr 2023 00:42:59 -0700
parents 53d99454f394
children ed16fdbb3996
line wrap: on
line diff
--- a/video_file_store.py	Mon Apr 17 00:42:27 2023 -0700
+++ b/video_file_store.py	Mon Apr 17 00:42:59 2023 -0700
@@ -1,10 +1,13 @@
 import asyncio
 import hashlib
 import re
+import os
 from dataclasses import dataclass
 from pathlib import Path
 from typing import Iterable, Iterator, NewType
 
+IGNORE = {'_thumb'}
+
 
 @dataclass
 class VideoFile:
@@ -43,7 +46,7 @@
         if subdir[0] != '/': raise ValueError
         here = self.top / subdir[1:]
         for p in here.iterdir():
-            if p.is_dir() and p.name not in {'_thumb'}:
+            if p.is_dir() and p.name not in IGNORE:
                 yield {
                     'label': p.name,
                     'path': '/' + str(p.relative_to(self.top))
@@ -74,3 +77,19 @@
         for c in chunks:
             data += c
         p.write_bytes(data)
+
+    def folderTree(self):
+        out = {'name': 'TOP'}
+
+        def fill(node: dict, pathToHere: Path):
+            for subName in sorted(os.listdir(pathToHere)):
+                if subName in IGNORE:
+                    continue
+                subDir = pathToHere / subName
+                if subDir.is_dir():
+                    subNode = {'name': subName}
+                    node.setdefault('children', []).append(subNode)
+                    fill(subNode, subDir)
+
+        fill(out, self.top)
+        return out
\ No newline at end of file