view video_file_store.py @ 37:7cacfae58430

thumbnails rewrite - store in db; don't use YT-provided pics for now
author drewp@bigasterisk.com
date Tue, 03 Dec 2024 19:28:11 -0800
parents ed16fdbb3996
children 0aea9e55899b
line wrap: on
line source

import asyncio
import hashlib
import logging
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Iterator

import pymongo.collection

log = logging.getLogger('vfs')


@dataclass
class VideoFile:
    diskPath: Path
    webRelPath: str
    webDataPath: str
    label: str
    # perms, playlists, req by/when


@dataclass
class VideoFileStore:
    fs: pymongo.collection.Collection

    def findInDir(self, subdir: str) -> Iterable[VideoFile]:
        webRelParent = '.' if subdir == '/' else subdir
        for doc in self.fs.find({
                'type': 'file',
                'webRelParent': webRelParent
        }, sort=[('label', 1)]):
            yield VideoFile(Path(doc['diskPath']), doc['webRelPath'],
                            doc['webDataPath'], doc['label'])

    def findSubdirs(self, subdir: str) -> Iterable:
        for doc in self.fs.find({
                'type':
                'dir',
                'webRelParent':
                '.' if subdir == '/' else subdir
        }, sort=[('label', 1)]):
            yield {
                'label': doc['label'],
                'path': doc['webRelPath'],
            }

    async def save(self, name: str, chunks: Iterator[bytes]):
        raise
        p = self.top / name
        if p.exists():
            raise ValueError(f'{p} exists')
        data = b''
        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