summaryrefslogtreecommitdiff
path: root/py_include/homekit/media/storage.py
blob: dd74ff84730fca7c48738ba01f0016a85e402427 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import re
import shutil
import logging

from typing import Optional, Union, List
from datetime import datetime
from ..util import strgen

logger = logging.getLogger(__name__)


# record file
# -----------

class RecordFile:
    EXTENSION = None

    start_time: Optional[datetime]
    stop_time: Optional[datetime]
    record_id: Optional[int]
    name: str
    file_id: Optional[str]
    remote: bool
    remote_filesize: int
    storage_root: str

    human_date_dmt = '%d.%m.%y'
    human_time_fmt = '%H:%M:%S'

    @staticmethod
    def create(filename: str, *args, **kwargs):
        if filename.endswith(f'.{SoundRecordFile.EXTENSION}'):
            return SoundRecordFile(filename, *args, **kwargs)
        elif filename.endswith(f'.{CameraRecordFile.EXTENSION}'):
            return CameraRecordFile(filename, *args, **kwargs)
        else:
            raise RuntimeError(f'unsupported file extension: {filename}')

    def __init__(self, filename: str, remote=False, remote_filesize=None, storage_root='/'):
        if self.EXTENSION is None:
            raise RuntimeError('this is abstract class')

        self.name = filename
        self.storage_root = storage_root

        self.remote = remote
        self.remote_filesize = remote_filesize

        m = re.match(r'^(\d{6}-\d{6})_(\d{6}-\d{6})_id(\d+)(_\w+)?\.'+self.EXTENSION+'$', filename)
        if m:
            self.start_time = datetime.strptime(m.group(1), RecordStorage.time_fmt)
            self.stop_time = datetime.strptime(m.group(2), RecordStorage.time_fmt)
            self.record_id = int(m.group(3))
            self.file_id = (m.group(1) + '_' + m.group(2)).replace('-', '_')
        else:
            logger.warning(f'unexpected filename: {filename}')
            self.start_time = None
            self.stop_time = None
            self.record_id = None
            self.file_id = None

    @property
    def path(self):
        if self.remote:
            return RuntimeError('remote recording, can\'t get real path')

        return os.path.realpath(os.path.join(
            self.storage_root, self.name
        ))

    @property
    def start_humantime(self) -> str:
        if self.start_time is None:
            return '?'
        fmt = f'{RecordFile.human_date_dmt} {RecordFile.human_time_fmt}'
        return self.start_time.strftime(fmt)

    @property
    def stop_humantime(self) -> str:
        if self.stop_time is None:
            return '?'
        fmt = RecordFile.human_time_fmt
        if self.start_time.date() != self.stop_time.date():
            fmt = f'{RecordFile.human_date_dmt} {fmt}'
        return self.stop_time.strftime(fmt)

    @property
    def start_unixtime(self) -> int:
        if self.start_time is None:
            return 0
        return int(self.start_time.timestamp())

    @property
    def stop_unixtime(self) -> int:
        if self.stop_time is None:
            return 0
        return int(self.stop_time.timestamp())

    @property
    def filesize(self):
        if self.remote:
            if self.remote_filesize is None:
                raise RuntimeError('file is remote and remote_filesize is not set')
            return self.remote_filesize
        return os.path.getsize(self.path)

    def __dict__(self) -> dict:
        return {
            'start_unixtime': self.start_unixtime,
            'stop_unixtime': self.stop_unixtime,
            'filename': self.name,
            'filesize': self.filesize,
            'fileid': self.file_id,
            'record_id': self.record_id or 0,
        }


class PseudoRecordFile(RecordFile):
    EXTENSION = 'null'

    def __init__(self):
        super().__init__('pseudo.null')

    @property
    def filesize(self):
        return 0


class SoundRecordFile(RecordFile):
    EXTENSION = 'mp3'


class CameraRecordFile(RecordFile):
    EXTENSION = 'mp4'


# record storage
# --------------

class RecordStorage:
    EXTENSION = None

    time_fmt = '%d%m%y-%H%M%S'

    def __init__(self, root: str):
        if self.EXTENSION is None:
            raise RuntimeError('this is abstract class')

        self.root = root

    def getfiles(self, as_objects=False) -> Union[List[str], List[RecordFile]]:
        files = []
        for name in os.listdir(self.root):
            path = os.path.join(self.root, name)
            if os.path.isfile(path) and name.endswith(f'.{self.EXTENSION}'):
                files.append(name if not as_objects else RecordFile.create(name, storage_root=self.root))
        return files

    def find(self, file_id: str) -> Optional[RecordFile]:
        for name in os.listdir(self.root):
            if os.path.isfile(os.path.join(self.root, name)) and name.endswith(f'.{self.EXTENSION}'):
                item = RecordFile.create(name, storage_root=self.root)
                if item.file_id == file_id:
                    return item
        return None

    def purge(self):
        files = self.getfiles()
        if files:
            logger = logging.getLogger(self.__name__)
            for f in files:
                try:
                    path = os.path.join(self.root, f)
                    logger.debug(f'purge: deleting {path}')
                    os.unlink(path)
                except OSError as exc:
                    logger.exception(exc)

    def delete(self, file: RecordFile):
        os.unlink(file.path)

    def save(self,
             fn: str,
             record_id: int,
             start_time: int,
             stop_time: int) -> RecordFile:

        start_time_s = datetime.fromtimestamp(start_time).strftime(self.time_fmt)
        stop_time_s = datetime.fromtimestamp(stop_time).strftime(self.time_fmt)

        dst_fn = f'{start_time_s}_{stop_time_s}_id{record_id}'
        if os.path.exists(os.path.join(self.root, dst_fn)):
            dst_fn += strgen(4)
        dst_fn += f'.{self.EXTENSION}'
        dst_path = os.path.join(self.root, dst_fn)

        shutil.move(fn, dst_path)
        return RecordFile.create(dst_fn, storage_root=self.root)


class SoundRecordStorage(RecordStorage):
    EXTENSION = 'mp3'


class ESP32CameraRecordStorage(RecordStorage):
    EXTENSION = 'jpg'  # not used anyway

    def save(self, *args, **kwargs):
        return PseudoRecordFile()