-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfile.py
61 lines (53 loc) · 1.44 KB
/
file.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
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
from enum import Enum
from urllib.parse import unquote, urlparse
from pydantic import BaseModel
class FileType(Enum):
pdf = "PDF"
docx = "DOCX"
txt = "TXT"
pptx = "PPTX"
md = "MARKDOWN"
csv = "CSV"
xlsx = "XLSX"
html = "HTML"
json = "JSON"
eml = "EML"
msg = "MSG"
def suffix(self) -> str:
suffixes = {
"TXT": ".txt",
"PDF": ".pdf",
"MARKDOWN": ".md",
"DOCX": ".docx",
"CSV": ".csv",
"XLSX": ".xlsx",
"PPTX": ".pptx",
"HTML": ".html",
"JSON": ".json",
"EML": ".eml",
"MSG": ".msg",
}
return suffixes[self.value]
class File(BaseModel):
url: str
name: str | None = None
metadata: dict | None = None
@property
def type(self) -> FileType | None:
url = self.url
if url:
parsed_url = urlparse(url)
path = unquote(parsed_url.path)
extension = path.split(".")[-1].lower()
try:
return FileType[extension]
except KeyError:
raise ValueError(f"Unsupported file type for URL: {url}")
return None
@property
def suffix(self) -> str:
file_type = self.type
if file_type is not None:
return file_type.suffix()
else:
raise ValueError("File type is undefined, cannot determine suffix.")