Fix permission denied error in Windows, which was introduced using tempfile.NamedTemporaryFile, by using a TemporaryDirectory instead

This commit is contained in:
Micah Lee 2021-12-21 14:28:23 -08:00
parent f0d40beb77
commit 7e55e8e816
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73

View File

@ -189,7 +189,7 @@ class ShareModeWeb(SendBaseModeWeb):
# and serve that
use_gzip = self.should_use_gzip()
if use_gzip:
file_to_download = self.gzip_file.name
file_to_download = self.gzip_filename
self.filesize = self.gzip_filesize
etag = self.gzip_etag
else:
@ -492,20 +492,21 @@ class ShareModeWeb(SendBaseModeWeb):
self.download_etag = make_etag(f)
# Compress the file with gzip now, so we don't have to do it on each request
self.gzip_file = tempfile.NamedTemporaryFile(
"wb+", dir=self.common.build_tmp_dir()
self.gzip_tmp_dir = tempfile.TemporaryDirectory(
dir=self.common.build_tmp_dir()
)
self.gzip_filename = os.path.join(self.gzip_tmp_dir.name, "file.gz")
self._gzip_compress(
self.download_filename, self.gzip_file.name, 6, processed_size_callback
self.download_filename, self.gzip_filename, 6, processed_size_callback
)
self.gzip_filesize = os.path.getsize(self.gzip_file.name)
with open(self.gzip_file.name, "rb") as f:
self.gzip_filesize = os.path.getsize(self.gzip_filename)
with open(self.gzip_filename, "rb") as f:
self.gzip_etag = make_etag(f)
self.is_zipped = False
# Cleanup this tempfile
self.web.cleanup_tempfiles.append(self.gzip_file)
self.web.cleanup_tempdirs.append(self.gzip_tmp_dir)
else:
# Zip up the files and folders