diff options
author | Micah Lee <micah@micahflee.com> | 2022-03-06 13:55:03 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-06 13:55:03 -0800 |
commit | 76b03b3fc3b9a09a375ee83f31b322f87c4a8cff (patch) | |
tree | 40ecf823fcbb2ec2ce256d35d8e1057c46359760 | |
parent | acb31117e41c8445384aff7f949e1ffd57630755 (diff) | |
parent | 5f2b3b58aa4b73ec7af7189065d226bdc49081de (diff) | |
download | onionshare-76b03b3fc3b9a09a375ee83f31b322f87c4a8cff.tar.gz onionshare-76b03b3fc3b9a09a375ee83f31b322f87c4a8cff.zip |
Merge pull request #1524 from onionshare/1512_individual_files_windows
Fix individual files problem in Windows
-rw-r--r-- | cli/onionshare_cli/web/send_base_mode.py | 18 | ||||
-rw-r--r-- | cli/onionshare_cli/web/web.py | 6 | ||||
-rw-r--r-- | cli/tests/test_cli_web.py | 5 |
3 files changed, 8 insertions, 21 deletions
diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index e608298b..d690c98d 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -44,8 +44,9 @@ class SendBaseModeWeb: self.download_filesize = None self.zip_writer = None - # Store the tempfile objects here, so when they're garbage collected the files are deleted - self.gzip_files = [] + # Create a temporary dir to store gzip files in + self.gzip_tmp_dir = tempfile.TemporaryDirectory(dir=self.common.build_tmp_dir()) + self.gzip_counter = 0 # If autostop_sharing, only allow one download at a time self.download_in_progress = False @@ -193,15 +194,12 @@ class SendBaseModeWeb: # gzip compress the individual file, if it hasn't already been compressed if use_gzip: if filesystem_path not in self.gzip_individual_files: - self.gzip_files.append( - tempfile.NamedTemporaryFile("wb+", dir=self.common.build_tmp_dir()) + gzip_filename = os.path.join( + self.gzip_tmp_dir.name, str(self.gzip_counter) ) - gzip_file = self.gzip_files[-1] - self._gzip_compress(filesystem_path, gzip_file.name, 6, None) - self.gzip_individual_files[filesystem_path] = gzip_file.name - - # Cleanup this temp file - self.web.cleanup_tempfiles.append(gzip_file) + self.gzip_counter += 1 + self._gzip_compress(filesystem_path, gzip_filename, 6, None) + self.gzip_individual_files[filesystem_path] = gzip_filename file_to_download = self.gzip_individual_files[filesystem_path] filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) diff --git a/cli/onionshare_cli/web/web.py b/cli/onionshare_cli/web/web.py index 64844b5c..fdbed567 100644 --- a/cli/onionshare_cli/web/web.py +++ b/cli/onionshare_cli/web/web.py @@ -171,7 +171,6 @@ class Web: self.socketio.init_app(self.app) self.chat_mode = ChatModeWeb(self.common, self) - self.cleanup_tempfiles = [] self.cleanup_tempdirs = [] def get_mode(self): @@ -405,13 +404,8 @@ class Web: """ self.common.log("Web", "cleanup") - # Close all of the tempfile.NamedTemporaryFile - for file in self.cleanup_tempfiles: - file.close() - # Clean up the tempfile.NamedTemporaryDirectory objects for dir in self.cleanup_tempdirs: dir.cleanup() - self.cleanup_tempfiles = [] self.cleanup_tempdirs = [] diff --git a/cli/tests/test_cli_web.py b/cli/tests/test_cli_web.py index f6076ef9..335c3a1a 100644 --- a/cli/tests/test_cli_web.py +++ b/cli/tests/test_cli_web.py @@ -50,7 +50,6 @@ def web_obj(temp_dir, common_obj, mode, num_files=0): web = Web(common_obj, False, mode_settings, mode) web.running = True - web.cleanup_tempfiles == [] web.cleanup_tempdirs == [] web.app.testing = True @@ -308,17 +307,13 @@ class TestWeb: def test_cleanup(self, common_obj, temp_dir_1024): web = web_obj(temp_dir_1024, common_obj, "share", 3) - temp_file = tempfile.NamedTemporaryFile() temp_dir = tempfile.TemporaryDirectory() - web.cleanup_tempfiles = [temp_file] web.cleanup_tempdirs = [temp_dir] web.cleanup() - assert os.path.exists(temp_file.name) is False assert os.path.exists(temp_dir.name) is False - assert web.cleanup_tempfiles == [] assert web.cleanup_tempdirs == [] |