internal files
This commit is contained in:
69
internfiles/__init__.py
Normal file
69
internfiles/__init__.py
Normal 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))
|
||||||
38
internfiles/pth.py
Normal file
38
internfiles/pth.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import os
|
||||||
|
from slugify import slugify
|
||||||
|
|
||||||
|
|
||||||
|
class pth(str):
|
||||||
|
def __new__(cls, value, *args, **kw):
|
||||||
|
if not value:
|
||||||
|
value = ""
|
||||||
|
value = value.rstrip("/")
|
||||||
|
return str.__new__(cls, value, *args, **kw)
|
||||||
|
|
||||||
|
def __add__(self, add):
|
||||||
|
if isinstance(add, str) or isinstance(add, pth):
|
||||||
|
return pth(os.path.join(str(self), str(add))) # .rstrip("/")
|
||||||
|
else:
|
||||||
|
return super().__add__(add)
|
||||||
|
|
||||||
|
def __sub__(self, sub):
|
||||||
|
if isinstance(sub, int):
|
||||||
|
return pth("/".join(self.split("/")[:-sub]))
|
||||||
|
else:
|
||||||
|
return super().__sub__(sub)
|
||||||
|
|
||||||
|
def __contains__(self, path):
|
||||||
|
directory = os.path.join(os.path.realpath(str(self)), "")
|
||||||
|
file = os.path.realpath(str(path))
|
||||||
|
return os.path.commonprefix([file, directory]) == directory
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return str(self).split("/")[key]
|
||||||
|
|
||||||
|
def slugify(self):
|
||||||
|
s = self.split("/")
|
||||||
|
s = s[:-1]
|
||||||
|
s2 = s[-1]
|
||||||
|
if "." in s[-1]:
|
||||||
|
s2 = ".".join([slugify(p) for p in s2])
|
||||||
|
return "/".join([slugify(p) for p in s] + [s2])
|
||||||
2
test2.py
2
test2.py
@@ -17,6 +17,7 @@ from solrfet2020 import SolrFet2020
|
|||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
|
|
||||||
import wiki
|
import wiki
|
||||||
|
import internfiles
|
||||||
|
|
||||||
#logging.basicConfig(level=logging.INFO)
|
#logging.basicConfig(level=logging.INFO)
|
||||||
URL_HOSTNAME='bot.2020.fet.at'
|
URL_HOSTNAME='bot.2020.fet.at'
|
||||||
@@ -65,6 +66,7 @@ def auth_user(user_id, token):
|
|||||||
return "development"
|
return "development"
|
||||||
|
|
||||||
app.register_blueprint(wiki.bp)
|
app.register_blueprint(wiki.bp)
|
||||||
|
app.register_blueprint(internfiles.bp)
|
||||||
|
|
||||||
def download_file(url):
|
def download_file(url):
|
||||||
local_filename = url.split('/')[-1]
|
local_filename = url.split('/')[-1]
|
||||||
|
|||||||
Reference in New Issue
Block a user