-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
437 lines (323 loc) · 14.4 KB
/
app.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from fastapi import FastAPI, HTTPException, Body, Query, Request, BackgroundTasks
from fastapi.responses import JSONResponse, Response, StreamingResponse
from pydantic import BaseModel
from pathlib import Path
import shutil
import magic
from typing import Annotated, Optional, List
from fastapi.middleware.cors import CORSMiddleware
from http import HTTPStatus
import zipfile
from datetime import datetime
from io import BytesIO
import tempfile
app = FastAPI()
# CORS Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"],
allow_methods=["*"],
allow_headers=["*"],
)
BUFFER_SIZE = 1024 * 1024 # 1MB
MAX_ZIP_MEMORY_SIZE = 1024 * 1024 * 512 # 512MB
MAX_UNCOMPRESSED_SIZE = 1024 * 1024 * 128 # 128MB
mime = magic.Magic(mime=True)
# Define the root directory to restrict access
ROOT_DIRECTORY = Path("./uploads").resolve()
print(f"Root Directory: {ROOT_DIRECTORY}")
# Will be combined with user directory eventually
def secure_path(path: str) -> Path:
if path == "/":
path = "." # Make root directory relative
else:
path = path.lstrip("/") # Make all paths provided relative
combined_path = ROOT_DIRECTORY / path
print(ROOT_DIRECTORY, combined_path)
# Make sure path is within defined root directory
resolved_path = combined_path.resolve()
if not resolved_path.is_relative_to(ROOT_DIRECTORY):
raise HTTPException(status_code=403, detail="Access to this path is forbidden")
return resolved_path
# [GET] /api/directory
@app.get("/api/directory")
async def list_directory(path: list[str] = Query(...)):
paths = set(path)
paths_info = {}
for p in paths:
directory = secure_path(p)
if not directory.exists() or not directory.is_dir():
continue
#raise HTTPException(status_code=404, detail="Directory not found")
files_info = []
for file_path in directory.iterdir():
files_info.append(
get_file_info(file_path)
)
paths_info[p] = files_info
return {"directories": paths_info}
# [GET] /api/file
@app.get("/api/file")
async def list_file(path: list[str] = Query(...)):
paths = set(path)
files_info = {}
for p in paths:
file_path = secure_path(p)
if not file_path.exists() or not file_path.is_file():
files_info[p] = None
#raise HTTPException(status_code=404, detail="Directory not found")
continue
files_info[p] = get_file_info(file_path)
return {"files": files_info}
# [GET] /api/file/read
@app.get("/api/file/read")
async def read_file(background_tasks: BackgroundTasks, path: list[str] = Query(...), nodl: Optional[bool] = Query(False)):
"""
Read file from the server and return it as a Response or StreamingResponse
Args:
background_tasks (BackgroundTasks): Background task to delete temporary files after request
path (list[str] = Query(...)): List of paths to read
nodl (Optional[bool] = Query(False)): Return file without attachment header so it can be opened in the browser
Retruns:
Response: File content as a Response or Streaming
"""
paths = set(path)
nonexistent_files = []
for p in paths:
file_path = secure_path(p)
if not file_path.exists():
nonexistent_files.append(p)
continue
if len(nonexistent_files) > 0:
return JSONResponse(status_code=404, content={"message": "Files not found", "files": nonexistent_files})
if len(paths) == 0:
raise HTTPException(status_code=400, detail="Paths not provided in request")
elif len(paths) == 1 and secure_path(path[0]).is_file():
return read_single_file(paths.pop(), nodl, background_tasks)
else:
return read_multiple_files(paths, background_tasks)
if not file_path.exists() or not file_path.is_file():
raise HTTPException(status_code=404, detail="File not found")
return read_single_file(file_path, nodl, background_tasks)
def read_single_file(path: str, nodl: bool, background_tasks: BackgroundTasks):
"""
Read a single file from the server and return it as a Response or StreamingResponse
Uses file size to determine if the file should be returned as a Response or StreamingResponse and if it should be compressed
Args:
path (str): Path to read
nodl (bool): Return file without attachment header so it can be opened in the browser
background_tasks (BackgroundTasks): Background task to delete temporary files after request
Returns:
Response: File content as a Response or StreamingResponse
"""
file_path = secure_path(path)
with open(file_path, "rb") as file:
if file_path.stat().st_size < BUFFER_SIZE:
if nodl:
return Response(content=file_unbuffered(file_path), media_type=mime.from_file(str(file_path)))
else:
return Response(content=file_unbuffered(file_path), media_type='application/octet-stream', headers={'Content-Disposition': f'attachment; filename={file_path.name}'})
# Return Response with mime type so it can be opened by the browser
if file_path.stat().st_size < MAX_UNCOMPRESSED_SIZE:
return StreamingResponse(content=file_stream(file_path), media_type='application/octet-stream', headers={'Content-Disposition': f'attachment; filename={file_path.name}'})
return read_multiple_files(set(path))
def file_unbuffered(file_path: str):
"""
Read file from the server and return it as a Response
Args:
file_path (str): Path to read
Returns:
bytes: File content
"""
with open(file_path, "rb") as file:
return file.read()
def file_stream(file_path: str):
"""
Read file from the server and return it as a StreamingResponse
Args:
file_path (str): Path to read
Returns:
Generator: File content
"""
with open(file_path, "rb") as file:
while True:
chunk = file.read(BUFFER_SIZE)
if not chunk:
break
yield chunk
def read_multiple_files(paths: set[str], background_tasks: BackgroundTasks):
"""
Read multiple files from the server and return them as a Response or StreamingResponse
Args:
paths (set[str]): Paths to read
background_tasks (BackgroundTasks): Background task to delete temporary files after request
Returns:
Response: File content as a Response or StreamingResponse
"""
paths = [secure_path(p) for p in paths]
zbuf = BytesIO()
zipname = f'NextFileManager-{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'
with zipfile.ZipFile(zbuf, "w", zipfile.ZIP_DEFLATED) as zf:
for file_path in paths:
if not file_path.exists():
raise HTTPException(status_code=404, detail="File deleted during compression")
zf.write(file_path, arcname=file_path.name)
if zbuf.tell() > MAX_ZIP_MEMORY_SIZE:
zf.close()
tempfile.mk
temp_file_path = Path(tempfile.mkdtemp()) / f"{zipname}.zip"
background_tasks.add_task(delete_after_request, temp_file_path.parent)
with open(temp_file_path, 'wb') as f:
f.write(zbuf.getvalue())
# Return zip file stream
return StreamingResponse(open(temp_file_path, 'rb'), media_type='application/zip', headers={'Content-Disposition': f'attachment; filename={zipname}.zip'})
# Return memory buffer stream
zbuf.seek(0)
return StreamingResponse(zbuf, media_type='application/zip', headers={'Content-Disposition': f'attachment; filename={zipname}.zip'})
# Required to be parsed as a list instead of query parameter
class DirectoryRequest(BaseModel):
path: List[str]
# [POST] /api/directory
@app.post("/api/directory")
async def create_directory(request: DirectoryRequest, verbose: Optional[bool] = Query(False)):
path = request.path
if len(path) == 0:
raise HTTPException(status_code=400, detail="Paths not provided in request")
paths = set(path)
successful_paths = []
failed_paths = []
for p in paths:
directory = secure_path(p)
try:
directory.mkdir(parents=True, exist_ok=False)
except FileExistsError:
if (directory.is_file()):
failed_paths.append(p)
elif (directory.is_dir()):
successful_paths.append(p)
continue
#raise HTTPException(status_code=400, detail="Directory already exists")
except Exception as e:
failed_paths.append(p)
#raise HTTPException(status_code=500, detail=str(e))
successful_paths.append(p)
return JSONResponse(**generate_response(successful_paths, failed_paths, "Directories created", verbose))
# [PATCH] /api/directory
@app.patch("/api/directory")
async def rename_directory(paths: dict[str, str], verbose: Optional[bool] = Query(False)):
successful_paths = {}
failed_paths = {}
for old_name, new_name in paths.items():
old_path = secure_path(old_name)
new_path = secure_path(new_name)
if not old_path.exists() or not old_path.exists():
#raise HTTPException(status_code=404, detail="Directory not found")
failed_paths[old_name] = new_name
continue
if new_path.exists():
failed_paths[old_name] = new_name
continue
try:
old_path.rename(new_path)
except Exception as e:
#raise HTTPException(status_code=500, detail=str(e))
failed_paths[old_name] = new_name
continue
successful_paths[old_name] = new_name
return JSONResponse(**generate_response(successful_paths, failed_paths, "Directories renamed", verbose))
# [DELETE] /api/directory
@app.delete("/api/directory") # Must use query parameters as body is not supported in DELETE requests
async def delete_directory(path: list[str] = Query(...), verbose: Optional[bool] = Query(False)):
if len(path) == 0:
raise HTTPException(status_code=400, detail="Paths not provided in request")
paths = set(path)
successful_paths = []
failed_paths = []
for p in paths:
directory = secure_path(p)
if not directory.exists() or not directory.is_dir():
failed_paths.append(p)
continue
try:
shutil.rmtree(directory) # Recursive delete
except Exception as e:
failed_paths.append(p)
continue
successful_paths.append(p)
return JSONResponse(**generate_response(successful_paths, failed_paths, "Directories deleted", verbose))
# [PATCH] /api/file
@app.patch("/api/file")
async def rename_file(paths: dict[str, str], verbose: Optional[bool] = Query(False)):
successful_paths = {}
failed_paths = {}
for old_name, new_name in paths.items():
old_file = secure_path(old_name)
new_file = secure_path(new_name)
if not old_file.exists() or not old_file.is_file():
failed_paths[old_name] = new_name
continue
if new_file.exists():
failed_paths[old_name] = new_name
continue
try:
old_file.rename(new_file)
except Exception as e:
failed_paths[old_name] = new_name
continue
successful_paths[old_name] = new_name
return JSONResponse(**generate_response(successful_paths, failed_paths, "Files renamed", verbose))
# [DELETE] /api/file
@app.delete("/api/file")
async def delete_file(path: list[str] = Query(...), verbose: Optional[bool] = Query(False)): # Must use query parameters as body is not supported in DELETE requests
if len(path) == 0:
raise HTTPException(status_code=400, detail="Paths not provided in request")
paths = set(path)
successful_paths = []
failed_paths = []
for p in paths:
file_path = secure_path(p)
# Print file info
if not file_path.exists() or not file_path.is_file():
failed_paths.append(p)
continue
try:
file_path.unlink() # Delete file
except Exception as e:
failed_paths.append(p)
continue
successful_paths.append(p)
return JSONResponse(**generate_response(successful_paths, failed_paths, "Files deleted", verbose))
# Function to determine the response
# Will be moved to a utils library later
def generate_response(successful_paths, failed_paths, operation, verbose):
http_status = HTTPStatus.OK.value
if len(successful_paths) == 0 and len(failed_paths) > 0:
http_status = HTTPStatus.BAD_REQUEST.value
if len(successful_paths) > 0 and len(failed_paths) > 0:
http_status = HTTPStatus.MULTI_STATUS.value
if verbose:
return {"status_code": http_status, "content": {"message": str(len(successful_paths)) + " " + operation + " successfully", "paths": {"success": successful_paths, "fail": failed_paths}}}
if (len(failed_paths) > 0):
return {"status_code": http_status, "content": {"message": str(len(successful_paths)) + " " + operation + " successfully", "failed": failed_paths}}
return {"status_code": http_status, "content": {"message": str(len(successful_paths)) + " " + operation + " successfully"}}
def get_file_info(file_path: Path):
return {
"modified": int(file_path.stat().st_mtime),
"created": int(file_path.stat().st_ctime),
"fileName": file_path.name,
"thumbnail": "placeholder_thumbnail", # Temporary Placeholder
"mime_type": "inode/directory" if file_path.is_dir() else mime.from_file(str(file_path)) if file_path.is_file() else "unknown",
"size": file_path.stat().st_size,
}
def delete_after_request(file_path):
try:
file_path.unlink()
except Exception as e:
print(f"Unable to delete file: {str(e)}")
# Return 'message' instead of 'detail' for all HTTPExceptions to be interpreted by frontend
@app.exception_handler(HTTPException)
def custom_http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail} # Change "detail" to "message"
)