internal files

This commit is contained in:
www
2020-12-28 00:50:17 +00:00
parent 77205f6b3d
commit 161b5710cd
3 changed files with 109 additions and 0 deletions

69
internfiles/__init__.py Normal file
View File

@@ -0,0 +1,69 @@
from flask import Blueprint, make_response, render_template
import os
from slugify import slugify
import re
from flask import redirect, url_for, send_from_directory
from .pth import pth
bp = Blueprint("internfiles", __name__, url_prefix="/internfiles")
class Folder:
def __init__(self, path="", p=""):
self.path = pth(path)
self.p = p
self.files = []
self.dirs = []
for it in os.scandir(path):
if it.is_file():
self.files.append(pth(it.name))
if it.is_dir():
self.dirs.append(pth(it.name))
@property
def bookmarks(self):
f = self.p.split("/")
# return [i for i in range(len(f)-1) ]
b = ["/".join(f[0:i]) for i in range(len(f))]
return b
def __repr__(self):
return f"Folder({self.path})"
def load_file_and_folder(path: str = ""):
f, directory = (None, None)
path = pth(path)
datapath = pth("/mnt/save/daten")
filepath = datapath + path
if not str(path) == "" and not (filepath in datapath):
return None, None
if os.path.isfile(filepath):
f = filepath
for d in [path, path - 1, pth("")]:
if os.path.isdir(datapath + d):
directory = datapath + d
break
return f, Folder(directory, d)
@bp.route("/")
@bp.route("/<path:path>")
def web(path=""):
f, d = load_file_and_folder(path)
if not f and not d.p == path:
return redirect(url_for("internfiles.web", path=d.p), code=302)
text = None
if f and f.endswith(".txt"):
text = os.path.abspath(f)
with open(os.path.abspath(f), "r") as fh:
text = fh.read()
text = re.sub("\\n\s*\\n", "<br>", text)
elif f:
return send_from_directory(str(f - 1), str(f[-1]))
return render_template("internfiles.html", file=f, dir=d, text=text, path=pth(d.p))