From 32672077d09914579b0edea434acd4674f966a25 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Thu, 13 Feb 2025 12:17:42 +1100 Subject: [PATCH] Properly set the web 'done' state to True on loading the directory listing --- cli/onionshare_cli/__init__.py | 2 +- cli/onionshare_cli/web/send_base_mode.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cli/onionshare_cli/__init__.py b/cli/onionshare_cli/__init__.py index 0dffb493..28e710fb 100644 --- a/cli/onionshare_cli/__init__.py +++ b/cli/onionshare_cli/__init__.py @@ -515,7 +515,7 @@ def main(cwd=None): if not app.autostop_timer_thread.is_alive(): if mode == "share": # If there were no attempts to download the share, or all downloads are done, we can stop - if web.share_mode.cur_history_id == 0 or web.done: + if not web.share_mode.download_in_progress or web.share_mode.cur_history_id == 0 or web.done: print("Stopped because auto-stop timer ran out") web.stop(app.port) break diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index 3481f39a..e55841a8 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -132,6 +132,9 @@ class SendBaseModeWeb: self.set_file_info_custom(filenames, processed_size_callback) def directory_listing(self, filenames, path="", filesystem_path=None, add_trailing_slash=False): + """ + Display the front page of a share or index.html-less website, listing the files/directories. + """ # Tell the GUI about the directory listing history_id = self.cur_history_id self.cur_history_id += 1 @@ -151,6 +154,11 @@ class SendBaseModeWeb: # If filesystem_path is None, this is the root directory listing files, dirs = self.build_directory_listing(path, filenames, filesystem_path, add_trailing_slash) + + # Mark the request as done so we know we can close the share if in auto-stop mode. + self.web.done = True + + # Render and return the response. return self.directory_listing_template( path, files, dirs, breadcrumbs, breadcrumbs_leaf ) @@ -228,11 +236,11 @@ class SendBaseModeWeb: chunk_size = 102400 # 100kb fp = open(file_to_download, "rb") - done = False - while not done: + self.web.done = False + while not self.web.done: chunk = fp.read(chunk_size) if chunk == b"": - done = True + self.web.done = True else: try: yield chunk @@ -273,10 +281,10 @@ class SendBaseModeWeb: "filesize": filesize, }, ) - done = False + self.web.done = False except Exception: # Looks like the download was canceled - done = True + self.web.done = True # Tell the GUI the individual file was canceled self.web.add_request(