-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_storage.py
31 lines (24 loc) · 1.04 KB
/
local_storage.py
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
from io import BytesIO
from pathlib import Path
from typing import IO, Union
from uuid import uuid4
from speech_recognition_api.core.async_api.file_storage.interface import IFileStorage
class LocalStorage(IFileStorage):
def __init__(self, folder_path: Union[Path, str]) -> None:
if isinstance(folder_path, str):
folder_path = Path(folder_path)
self.folder_path = folder_path # TODO: add checks
def save_file(self, file: IO) -> str:
name = str(uuid4())
with (self.folder_path / name).open("wb") as output_file:
for b in file:
output_file.write(b)
return name
def get_file(self, file_id: str) -> IO:
with (self.folder_path / file_id).open("rb") as file:
output = file.read()
return BytesIO(output)
@classmethod
def build_from_config(cls) -> "LocalStorage":
from speech_recognition_api.extra.local_storage.config import local_storage_config # noqa: PLC0415
return cls(folder_path=local_storage_config.folder_path)