use new base_path Setting, but have not tested use of absolute paths instead of relative paths, also need better tooltips for the paths -- AND, still have odd trailing slash due to SymLinkName, etc. being too complex

This commit is contained in:
2021-09-05 21:58:54 +10:00
parent 7c192b5d66
commit a64b651118
6 changed files with 158 additions and 55 deletions

View File

@@ -231,6 +231,7 @@ class FileType(Base):
class Settings(Base):
__tablename__ = "settings"
id = Column(Integer, Sequence('settings_id_seq'), primary_key=True )
base_path = Column(String)
import_path = Column(String)
storage_path = Column(String)
recycle_bin_path = Column(String)
@@ -239,7 +240,7 @@ class Settings(Base):
default_threshold = Column(Integer)
def __repr__(self):
return f"<id: {self.id}, import_path: {self.import_path}, recycle_bin_path: {self.recycle_bin_path}, default_refimg_model: {self.default_refimg_model}, default_scan_model: {self.default_scan_model}, default_threshold: {self.default_threshold}>"
return f"<id: {self.id}, base_path: {self.base_path}, import_path: {self.import_path}, storage_path: {self.storage_path}, recycle_bin_path: {self.recycle_bin_path}, default_refimg_model: {self.default_refimg_model}, default_scan_model: {self.default_scan_model}, default_threshold: {self.default_threshold}>"
################################################################################
# Class describing Person to Refimg link in DB via sqlalchemy
@@ -421,50 +422,89 @@ def MessageToFE( job_id, alert, message ):
session.commit()
return msg.id
##############################################################################
# SettingsRBPath(): return modified array of paths (take each path in
# recycle_bin_path and add base_path if needed)
##############################################################################
def SettingsRBPath():
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / recycle bin path is missing")
# path setting is an absolute path, just use it, otherwise prepend base_path first
if settings.recycle_bin_path[0] == '/':
path = settings.recycle_bin_path
else:
path = settings.base_path+settings.recycle_bin_path
return path
##############################################################################
# ProcessRecycleBinDir(): create Path/symlink if needed (func called once on
# startup)
##############################################################################
def ProcessRecycleBinDir(job):
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / recycle bin path is missing")
path = SettingsRBPath()
print( f"here1: {path}" )
if not os.path.exists( path ):
AddLogForJob( job, f"Not Importing {path} -- Path does not exist" )
return
ptype = session.query(PathType).filter(PathType.name=='Bin').first()
paths = settings.recycle_bin_path.split("#")
for path in paths:
if not os.path.exists( path ):
AddLogForJob( job, f"Not Importing {path} -- Path does not exist" )
continue
symlink=SymlinkName(ptype.name, path, path)
# create the Path (and Dir objects for the Bin)
AddPath( job, symlink, ptype.id )
# check/create if needed
symlink=CreateSymlink(job,ptype.id,path)
print( f"here2: {path}, s={symlink}" )
# create the Path (and Dir objects for the Bin)
AddPath( job, symlink, ptype.id )
session.commit()
return
##############################################################################
# SettingsSPath(): return modified array of paths (take each path in
# storage_path and add base_path if needed)
##############################################################################
def SettingsSPath():
paths=[]
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / storage path is missing")
for p in settings.storage_path.split("#"):
if p[0] == '/':
paths.append(p)
else:
paths.append(settings.base_path+p)
return paths
##############################################################################
# ProcessStorageDirs(): wrapper func to call passed in job for each
# storage path defined in Settings - called via scan storage job
##############################################################################
def ProcessStorageDirs(parent_job):
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / storage path is missing")
paths = settings.storage_path.split("#")
paths = SettingsSPath()
ptype = session.query(PathType).filter(PathType.name=='Storage').first()
JobsForPaths( parent_job, paths, ptype )
return
##############################################################################
# SettingsIPath(): return modified array of paths (take each path in
# import_path and add base_path if needed)
##############################################################################
def SettingsIPath():
paths=[]
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / import path is missing")
for p in settings.import_path.split("#"):
if p[0] == '/':
paths.append(p)
else:
paths.append(settings.base_path+p)
return paths
##############################################################################
# ProcessImportDirs(): wrapper func to call passed in job for each
# storage path defined in Settings - called via scan import job
##############################################################################
def ProcessImportDirs(parent_job):
settings = session.query(Settings).first()
if settings == None:
raise Exception("Cannot create file data with no settings / import path is missing")
paths = settings.import_path.split("#")
paths = SettingsIPath()
ptype = session.query(PathType).filter(PathType.name=='Import').first()
JobsForPaths( parent_job, paths, ptype )
return
@@ -720,7 +760,7 @@ def JobForceScan(job):
##############################################################################
# CreateSymlink(): to serve static content of the images, we create a symlink
# from inside the static subdir of each import_path that exists
# from inside the static subdir of each path that exists
##############################################################################
def CreateSymlink(job,ptype,path):
path_type = session.query(PathType).get(ptype)
@@ -770,7 +810,7 @@ def AddDir(job, dirname, in_dir, rel_path, in_path ):
dtype=session.query(FileType).filter(FileType.name=='Directory').first()
e=Entry( name=dirname, type=dtype, exists_on_fs=True )
e.dir_details=dir
# no in_dir occurs when we Add the actual Dir for the import_path (top of the tree)
# no in_dir occurs when we Add the actual Dir for the Path (top of the tree)
if in_dir:
e.in_dir=in_dir
if DEBUG==1:
@@ -924,7 +964,7 @@ def RestoreFile(job,restore_me):
def MoveFileToRecycleBin(job,del_me):
try:
settings = session.query(Settings).first()
dst_dir=settings.recycle_bin_path + '/' + del_me.in_dir.in_path.path_prefix.replace('static/','') + '/' + del_me.in_dir.rel_path + '/'
dst_dir= SettingsRBPath() + '/' + del_me.in_dir.in_path.path_prefix.replace('static/','') + '/' + del_me.in_dir.rel_path + '/'
os.makedirs( dst_dir,mode=0o777, exist_ok=True )
src=del_me.FullPathOnFS()
dst=dst_dir + '/' + del_me.name
@@ -1531,7 +1571,7 @@ def JobDeleteFiles(job):
if 'eid-' in jex.name:
del_me=session.query(Entry).join(File).filter(Entry.id==jex.value).first()
MoveFileToRecycleBin(job,del_me)
ynw=datetime.now(pytz.utc)
now=datetime.now(pytz.utc)
next_job=Job(start_time=now, last_update=now, name="checkdups", state="New", wait_for=None, pa_job_state="New", current_file_num=0 )
session.add(next_job)
MessageToFE( job.id, "success", "Completed (delete of selected files)" )
@@ -1564,28 +1604,25 @@ def InitialValidationChecks():
job=Job(start_time=now, last_update=now, name="init", state="New", wait_for=None, pa_job_state="New", current_file_num=0 )
session.add(job)
settings = session.query(Settings).first()
rbp_exists=0
paths = settings.recycle_bin_path.split("#")
AddLogForJob(job, f"INFO: Starting Initial Validation checks...")
for path in paths:
if os.path.exists(path):
rbp_exists=1
ptype = session.query(PathType).filter(PathType.name=='Bin').first().id
symlink=CreateSymlink(job,ptype,path)
root, dirs, files = next(os.walk(path))
if len(dirs) + len(files) > 0:
AddLogForJob(job, "INFO: the bin path contains content, cannot process to know where original deletes were form - skipping content!" )
AddLogForJob(job, "TODO: could be smart about what is known in the DB vs on the FS, and change below to an ERROR if it is one")
AddLogForJob(job, "WARNING: IF the files in the bin are in the DB (succeeded from GUI deletes) then this is okay, otherwise you should delete contents form the recycle bin and restart the job manager)" )
break
if not rbp_exists:
AddLogForJob(job, "ERROR: The bin path in settings does not exist - Please fix now");
path=SettingsRBPath()
rbp_exists=0
if os.path.exists(path):
rbp_exists=1
root, dirs, files = next(os.walk(path))
if len(dirs) + len(files) > 0:
AddLogForJob(job, "INFO: the bin path contains content, cannot process to know where original deletes were form - skipping content!" )
AddLogForJob(job, "TODO: could be smart about what is known in the DB vs on the FS, and change below to an ERROR if it is one")
AddLogForJob(job, "WARNING: IF the files in the bin are in the DB (succeeded from GUI deletes) then this is okay, otherwise you should delete contents form the recycle bin and restart the job manager)" )
# create symlink and Path/Dir if needed
ProcessRecycleBinDir(job)
# bin_path=session.query(Path).join(PathType).filter(PathType.name=='Bin').first()
# if not bin_path:
# ProcessRecycleBinDir(job)
else:
bin_path=session.query(Path).join(PathType).filter(PathType.name=='Bin').first()
if not bin_path:
ProcessRecycleBinDir(job)
AddLogForJob(job, "ERROR: The bin path in settings does not exist - Please fix now");
sp_exists=0
paths = settings.storage_path.split("#")
paths = SettingsSPath()
for path in paths:
if os.path.exists(path):
sp_exists=1
@@ -1594,7 +1631,7 @@ def InitialValidationChecks():
if not sp_exists:
AddLogForJob(job, "ERROR: None of the storage paths in the settings exist - Please fix now");
ip_exists=0
paths = settings.import_path.split("#")
paths = SettingsIPath()
for path in paths:
if os.path.exists(path):
ip_exists=1