Merge pull request #1524 from onionshare/1512_individual_files_windows

Fix individual files problem in Windows
This commit is contained in:
Micah Lee 2022-03-06 13:55:03 -08:00 committed by GitHub
commit 76b03b3fc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 21 deletions

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,12 @@ 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
self.web.cleanup_tempfiles.append(gzip_file)
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])

View File

@ -171,7 +171,6 @@ class Web:
self.socketio.init_app(self.app) self.socketio.init_app(self.app)
self.chat_mode = ChatModeWeb(self.common, self) self.chat_mode = ChatModeWeb(self.common, self)
self.cleanup_tempfiles = []
self.cleanup_tempdirs = [] self.cleanup_tempdirs = []
def get_mode(self): def get_mode(self):
@ -405,13 +404,8 @@ class Web:
""" """
self.common.log("Web", "cleanup") 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 # Clean up the tempfile.NamedTemporaryDirectory objects
for dir in self.cleanup_tempdirs: for dir in self.cleanup_tempdirs:
dir.cleanup() dir.cleanup()
self.cleanup_tempfiles = []
self.cleanup_tempdirs = [] self.cleanup_tempdirs = []

View File

@ -50,7 +50,6 @@ def web_obj(temp_dir, common_obj, mode, num_files=0):
web = Web(common_obj, False, mode_settings, mode) web = Web(common_obj, False, mode_settings, mode)
web.running = True web.running = True
web.cleanup_tempfiles == []
web.cleanup_tempdirs == [] web.cleanup_tempdirs == []
web.app.testing = True web.app.testing = True
@ -308,17 +307,13 @@ class TestWeb:
def test_cleanup(self, common_obj, temp_dir_1024): def test_cleanup(self, common_obj, temp_dir_1024):
web = web_obj(temp_dir_1024, common_obj, "share", 3) web = web_obj(temp_dir_1024, common_obj, "share", 3)
temp_file = tempfile.NamedTemporaryFile()
temp_dir = tempfile.TemporaryDirectory() temp_dir = tempfile.TemporaryDirectory()
web.cleanup_tempfiles = [temp_file]
web.cleanup_tempdirs = [temp_dir] web.cleanup_tempdirs = [temp_dir]
web.cleanup() web.cleanup()
assert os.path.exists(temp_file.name) is False
assert os.path.exists(temp_dir.name) is False assert os.path.exists(temp_dir.name) is False
assert web.cleanup_tempfiles == []
assert web.cleanup_tempdirs == [] assert web.cleanup_tempdirs == []