Skip to content

Commit

Permalink
fix current path
Browse files Browse the repository at this point in the history
  • Loading branch information
fnwinter committed Sep 20, 2024
1 parent a889d57 commit 888e9be
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
58 changes: 32 additions & 26 deletions cnas/pages/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from util.file_util import is_image_file
from util.config_path import get_gallery_path
from util.system_path import get_gallery_thumbnail_path
from util.path_util import rel_path

from genhtml.w3c.html import html
from genhtml.w3c.section import section
Expand All @@ -22,62 +23,67 @@
class gallery(page):
def __init__(self):
super().__init__()
self.pictures = []
self.files = []

self.gallery_path = get_gallery_path()
self.thumbnail_path = get_gallery_thumbnail_path()
self.current_path = self.get_session("current_path")

self.set_current_path()
self.get_files()

def set_current_path(self):
if not self.current_path:
self.current_path = self.gallery_path
if not os.path.abspath(self.gallery_path) in\
os.path.abspath(self.current_path):
if not rel_path(self.current_path, self.gallery_path):
self.current_path = self.gallery_path
if not os.path.exists(self.current_path):
self.current_path = self.gallery_path
if not os.path.isdir(self.current_path):
self.current_path = self.gallery_path
if not self.json_data.get("path"):
return

self.get_path()
self.get_list()

def get_path(self):
if self.json_data and self.json_data.get("path"):
_rel_path = self.json_data.get("path")
_full = os.path.join(self.current_path, _rel_path)
self.set_session("current_path", os.path.abspath(_full))
print(_full)
print(_rel_path)
_path = self.json_data.get("path")
_full = os.path.abspath(os.path.join(self.current_path, _path))
if os.path.isdir(_full):
self.current_path = _full
self.set_session("current_path", _full)

def get_list(self):
def get_files(self):
# folder
for _folder in os.listdir(self.current_path):
_path = os.path.join(self.current_path, _folder)
if os.path.isdir(_path):
self.pictures.append(
self.files.append(
photo_builder(
src="static/images/folder.png",
path=_folder))

# images
for _file in os.listdir(self.current_path):
_path = os.path.join(self.current_path, _file)
if not is_image_file(_file):
continue

_rel_path = os.path.relpath(self.current_path, self.gallery_path)
_rel_full = os.path.join(_rel_path, _file)
_thumb_nail_path = os.path.join(self.thumbnail_path, _rel_path)
_rel_path = rel_path(self.current_path, self.gallery_path)
_rel_file = os.path.join(_rel_path, _file)
_thumb_nail_path = os.path.join(self.thumbnail_path, _rel_file)

if os.path.exists(_thumb_nail_path):
self.pictures.append(
self.files.append(
photo_builder(
src="gallery_thumbnail/" + _rel_full,
src="gallery_thumbnail/" + _rel_file,
path=_file))
else:
self.pictures.append(
self.files.append(
photo_builder(src="static/images/no_cache.png"))


def __str__(self):
_rel_path = rel_path(self.current_path, self.gallery_path)

_title_div = div(
para(class_="'title is-1 is-spaced'").set_content("Gallery"),
para(class_="'subtitle is-3'").set_content("/root"),
para(class_="'subtitle is-3'").set_content(f"/root/{_rel_path}"),
br()
)

Expand All @@ -86,8 +92,8 @@ def __str__(self):
class_="'columns is-multiline'"
)

for pic in self.pictures:
_div.append(pic)
for _file in self.files:
_div.append(_file)

_section = section(
div(_title_div, class_="container").append(_div),
Expand Down
2 changes: 1 addition & 1 deletion cnas/pages/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class page:
def __init__(self):
self.json_data = None
self.json_data = {}
if request.is_json:
self.json_data = request.get_json()

Expand Down
23 changes: 23 additions & 0 deletions cnas/util/path_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

def rel_path(path, base_path, debug=False):
"""
>>> from util.path_util import rel_path
>>> rel_path("/abc/def/xyz/", "/abc/def", True)
'xyz'
>>> print(rel_path("/abc/def/xyz/", "/ab/cd", True))
None
>>> print(rel_path("/abc/def/xyz/", "/abc", True))
def/xyz
"""
if not os.path.exists(base_path) and debug is False:
return None
if not os.path.exists(path) and debug is False:
return None
abs_path = os.path.abspath(path)
abs_base_path = os.path.abspath(base_path)
rel_path = os.path.relpath(abs_path, abs_base_path)
if ".." in rel_path:
# wrong base path
return None
return rel_path
1 change: 1 addition & 0 deletions cnas/util/system_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ def get_gallery_thumbnail_path():
if not os.path.exists(thumbnail_path):
os.makedirs(thumbnail_path, exist_ok=True)
return thumbnail_path

0 comments on commit 888e9be

Please sign in to comment.