awacke1 commited on
Commit
97ef61e
1 Parent(s): 41e4d4b

Update backup.py

Browse files
Files changed (1) hide show
  1. backup.py +28 -26
backup.py CHANGED
@@ -29,6 +29,30 @@ if not os.path.exists("history.json"):
29
  with open("history.json", "w") as f:
30
  json.dump({}, f)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  @st.cache_resource
34
  def create_zip_of_files(files):
@@ -213,34 +237,12 @@ def main():
213
  for file in all_files:
214
  os.remove(file)
215
  st.experimental_rerun()
216
-
217
  if st.sidebar.button("⬇️ Download All"):
218
- # Compose all_files
219
- #all_files = glob.glob("*.*")
220
- #all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
221
- #all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
222
- for subdir in history.values(): # go through each download directory
223
- #show_download_links(subdir)
224
- global file_sequence_numbers
225
- for file in list_files(subdir): # go through each file that was downloaded
226
- file_path = os.path.join(subdir, file)
227
- if file_path not in file_sequence_numbers:
228
- file_sequence_numbers[file_path] = 1
229
- else:
230
- file_sequence_numbers[file_path] += 1
231
- sequence_number = file_sequence_numbers[file_path]
232
- if os.path.isfile(file_path):
233
- st.markdown(get_download_link(file_path), unsafe_allow_html=True)
234
- show_file_operations(file_path, sequence_number)
235
- else:
236
- st.write(f"File not found: {file}")
237
- zip_file = create_zip_of_files(list_files(subdir))
238
- #with open(file, "rb") as f:
239
- # bytes = f.read()
240
- # b64 = base64.b64encode(bytes).decode()
241
- # href = f'<a href="data:file/octet-stream;base64,{b64}" download=\'{os.path.basename(file)}\'>Download: {os.path.basename(file)}</a>'
242
  st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
243
-
244
  # Expander for showing URL history and download links
245
  with st.expander("URL History and Downloaded Files"):
246
  try:
 
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):
 
237
  for file in all_files:
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: