awacke1 commited on
Commit
c81afb0
1 Parent(s): 99cb0da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -29,42 +29,29 @@ if not os.path.exists("history.json"):
29
  with open("history.json", "w") as f:
30
  json.dump({}, f)
31
 
32
- import os
33
- import base64
34
- import zipfile
35
- import streamlit as st
36
-
37
- def zip_subdirs(start_dir):
38
  for subdir, dirs, files in os.walk(start_dir):
39
  if subdir != start_dir: # Skip the root directory
40
- zip_filename = os.path.join(start_dir, subdir.split(os.sep)[-1] + '.zip')
41
- with zipfile.ZipFile(zip_filename, 'w') as zipf:
42
- for file in files:
43
- file_path = os.path.join(subdir, file)
44
- zipf.write(file_path, os.path.relpath(file_path, start_dir))
45
- st.write(f"Added: {file_path}")
46
- yield zip_filename
47
-
48
- def get_zip_download_link(zip_file):
49
- # Count number of files in the zip
50
- with zipfile.ZipFile(zip_file, 'r') as zipf:
51
- num_files = len(zipf.namelist())
52
- # Get the size of the zip file
53
- zip_size = os.path.getsize(zip_file)
54
- # Convert size to a more readable format (e.g., KB, MB)
55
- if zip_size < 1024:
56
- size_str = f"{zip_size} B"
57
- elif zip_size < 1024**2:
58
- size_str = f"{zip_size / 1024:.2f} KB"
59
- else:
60
- size_str = f"{zip_size / 1024**2:.2f} MB"
61
-
62
  # Create the download link
63
  with open(zip_file, 'rb') as f:
64
  bytes = f.read()
65
  b64 = base64.b64encode(bytes).decode()
66
- link_name = os.path.basename(zip_file)
67
- href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}">{link_name} - {num_files} files, {size_str}</a>'
68
  return href
69
 
70
  @st.cache_resource
@@ -251,12 +238,20 @@ def main():
251
  os.remove(file)
252
  st.experimental_rerun()
253
 
 
254
  if st.sidebar.button("⬇️ Download All"):
255
  start_directory = '.' # Current directory
256
- for zip_file in zip_subdirs(start_directory):
257
- # Display enhanced download link with file count and size
258
- st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
259
-
 
 
 
 
 
 
 
260
  # Expander for showing URL history and download links
261
  with st.expander("URL History and Downloaded Files"):
262
  try:
 
29
  with open("history.json", "w") as f:
30
  json.dump({}, f)
31
 
32
+ def zip_subdirs(start_dir, history):
 
 
 
 
 
33
  for subdir, dirs, files in os.walk(start_dir):
34
  if subdir != start_dir: # Skip the root directory
35
+ # Find the URL associated with the subdir in history
36
+ url_key = next((url for url, path in history.items() if path == subdir), None)
37
+ if url_key:
38
+ # Use a sanitized version of the URL as the zip filename
39
+ sanitized_url = hashlib.md5(url_key.encode()).hexdigest()
40
+ zip_filename = os.path.join(start_dir, sanitized_url + '.zip')
41
+ with zipfile.ZipFile(zip_filename, 'w') as zipf:
42
+ for file in files:
43
+ file_path = os.path.join(subdir, file)
44
+ zipf.write(file_path, os.path.relpath(file_path, start_dir))
45
+ st.write(f"Added: {file_path}")
46
+ yield zip_filename, url_key
47
+
48
+ def get_zip_download_link(zip_file, url, num_files, size_str):
 
 
 
 
 
 
 
 
49
  # Create the download link
50
  with open(zip_file, 'rb') as f:
51
  bytes = f.read()
52
  b64 = base64.b64encode(bytes).decode()
53
+ link_name = os.path.basename(url) # Use URL as the link name
54
+ href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}.zip">{link_name} - {num_files} files, {size_str}</a>'
55
  return href
56
 
57
  @st.cache_resource
 
238
  os.remove(file)
239
  st.experimental_rerun()
240
 
241
+
242
  if st.sidebar.button("⬇️ Download All"):
243
  start_directory = '.' # Current directory
244
+ with open("history.json", "r") as f:
245
+ history = json.load(f)
246
+ for zip_file, url in zip_subdirs(start_directory, history):
247
+ # Get file count and size info
248
+ with zipfile.ZipFile(zip_file, 'r') as zipf:
249
+ num_files = len(zipf.namelist())
250
+ zip_size = os.path.getsize(zip_file)
251
+ size_str = f"{zip_size / 1024**2:.2f} MB" if zip_size >= 1024**2 else f"{zip_size / 1024:.2f} KB"
252
+ # Display enhanced download link with URL, file count, and size
253
+ st.sidebar.markdown(get_zip_download_link(zip_file, url, num_files, size_str), unsafe_allow_html=True)
254
+
255
  # Expander for showing URL history and download links
256
  with st.expander("URL History and Downloaded Files"):
257
  try: