Use a TemporaryDirectory instead of NamedTemporaryFile to avoid opening an already open file in Windows

This commit is contained in:
Micah Lee 2022-02-09 18:19:50 -08:00
parent 1ca017d5ad
commit 33fd639f2a

View file

@ -44,8 +44,9 @@ class SendBaseModeWeb:
self.download_filesize = None self.download_filesize = None
self.zip_writer = None self.zip_writer = None
# Store the tempfile objects here, so when they're garbage collected the files are deleted # Create a temporary dir to store gzip files in
self.gzip_files = [] 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 # If autostop_sharing, only allow one download at a time
self.download_in_progress = False self.download_in_progress = False
@ -193,15 +194,15 @@ class SendBaseModeWeb:
# gzip compress the individual file, if it hasn't already been compressed # gzip compress the individual file, if it hasn't already been compressed
if use_gzip: if use_gzip:
if filesystem_path not in self.gzip_individual_files: if filesystem_path not in self.gzip_individual_files:
self.gzip_files.append( gzip_filename = os.path.join(
tempfile.NamedTemporaryFile("wb+", dir=self.common.build_tmp_dir()) self.gzip_tmp_dir.name, str(self.gzip_counter)
) )
gzip_file = self.gzip_files[-1] self.gzip_counter += 1
self._gzip_compress(filesystem_path, gzip_file.name, 6, None) self._gzip_compress(filesystem_path, gzip_filename, 6, None)
self.gzip_individual_files[filesystem_path] = gzip_file.name self.gzip_individual_files[filesystem_path] = gzip_filename
# Cleanup this temp file # 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] file_to_download = self.gzip_individual_files[filesystem_path]
filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) filesize = os.path.getsize(self.gzip_individual_files[filesystem_path])