71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
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
|
|
import bs4
|
|
|
|
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", encoding='utf-8') as fh:
|
|
text = (fh.read())
|
|
text = re.sub("\\n\s*\\n", "<br>", text)
|
|
#text=bs4.BeautifulSoup(text).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))
|