Browse Source

code for on-the-fly zip creation added

Markus Spring 1 year ago
parent
commit
0379130aaf
1 changed files with 30 additions and 1 deletions
  1. 30 1
      create_lightgallery.py

+ 30 - 1
create_lightgallery.py

@@ -41,6 +41,7 @@ html = Template("""<html>
         <script src="/res/plugins/autoplay/lg-autoplay.min.js"></script>
         <script src="/res/plugins/thumbnail/lg-thumbnail.min.js"></script>
         <script src="/res/plugins/zoom/lg-zoom.min.js"></script>
+        <script src="/res/js/zipfile_creation.js" defer></script>
         <style>
          #header {
              background-image: url("header/header.jpg");
@@ -51,6 +52,10 @@ html = Template("""<html>
         <div id="header">
             <h1>$title</h1>
             <div id="nav">$navigation</div>
+            <div id="zip">
+              <button id="startButton" style="width:100%" data-dir2zip="$dir_to_zip">Zipfile erstellen</button><br />
+              <div id="zipstatus" style="width:100%; text-align:center;"></div>
+            </div>
         </div>
         <div id="outer_lightgallery">
         <div id="lightgallery">
@@ -69,6 +74,30 @@ html = Template("""<html>
 </html>
 """)
 
+def find_file_upwards(filename, max_levels=4):
+    current_dir = os.getcwd()
+    for level in range(max_levels + 1):  # +1 to include current directory
+        file_path = os.path.join(current_dir, "res", "js", filename)
+        if os.path.isfile(file_path):
+            return current_dir, level
+        parent_dir = os.path.dirname(current_dir)
+        if parent_dir == current_dir:  # Reached root directory
+            break
+        current_dir = parent_dir
+    return None, None
+
+def get_updirs_to_webroot(filename):
+    try:
+        result_dir, levels = find_file_upwards(filename)
+        path_components = []
+        current = os.getcwd()
+        for _ in range(levels):
+            path_components.insert(0, os.path.basename(current))
+            current = os.path.dirname(current)
+        return "/"+"/".join(path_components)
+    except:
+        print(f"could not upwards find {filename}")
+        
 def write_command_to_history(max_entries=20):
     # Define the history file path
     history_file = os.path.join(os.getcwd(), '.history')
@@ -263,7 +292,7 @@ def create_index_html(indexdir, opts):
     
     title = read_title_file() or opts.title or os.path.basename(os.path.normpath(os.getcwd()))
     with open('index.html', 'w') as f:
-        f.write(html.substitute(images=images, title=title, navigation='&nbsp;&nbsp;&nbsp;&nbsp;'.join([backward, up, forward]) ))
+        f.write(html.substitute(images=images, title=title, navigation='&nbsp;&nbsp;&nbsp;&nbsp;'.join([backward, up, forward]), dir_to_zip=get_updirs_to_webroot("lightgallery.min.js") ))
     write_config(opts)
     os.chdir(startdir)