changed Photo class and DB table to be Files, also reflected through templates/files* - files.html is not used yet

This commit is contained in:
2021-01-11 11:00:31 +11:00
parent e0400ff321
commit ccc8085aaf
5 changed files with 42 additions and 16 deletions

135
files.py Normal file
View File

@@ -0,0 +1,135 @@
from wtforms import SubmitField, StringField, HiddenField, validators, Form
from flask_wtf import FlaskForm
from flask import request, render_template, redirect, send_from_directory
from main import db, app, ma
from sqlalchemy import Sequence
from sqlalchemy.exc import SQLAlchemyError
from status import st, Status
import os
import glob
from PIL import Image
from pymediainfo import MediaInfo
import hashlib
import exifread
import base64
import numpy
################################################################################
# Local Class imports
################################################################################
from settings import Settings
################################################################################
# Utility Functions for Files
################################################################################
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def isImage(file):
try:
img = Image.open(file)
return True
except:
return False
def isVideo(file):
try:
fileInfo = MediaInfo.parse(file)
for track in fileInfo.tracks:
if track.track_type == "Video":
return True
return False
except Exception as e:
return False
def getExif(file):
f = open(file, 'rb')
try:
tags = exifread.process_file(f)
except:
print('NO EXIF TAGS?!?!?!?')
f.close()
raise
f.close()
fthumbnail = base64.b64encode(tags['JPEGThumbnail'])
fthumbnail = str(fthumbnail)[2:-1]
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)
def __repr__(self):
return "<id: {}, name: {}>".format(self.id, self.name )
################################################################################
# /file_list -> show files from import_path(s)
################################################################################
@app.route("/file_list", methods=["GET"])
def file_list():
sets = Settings.query.filter(Settings.name=="import_path").all()
paths= sets[0].value.split("#")
file_list=[]
view_list=[]
view_path=""
for p in paths:
if p.startswith('c:'):
p = p.replace('/', '\\')
if( os.path.exists( p ) ):
view_path = 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))
for file in file_list[0]:
fthumbnail = None
if file == p:
continue
if os.path.isdir(file):
ftype = 'Directory'
elif isImage(file):
ftype = 'Image'
fthumbnail = getExif(file)
elif isVideo(file):
ftype = 'Video'
else:
ftype = 'File'
if ftype != "Directory":
fhash=md5(file)
else:
fhash=None
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 ))
# db.session.commit()
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() )
@app.route("/static/<filename>")
def custom_static(filename):
return send_from_directory("static/", filename)