awacke1 commited on
Commit
6a63a73
1 Parent(s): c81afb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -32
app.py CHANGED
@@ -29,31 +29,31 @@ if not os.path.exists("history.json"):
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
58
  def create_zip_of_files(files):
59
  zip_name = "all_files.zip"
@@ -238,20 +238,11 @@ def main():
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:
@@ -264,4 +255,4 @@ def main():
264
  for subdir in history.values():
265
  show_download_links(subdir)
266
  if __name__ == "__main__":
267
- main()
 
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
  with open(zip_file, 'rb') as f:
50
  bytes = f.read()
51
  b64 = base64.b64encode(bytes).decode()
52
+ link_name = os.path.basename(zip_file)
53
+ href = f'<a href="data:file/zip;base64,{b64}" download="{link_name}">Download: {link_name}</a>'
54
  return href
55
+
56
+
57
  @st.cache_resource
58
  def create_zip_of_files(files):
59
  zip_name = "all_files.zip"
 
238
  os.remove(file)
239
  st.experimental_rerun()
240
 
 
241
  if st.sidebar.button("⬇️ Download All"):
242
  start_directory = '.' # Current directory
243
+ for zip_file in zip_subdirs(start_directory):
244
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
245
+
 
 
 
 
 
 
 
 
246
  # Expander for showing URL history and download links
247
  with st.expander("URL History and Downloaded Files"):
248
  try:
 
255
  for subdir in history.values():
256
  show_download_links(subdir)
257
  if __name__ == "__main__":
258
+ main()