-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlocal.py
More file actions
96 lines (84 loc) · 2.4 KB
/
local.py
File metadata and controls
96 lines (84 loc) · 2.4 KB
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
from pathlib import Path
import io
import time
import shutil
from typing import BinaryIO
import anyio.abc
from core.abc import ResponseFile, ResponseFileLocal, ResponseFileNotFound
from ..logger import logger
from .abc import FileInfo, Storage
class LocalStorage(Storage):
type = "local"
def __init__(
self,
name: str,
path: str,
weight: int,
**kwargs
):
super().__init__(name, path, weight, **kwargs)
async def setup(
self,
task_group: anyio.abc.TaskGroup
):
await super().setup(task_group)
path = Path(str(self.path))
path.mkdir(parents=True, exist_ok=True)
task_group.start_soon(self._check)
async def list_files(
self,
path: str
) -> list[FileInfo]:
root = Path(str(self.path)) / path
res = []
# only files
if not root.exists():
return []
for file in root.iterdir():
if not file.is_file():
continue
res.append(FileInfo(
name=file.name,
size=file.stat().st_size,
path=str(file.relative_to(root))
))
return res
async def upload(
self,
path: str,
data: BinaryIO,
size: int
):
root = Path(str(self.path)) / path
root.parent.mkdir(parents=True, exist_ok=True)
with open(root, "wb") as f:
data.seek(0)
shutil.copyfileobj(data, f)
return True
async def _check(
self,
):
file = Path(str(self.path)) / ".py_check"
while 1:
try:
file.write_text(str(time.perf_counter_ns()))
file.unlink()
self.online = True
except:
self.online = False
logger.traceback()
finally:
self.emit_status()
await anyio.sleep(60)
async def get_file(self, path: str) -> ResponseFile:
p = Path(str(self.path / path))
if not p.exists():
return ResponseFileNotFound()
size = p.stat().st_size
return ResponseFileLocal(
size=size,
path=p
)
async def check_measure(self, size: int) -> bool:
p = Path(str(self.path / "measure" / size))
return p.exists() and p.stat().st_size == size * 1024 * 1024