50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from settings import app, root_path
|
|
from flask import request, send_from_directory
|
|
import os
|
|
import mime
|
|
from auth import token_auth
|
|
from errors import error_response
|
|
|
|
|
|
@app.route("/api/file/root")
|
|
@token_auth.login_required
|
|
def get_root_path() -> list:
|
|
user = token_auth.current_user()
|
|
if not user.is_admin:
|
|
return error_response(403)
|
|
file_list = []
|
|
for root, dirs, files in os.walk(root_path, topdown=False):
|
|
files = list(
|
|
map(
|
|
lambda file: {
|
|
"name": file,
|
|
"size": os.path.getsize(root + "/" + file),
|
|
},
|
|
files,
|
|
)
|
|
)
|
|
file_list.append(
|
|
{
|
|
"root": "/" + "/".join(root.split("/")[1:]),
|
|
"dirs": dirs,
|
|
"files": files,
|
|
}
|
|
)
|
|
return file_list[::-1]
|
|
|
|
|
|
@app.route("/api/file", methods=["POST"])
|
|
@token_auth.login_required
|
|
def get_file() -> dict:
|
|
req = request.get_json()
|
|
path = req["path"]
|
|
mimetype = str(mime.Types.of(path + "/" + req["filename"])[0])
|
|
if not os.path.exists(path):
|
|
return {"error": "file not found"}
|
|
try:
|
|
return send_from_directory(
|
|
path, req["filename"], as_attachment=True, mimetype=mimetype
|
|
)
|
|
except FileNotFoundError:
|
|
return error_response(404, f"File not found: {path}/{req["filename"]}")
|