From 5e29384a4d31b4e0ef9365857cbdc1064f2e24e4 Mon Sep 17 00:00:00 2001 From: JungJik Lee Date: Fri, 20 Sep 2024 00:58:05 +0900 Subject: [PATCH] [WIP] fix path --- cnas/pages/gallery.py | 45 +++++++++++++++---------- cnas/pages/page.py | 6 ++-- cnas/route/route.py | 4 +-- cnas/static/javascripts/cnas_gallery.js | 2 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cnas/pages/gallery.py b/cnas/pages/gallery.py index 057205c..07bebba 100644 --- a/cnas/pages/gallery.py +++ b/cnas/pages/gallery.py @@ -5,6 +5,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 genhtml.w3c.html import html from genhtml.w3c.section import section @@ -23,23 +24,32 @@ def __init__(self): super().__init__() self.pictures = [] + self.gallery_path = get_gallery_path() + self.thumbnail_path = get_gallery_thumbnail_path() + self.current_path = self.get_session("current_path") + 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): + self.current_path = self.gallery_path + + self.get_path() + self.get_list() + def get_path(self): - _gallery_path = None if self.json_data and self.json_data.get("path"): - _gallery_path = self.json_data.get("path") - else: - _gallery_path = get_gallery_path() - print(_gallery_path) - - if _gallery_path is None or not os.path.exists(_gallery_path): - return str(error("No gallery 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) def get_list(self): # folder for _folder in os.listdir(self.current_path): _path = os.path.join(self.current_path, _folder) if os.path.isdir(_path): - _div.append( + self.pictures.append( photo_builder( src="static/images/folder.png", path=_folder)) @@ -47,19 +57,20 @@ def get_list(self): # images for _file in os.listdir(self.current_path): _path = os.path.join(self.current_path, _file) - _rel_path = "" - _thumb_nail_path = "" if not is_image_file(_file): continue - if not os.path.exists(_path): - continue - if os.path.exist(_thumb_nail_path): - _div.append( + + _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) + + if os.path.exists(_thumb_nail_path): + self.pictures.append( photo_builder( - src="gallery_thumbnail/" + _rel_path, + src="gallery_thumbnail/" + _rel_full, path=_file)) else: - _div.append( + self.pictures.append( photo_builder(src="static/images/no_cache.png")) diff --git a/cnas/pages/page.py b/cnas/pages/page.py index 3e5eb25..c56a0b4 100644 --- a/cnas/pages/page.py +++ b/cnas/pages/page.py @@ -6,11 +6,11 @@ def __init__(self): if request.is_json: self.json_data = request.get_json() - def set_session(key, value): + def set_session(self, key, value): session[key] = value - def get_session(key): + def get_session(self, key): return session.get(key) - def clear_session(key): + def clear_session(self, key): session.pop(key, None) diff --git a/cnas/route/route.py b/cnas/route/route.py index 669e385..05aa424 100644 --- a/cnas/route/route.py +++ b/cnas/route/route.py @@ -16,12 +16,12 @@ def index_page() -> str: def gallery_page() -> str: return str(gallery()) - @app.route('/gallery_file/') + @app.route('/gallery_file/') def gallery_file(filename): directory = CONFIG.get('gallery_path') return send_from_directory(directory, filename) - @app.route('/gallery_thumbnail/') + @app.route('/gallery_thumbnail/') def gallery_thumbnail(filename): directory = get_gallery_thumbnail_path() return send_from_directory(directory, filename) diff --git a/cnas/static/javascripts/cnas_gallery.js b/cnas/static/javascripts/cnas_gallery.js index e6cbba8..4336cd1 100644 --- a/cnas/static/javascripts/cnas_gallery.js +++ b/cnas/static/javascripts/cnas_gallery.js @@ -10,8 +10,8 @@ $(document).ready(function(){ contentType: 'application/json', data: JSON.stringify(data), success: function(response) { - console.log(response) document.body.innerHTML = response + window.location.reload(); }, error: function(xhr, status, error) { console.log(error)