Merge branch 'master' of 192.168.0.2:photoassistant

This commit is contained in:
2021-01-11 11:48:33 +11:00
2 changed files with 97 additions and 96 deletions

2
BUGs
View File

@@ -1,2 +1,4 @@
### Next: 1 ### Next: 1
BUG-1: whenever we see a bug... BUG-1: whenever we see a bug...
BUG-2: Fix the function FixPath so its not just C:, use isPosixPath instead...

191
files.py
View File

@@ -19,109 +19,105 @@ import numpy
################################################################################ ################################################################################
from settings import Settings 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:
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 FileData(): class FileData():
def __init__(self): def __init__(self):
self.view_path='' self.view_path=''
self.view_list=[] self.view_list=[]
self.symlink='' self.symlink=''
self.file_list=[]
################################################################################
# Utility Functions for Files
################################################################################
# def getExif(self, file):
# HACK: At present this only handles one path (need to re-factor if we have f = open(file, 'rb')
# multiple valid paths in import_path) try:
# tags = exifread.process_file(f)
def GenerateFileData(): except:
sets = Settings.query.filter(Settings.name=="import_path").all() print('NO EXIF TAGS?!?!?!?')
paths= sets[0].value.split("#") f.close()
file_list=[] raise
view_list=[] f.close()
fdata = FileData()
for p in paths:
p = FixPath(p)
if os.path.exists( p ):
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)) fthumbnail = base64.b64encode(tags['JPEGThumbnail'])
for file in file_list[0]: fthumbnail = str(fthumbnail)[2:-1]
fthumbnail = None return fthumbnail
if file == p:
continue def isVideo(self, file):
if os.path.isdir(file): try:
ftype = 'Directory' fileInfo = MediaInfo.parse(file)
elif isImage(file): for track in fileInfo.tracks:
ftype = 'Image' if track.track_type == "Video":
fthumbnail = getExif(file) return True
elif isVideo(file): return False
ftype = 'Video' except Exception as e:
else: return False
ftype = 'File'
if ftype != "Directory": # Converts linux paths into windows paths
fhash=md5(file) # HACK: assumes c:, might be best to just look for [a-z]: ?
else: def FixPath(self, p):
fhash=None if p.startswith('c:'):
p = p.replace('/', '\\')
return p
fsize = round(os.stat(file).st_size/(1024*1024)) # Returns an md5 hash of the fnames' contents
fname=file.replace(p, "") def md5(self, fname):
view_list.append( Files( name=fname, type=ftype, size_mb=fsize, hash=fhash, thumbnail=fthumbnail )) hash_md5 = hashlib.md5()
fdata.view_list = view_list with open(fname, "rb") as f:
return fdata for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def isImage(self, file):
try:
img = Image.open(file)
return True
except:
return False
##############################################################################
# HACK: At present this only handles one path (need to re-factor if we have #
# multiple valid paths in import_path) #
##############################################################################
def GenerateFileData(self):
sets = Settings.query.filter(Settings.name=="import_path").all()
paths= sets[0].value.split("#")
for path in paths:
path = self.FixPath(path)
if os.path.exists( path ):
self.view_path = path
# to serve static content of the images, we create a symlink
# from inside the static subdir of each import_path that exists
self.symlink = self.FixPath('static/{}'.format( os.path.basename(path[0:-1])))
if not os.path.exists(self.symlink):
os.symlink(path, self.symlink)
self.file_list.append(glob.glob(self.view_path + '**', recursive=True))
for file in self.file_list[0]:
fthumbnail = None
if file == path:
continue
if os.path.isdir(file):
ftype = 'Directory'
elif self.isImage(file):
ftype = 'Image'
fthumbnail = self.getExif(file)
elif self.isVideo(file):
ftype = 'Video'
else:
ftype = 'File'
if ftype != "Directory":
fhash=self.md5(file)
else:
fhash=None
fsize = round(os.stat(file).st_size/(1024*1024))
fname=file.replace(path, "")
self.view_list.append( Files( name=fname, type=ftype, size_mb=fsize, hash=fhash, thumbnail=fthumbnail ))
return self
################################################################################ ################################################################################
# Class describing Files in the database, and via sqlalchemy, connected to the DB as well # Class describing Files in the database, and via sqlalchemy, connected to the DB as well
@@ -140,12 +136,14 @@ class Files(db.Model):
return "<id: {}, name: {}>".format(self.id, self.name ) return "<id: {}, name: {}>".format(self.id, self.name )
################################################################################ ################################################################################
# /file_list -> show detailed file list of files from import_path(s) # /file_list -> show detailed file list of files from import_path(s)
################################################################################ ################################################################################
@app.route("/file_list", methods=["GET"]) @app.route("/file_list", methods=["GET"])
def file_list(): def file_list():
file_data=GenerateFileData() filedata = FileData()
file_data=filedata.GenerateFileData()
return render_template("file_list.html", page_title='View Files (details)', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() ) return render_template("file_list.html", page_title='View Files (details)', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() )
################################################################################ ################################################################################
@@ -153,7 +151,8 @@ def file_list():
################################################################################ ################################################################################
@app.route("/files", methods=["GET"]) @app.route("/files", methods=["GET"])
def files(): def files():
file_data=GenerateFileData() filedata = FileData()
file_data=filedata.GenerateFileData()
return render_template("files.html", page_title='View Files', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() ) return render_template("files.html", page_title='View Files', file_data=file_data, alert=st.GetAlert(), message=st.GetMessage() )