WebFM/src/core/utils/shellfm/windows/View.py

160 lines
4.3 KiB
Python
Raw Normal View History

2021-02-06 04:52:46 +00:00
# Python imports
2021-02-08 00:44:35 +00:00
import hashlib
import os
2021-02-06 04:52:46 +00:00
from os import listdir
2021-02-06 07:38:15 +00:00
from os.path import isdir, isfile, join
2021-02-06 04:52:46 +00:00
# Lib imports
2021-02-06 07:38:15 +00:00
2021-02-06 04:52:46 +00:00
# Application imports
from . import Path, Settings, Launcher
2021-02-06 04:52:46 +00:00
class View(Settings, Launcher, Path):
2021-02-06 04:52:46 +00:00
def __init__(self):
self.files = []
2021-02-07 22:52:08 +00:00
self.dirs = []
2021-02-06 04:52:46 +00:00
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
2021-02-08 00:44:35 +00:00
self.set_to_home()
2021-02-06 04:52:46 +00:00
def load_directory(self):
path = self.get_path()
2021-02-07 22:52:08 +00:00
self.dirs = []
2021-02-06 04:52:46 +00:00
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
self.files = []
if not isdir(path):
self.set_to_home()
return ""
for f in listdir(path):
file = join(path, f)
2021-02-08 00:44:35 +00:00
if self.HIDE_HIDDEN_FILES:
2021-02-06 04:52:46 +00:00
if f.startswith('.'):
continue
if isfile(file):
lowerName = file.lower()
if lowerName.endswith(self.fvideos):
self.vids.append(f)
elif lowerName.endswith(self.fimages):
self.images.append(f)
elif lowerName.endswith((".desktop",)):
self.desktop.append(f)
else:
self.ungrouped.append(f)
else:
self.dirs.append(f)
self.dirs.sort()
self.vids.sort()
self.images.sort()
self.desktop.sort()
self.ungrouped.sort()
self.files = self.dirs + self.vids + self.images + self.desktop + self.ungrouped
2021-02-06 07:38:15 +00:00
def hashText(self, text):
return hashlib.sha256(str.encode(text)).hexdigest()[:18]
def hashSet(self, arry):
data = []
for arr in arry:
data.append([arr, self.hashText(arr)])
return data
2021-02-06 04:52:46 +00:00
2021-02-08 00:44:35 +00:00
def get_path_part_from_hash(self, hash):
2021-02-06 08:34:44 +00:00
files = self.get_files()
file = None
for f in files:
if hash == f[1]:
file = f[0]
break
return file
2021-02-06 08:34:44 +00:00
def get_files_formatted(self):
2021-02-07 22:52:08 +00:00
files = self.hashSet(self.files),
dirs = self.hashSet(self.dirs),
videos = self.get_videos(),
2021-02-07 22:52:08 +00:00
images = self.hashSet(self.images),
desktops = self.hashSet(self.desktop),
ungrouped = self.hashSet(self.ungrouped)
return {
2021-02-07 22:52:08 +00:00
'path_head': self.get_path(),
'list': {
'files': files,
'dirs': dirs,
'videos': videos,
'images': images,
'desktops': desktops,
'ungrouped': ungrouped
}
}
2021-02-08 00:44:35 +00:00
def is_folder_locked(self, hash):
if self.lock_folder:
path_parts = self.get_path().split('/')
file = self.get_path_part_from_hash(hash)
# Insure chilren folders are locked too.
lockedFolderInPath = False
for folder in self.locked_folders:
if folder in path_parts:
lockedFolderInPath = True
break
return (file in self.locked_folders or lockedFolderInPath)
else:
return False
2021-02-07 22:52:08 +00:00
def get_current_directory(self):
return self.get_path()
2021-02-10 04:15:32 +00:00
def get_current_sub_path(self):
path = self.get_path()
home = self.get_home() + "/"
return path.replace(home, "")
2021-02-07 22:52:08 +00:00
def get_dot_dots(self):
return self.hashSet(['.', '..'])
2021-02-06 04:52:46 +00:00
def get_files(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.files)
2021-02-06 04:52:46 +00:00
def get_dirs(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.dirs)
2021-02-06 04:52:46 +00:00
def get_videos(self):
videos_set = self.hashSet(self.vids)
current_directory = self.get_current_directory()
for video in videos_set:
hashImgPth = join(self.ABS_THUMBS_PTH, video[1]) + ".jpg"
if not os.path.exists(hashImgPth) :
fullPath = join(current_directory, video[0])
self.logger.debug(f"Hash Path: {hashImgPth}\nFile Path: {fullPath}")
self.generateVideoThumbnail(fullPath, hashImgPth)
return videos_set
2021-02-06 04:52:46 +00:00
def get_images(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.images)
2021-02-06 04:52:46 +00:00
def get_desktops(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.desktop)
2021-02-06 04:52:46 +00:00
def get_ungrouped(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.ungrouped)