annotate video_file_store.py @ 38:0aea9e55899b

hack in path router - dirs kind of work; putting a video in the path doesn't
author drewp@bigasterisk.com
date Wed, 04 Dec 2024 21:50:16 -0800
parents 7cacfae58430
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
1 import asyncio
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
2 import hashlib
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
3 import logging
20
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
4 import os
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
5 from dataclasses import dataclass
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
6 from pathlib import Path
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
7 import re
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
8 from typing import Iterable, Iterator
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
9
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
10 import pymongo.collection
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
11
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
12 log = logging.getLogger('vfs')
20
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
13
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
14
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
15 @dataclass
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
16 class VideoFile:
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
17 diskPath: Path
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
18 webRelPath: str
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
19 webDataPath: str
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
20 label: str
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
21 # perms, playlists, req by/when
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
22
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
23
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
24 def numSort(docs: list):
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
25
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
26 def pad(match):
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
27 return match.group(0).zfill(10)
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
28
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
29 docs.sort(key=lambda doc: re.sub(r"\d+", pad, doc['label']))
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
30
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
31 @dataclass
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
32 class VideoFileStore:
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
33 fs: pymongo.collection.Collection
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
34
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
35 def findInDir(self, subdir: str) -> Iterable[VideoFile]:
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
36 log.warning(f'findInDir: {subdir=}')
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
37 webRelParent = '.' if subdir == '/' else subdir
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
38 docs = list(self.fs.find({
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
39 'type': 'file',
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
40 'webRelParent': webRelParent
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
41 }))
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
42 numSort(docs)
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
43 for doc in docs:
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
44 yield VideoFile(Path(doc['diskPath']), doc['webRelPath'],
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
45 doc['webDataPath'], doc['label'])
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
46
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
47 def findSubdirs(self, subdir: str) -> Iterable:
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
48 docs = list(self.fs.find({
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
49 'type':
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
50 'dir',
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
51 'webRelParent':
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
52 '.' if subdir == '/' else subdir
38
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
53 }))
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
54 numSort(docs)
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
55
0aea9e55899b hack in path router - dirs kind of work; putting a video in the path doesn't
drewp@bigasterisk.com
parents: 37
diff changeset
56 for doc in docs:
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
57 yield {
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
58 'label': doc['label'],
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
59 'path': doc['webRelPath'],
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
60 }
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
61
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
62 async def save(self, name: str, chunks: Iterator[bytes]):
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
63 raise
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
64 p = self.top / name
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
65 if p.exists():
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
66 raise ValueError(f'{p} exists')
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
67 data = b''
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
68 for c in chunks:
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
69 data += c
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents:
diff changeset
70 p.write_bytes(data)
20
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
71
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
72 def folderTree(self):
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
73 out = {'name': 'TOP'}
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
74
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
75 def fill(node: dict, pathToHere: Path):
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
76 for subName in sorted(os.listdir(pathToHere)):
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
77 if subName in IGNORE:
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
78 continue
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
79 subDir = pathToHere / subName
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
80 if subDir.is_dir():
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
81 subNode = {'name': subName}
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
82 node.setdefault('children', []).append(subNode)
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
83 fill(subNode, subDir)
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
84
cf6842952e13 serve api/folderTree
drewp@bigasterisk.com
parents: 15
diff changeset
85 fill(out, self.top)
36
ed16fdbb3996 rewrite WIP. scan fs separately; store in db. thumbs are broken for now
drewp@bigasterisk.com
parents: 20
diff changeset
86 return out