Mercurial > code > home > repos > video
annotate video_file_store.py @ 49:1bd17c2e5517 default tip
video.py must sign video urls for serve-files.js to serve them
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 Dec 2024 17:13:51 -0800 |
parents | 0aea9e55899b |
children |
rev | line source |
---|---|
15 | 1 import asyncio |
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 | 4 import os |
15 | 5 from dataclasses import dataclass |
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 | 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 | 13 |
15 | 14 |
15 @dataclass | |
16 class VideoFile: | |
17 diskPath: Path | |
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 | 20 label: str |
21 # perms, playlists, req by/when | |
22 | |
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 | 31 @dataclass |
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 | 34 |
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 | 46 |
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 | 61 |
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 | 64 p = self.top / name |
65 if p.exists(): | |
66 raise ValueError(f'{p} exists') | |
67 data = b'' | |
68 for c in chunks: | |
69 data += c | |
70 p.write_bytes(data) | |
20 | 71 |
72 def folderTree(self): | |
73 out = {'name': 'TOP'} | |
74 | |
75 def fill(node: dict, pathToHere: Path): | |
76 for subName in sorted(os.listdir(pathToHere)): | |
77 if subName in IGNORE: | |
78 continue | |
79 subDir = pathToHere / subName | |
80 if subDir.is_dir(): | |
81 subNode = {'name': subName} | |
82 node.setdefault('children', []).append(subNode) | |
83 fill(subNode, subDir) | |
84 | |
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 |