|
1 | 1 | from typing import Optional |
2 | 2 |
|
| 3 | +from pathlib import Path |
3 | 4 | from pydantic import BaseModel, Field |
| 5 | +from app.schemas.types import StorageSchema |
4 | 6 |
|
5 | 7 |
|
6 | | -class FileItem(BaseModel): |
| 8 | +class FileURI(BaseModel): |
| 9 | + # 文件路径 |
| 10 | + path: Optional[str] = "/" |
7 | 11 | # 存储类型 |
8 | 12 | storage: Optional[str] = Field(default="local") |
| 13 | + |
| 14 | + @property |
| 15 | + def uri(self) -> str: |
| 16 | + return self.path if self.storage == "local" else f"{self.storage}:{self.path}" |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def from_uri(cls, uri: str) -> "FileURI": |
| 20 | + storage, path = 'local', uri |
| 21 | + for s in StorageSchema: |
| 22 | + protocol = f"{s.value}:" |
| 23 | + if uri.startswith(protocol): |
| 24 | + path = uri[len(protocol):] |
| 25 | + storage = s.value |
| 26 | + break |
| 27 | + if not path.startswith("/"): |
| 28 | + path = "/" + path |
| 29 | + path = Path(path).as_posix() |
| 30 | + return cls(storage=storage, path=path) |
| 31 | + |
| 32 | +class FileItem(FileURI): |
9 | 33 | # 类型 dir/file |
10 | 34 | type: Optional[str] = None |
11 | | - # 文件路径 |
12 | | - path: Optional[str] = "/" |
13 | 35 | # 文件名 |
14 | 36 | name: Optional[str] = None |
15 | 37 | # 文件名 |
@@ -46,3 +68,4 @@ class StorageUsage(BaseModel): |
46 | 68 | class StorageTransType(BaseModel): |
47 | 69 | # 传输类型 |
48 | 70 | transtype: Optional[dict] = Field(default_factory=dict) |
| 71 | + |
0 commit comments