Initial commit
This commit is contained in:
commit
78f8d225ee
21173 changed files with 2907774 additions and 0 deletions
44
app/files/main.py
Normal file
44
app/files/main.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi.responses import FileResponse
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
import os
|
||||
|
||||
app = FastAPI()
|
||||
FILES_ROOT = Path("/var/www/lasereverything.net.db/files").resolve()
|
||||
|
||||
@app.get("/list-files")
|
||||
def list_files(
|
||||
path: str = "",
|
||||
offset: int = 0,
|
||||
limit: int = 50
|
||||
):
|
||||
base_path = (FILES_ROOT / path).resolve()
|
||||
|
||||
if not base_path.is_dir() or not str(base_path).startswith(str(FILES_ROOT)):
|
||||
raise HTTPException(status_code=400, detail="Invalid path")
|
||||
|
||||
entries = []
|
||||
for item in base_path.iterdir():
|
||||
if item.is_dir():
|
||||
entries.append(f"{item.name}/")
|
||||
else:
|
||||
entries.append(item.name)
|
||||
|
||||
entries.sort()
|
||||
paginated = entries[offset:offset + limit]
|
||||
|
||||
return {
|
||||
"total": len(entries),
|
||||
"items": paginated
|
||||
}
|
||||
|
||||
@app.get("/download/{file_path:path}")
|
||||
def download_file(file_path: str):
|
||||
full_path = (FILES_ROOT / file_path).resolve()
|
||||
|
||||
if not full_path.is_file() or not str(full_path).startswith(str(FILES_ROOT)):
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
|
||||
return FileResponse(full_path, filename=full_path.name)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue