-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
146 lines (127 loc) · 4.26 KB
/
files.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
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
import os
import re
from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile, status
from fastapi.responses import FileResponse, Response, StreamingResponse
from api import models
from api.core.filesystem import FileSystem
from api.dependencies import filesystem_dep
from api.schemas import file as file_schemas
router = APIRouter()
@router.get(
"/files/{file_path:path}/download",
response_model=None,
response_class=Response,
description="Download a file",
)
def download_file(
file_path: str, filesystem: FileSystem = Depends(filesystem_dep)
) -> FileResponse | StreamingResponse:
try:
return filesystem.download(file_path)
except FileNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@router.get(
"/files/{file_path:path}/url",
response_model=file_schemas.FileHTTPRequest,
description="Get request parameters (pre-signed URL) to download a file",
)
def get_download_presigned_url(
file_path: str, request: Request, filesystem: FileSystem = Depends(filesystem_dep)
) -> file_schemas.FileHTTPRequest:
try:
return filesystem.download_url(
file_path, request, re.escape("/url") + "$", "/download"
)
except FileNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@router.get(
"/files/{base_path:path}",
response_model=list[file_schemas.FileInfo],
description="List files in a directory",
)
def list_files(
base_path: str = "",
show_dirs: bool = True,
recursive: bool = False,
filesystem: FileSystem = Depends(filesystem_dep),
) -> list[file_schemas.FileInfo]:
try:
return sorted(
filesystem.list_directory(base_path, dirs=show_dirs, recursive=recursive),
key=lambda x: x.path,
)
except NotADirectoryError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@router.post(
"/files/{f_type}/{base_path:path}/upload",
response_model=file_schemas.FileInfo,
status_code=status.HTTP_201_CREATED,
description="Upload a file",
)
def upload_file(
f_type: models.UploadFileTypes,
base_path: str,
file: UploadFile,
filesystem: FileSystem = Depends(filesystem_dep),
) -> file_schemas.FileInfo:
base_path = f"{f_type.value}/" + base_path
file_path = os.path.join(base_path, file.filename or "unnamed")
filesystem.create_file(file_path, file.file)
return filesystem.get_file_info(file_path)
@router.post(
"/files/{f_type}/{base_path:path}/url",
status_code=status.HTTP_201_CREATED,
response_model=file_schemas.FileHTTPRequest,
description="Get request parameters (pre-signed URL) to upload a file",
)
def get_upload_presigned_url(
f_type: models.UploadFileTypes,
base_path: str,
request: Request,
filesystem: FileSystem = Depends(filesystem_dep),
) -> file_schemas.FileHTTPRequest:
base_path = f"{f_type.value}/" + base_path
return filesystem.create_file_url(
base_path, request, re.escape("/url") + "$", "/upload"
)
@router.post(
"/files/{f_type}/{base_path:path}/",
status_code=status.HTTP_201_CREATED,
description="Create a directory",
)
def create_directory(
f_type: models.UploadFileTypes,
base_path: str,
filesystem: FileSystem = Depends(filesystem_dep),
) -> None:
return filesystem.create_directory(f"{f_type.value}/{base_path}/")
@router.put(
"/files/{file_path:path}",
response_model=file_schemas.FileInfo,
description="Rename a file",
)
def rename_file(
file_path: str,
file: file_schemas.FileUpdate,
filesystem: FileSystem = Depends(filesystem_dep),
) -> file_schemas.FileInfo:
try:
filesystem.rename(file_path, file.path)
except FileNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
except IsADirectoryError as e:
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
detail=str(e),
)
return filesystem.get_file_info(file.path)
@router.delete(
"/files/{file_path:path}",
status_code=status.HTTP_204_NO_CONTENT,
response_model=None,
description="Delete a file or directory",
)
def delete_file(
file_path: str, filesystem: FileSystem = Depends(filesystem_dep)
) -> None:
filesystem.delete(file_path)