From 5741470ce0dd65924018380dfd6d6b5e4131b8be Mon Sep 17 00:00:00 2001 From: Damien De Paoli Date: Mon, 11 Jan 2021 11:28:49 +1100 Subject: [PATCH] moved over to the beginnings of a class for file data, functional again --- files.py | 97 +++++++++++++++++++++++++--------------- templates/file_list.html | 6 +-- 2 files changed, 65 insertions(+), 38 deletions(-) diff --git a/files.py b/files.py index 5e6da68..3937a7f 100644 --- a/files.py +++ b/files.py @@ -23,6 +23,15 @@ from settings import Settings ################################################################################ # Utility Functions for Files ################################################################################ + +# Converts linux paths into windows paths +# HACK: assumes c:, might be best to just look for [a-z]: ? +def FixPath(p): + if p.startswith('c:'): + p = p.replace('/', '\\') + return p + +# Returns an md5 hash of the fnames' contents def md5(fname): hash_md5 = hashlib.md5() with open(fname, "rb") as f: @@ -62,44 +71,33 @@ def getExif(file): return fthumbnail -################################################################################ -# Class describing Files in the database, and via sqlalchemy, connected to the DB as well -################################################################################ -class Files(db.Model): - id = db.Column(db.Integer, db.Sequence('files_id_seq'), primary_key=True ) - name = db.Column(db.String, unique=True, nullable=False ) - type = db.Column(db.String, unique=False, nullable=False) - size_mb = db.Column(db.Integer, unique=False, nullable=False) - # hash might not be unique, this could be the source of dupe problems - hash = db.Column(db.Integer, unique=True, nullable=True) - thumbnail = db.Column(db.LargeBinary, unique=False, nullable=True) +class FileData(): + def __init__(self): + self.view_path='' + self.view_list=[] + self.symlink='' - def __repr__(self): - return "".format(self.id, self.name ) - -################################################################################ -# /file_list -> show files from import_path(s) -################################################################################ -@app.route("/file_list", methods=["GET"]) -def file_list(): +# +# HACK: At present this only handles one path (need to re-factor if we have +# multiple valid paths in import_path) +# +def GenerateFileData(): sets = Settings.query.filter(Settings.name=="import_path").all() paths= sets[0].value.split("#") file_list=[] view_list=[] - view_path="" + fdata = FileData() for p in paths: - if p.startswith('c:'): - p = p.replace('/', '\\') - if( os.path.exists( p ) ): - view_path = p + p = FixPath(p) if os.path.exists( p ): - if p.startswith('c:'): - symlink = 'static\\{}'.format( os.path.basename(p[0:-1])) - else: - symlink = 'static/{}'.format( os.path.basename(p[0:-1])) - if not os.path.exists(symlink): - os.symlink(p, symlink) - file_list.append(glob.glob(view_path + '**', recursive=True)) + fdata.view_path = p + # to serve static content of the images, we create a symlink + # from inside the static subdir of each import_path that exists + fdata.symlink = FixPath('static/{}'.format( os.path.basename(p[0:-1]))) + if not os.path.exists(fdata.symlink): + os.symlink(p, fdata.symlink) + + file_list.append(glob.glob(fdata.view_path + '**', recursive=True)) for file in file_list[0]: fthumbnail = None if file == p: @@ -121,13 +119,42 @@ def file_list(): fsize = round(os.stat(file).st_size/(1024*1024)) fname=file.replace(p, "") -# tmp_files=Files( name=fname, type=ftype, size_mb=fsize, hash=fhash ) -# db.session.add(tmp_files) view_list.append( Files( name=fname, type=ftype, size_mb=fsize, hash=fhash, thumbnail=fthumbnail )) + fdata.view_list = view_list + return fdata -# db.session.commit() +################################################################################ +# Class describing Files in the database, and via sqlalchemy, connected to the DB as well +# This has to match one-for-one the DB table +################################################################################ +class Files(db.Model): + id = db.Column(db.Integer, db.Sequence('files_id_seq'), primary_key=True ) + name = db.Column(db.String, unique=True, nullable=False ) + type = db.Column(db.String, unique=False, nullable=False) + size_mb = db.Column(db.Integer, unique=False, nullable=False) + # hash might not be unique, this could be the source of dupe problems + hash = db.Column(db.Integer, unique=True, nullable=True) + thumbnail = db.Column(db.LargeBinary, unique=False, nullable=True) - return render_template("file_list.html", page_title='View Files (details)', view_path=view_path, file_list=view_list, symlink=symlink, alert=st.GetAlert(), message=st.GetMessage() ) + def __repr__(self): + return "".format(self.id, self.name ) + + +################################################################################ +# /file_list -> show detailed file list of files from import_path(s) +################################################################################ +@app.route("/file_list", methods=["GET"]) +def file_list(): + file_data=GenerateFileData() + return render_template("file_list.html", page_title='View Files (details)', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() ) + +################################################################################ +# /files -> show thumbnail view of files from import_path(s) +################################################################################ +@app.route("/files", methods=["GET"]) +def files(): + file_data=GenerateFileData() + return render_template("files.html", page_title='View Files', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() ) @app.route("/static/") diff --git a/templates/file_list.html b/templates/file_list.html index f768580..ddf5c1e 100644 --- a/templates/file_list.html +++ b/templates/file_list.html @@ -1,10 +1,10 @@ {% extends "base.html" %} {% block main_content %}
-

{{page_title}} -- {{view_path}}

+

{{page_title}} -- {{file_data.view_path}}

- {% for obj in file_list %} + {% for obj in file_data.view_list %}
NameSize (MB)Hash
{% if obj.type=="Directory" %} @@ -16,7 +16,7 @@ {% endif %} {% if obj.type=="Image" %} - + {% endif %} {{obj.name}} {{obj.size_mb}}{{obj.hash}}