From 33fd639f2ad844031ef664107ec13d234fd7b6b2 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 9 Feb 2022 18:19:50 -0800 Subject: [PATCH] Use a TemporaryDirectory instead of NamedTemporaryFile to avoid opening an already open file in Windows --- cli/onionshare_cli/web/send_base_mode.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index e608298b..2e906081 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,15 @@ 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 + self.gzip_counter += 1 + self._gzip_compress(filesystem_path, gzip_filename, 6, None) + self.gzip_individual_files[filesystem_path] = gzip_filename # Cleanup this temp file - self.web.cleanup_tempfiles.append(gzip_file) + self.web.cleanup_tempfiles.append(gzip_filename) file_to_download = self.gzip_individual_files[filesystem_path] filesize = os.path.getsize(self.gzip_individual_files[filesystem_path])