Skip to content
Snippets Groups Projects
Verified Commit ae14a2cf authored by dos's avatar dos
Browse files

Download to .part files and rename after completion

parent 7aad7438
Branches
Tags
No related merge requests found
...@@ -38,6 +38,8 @@ def download_thread(self): ...@@ -38,6 +38,8 @@ def download_thread(self):
if not os.path.exists(rootpath): if not os.path.exists(rootpath):
os.mkdir(rootpath) os.mkdir(rootpath)
downloaded = []
while self.file_list and not self.cancel: while self.file_list and not self.cancel:
self.current_song = self.file_list.pop(0) self.current_song = self.file_list.pop(0)
self.file_no += 1 self.file_no += 1
...@@ -69,9 +71,19 @@ def download_thread(self): ...@@ -69,9 +71,19 @@ def download_thread(self):
req.close() req.close()
self.file_progress = 1.0 self.file_progress = 1.0
continue continue
else:
os.unlink(file_name)
if os.path.exists(file_name + ".part"):
stat = os.stat(file_name + ".part")
if stat[6] == total_size:
req.close()
self.file_progress = 1.0
downloaded.append(file_name)
continue
rec_size = 0 rec_size = 0
with open(file_name, "wb") as f: with open(file_name + ".part", "wb") as f:
try: try:
while True: while True:
new_data = req.raw.read(1024 * 32) new_data = req.raw.read(1024 * 32)
...@@ -81,6 +93,7 @@ def download_thread(self): ...@@ -81,6 +93,7 @@ def download_thread(self):
if self.cancel: if self.cancel:
raise Exception("Cancelled") raise Exception("Cancelled")
if not new_data: if not new_data:
downloaded.append(file_name)
break break
except Exception as e: except Exception as e:
os.unlink(file_name) os.unlink(file_name)
...@@ -92,12 +105,19 @@ def download_thread(self): ...@@ -92,12 +105,19 @@ def download_thread(self):
self.error = True self.error = True
utils.emit("downloaderror") utils.emit("downloaderror")
break break
if not self.error: if not self.error:
try: try:
for filename in downloaded:
os.rename(filename + ".part", filename)
if self.app: if self.app:
self.app.select = select.SelectView(self.app) self.app.select = select.SelectView(self.app)
except Exception as e: except Exception as e:
sys.print_exception(e) sys.print_exception(e)
self.error = True
utils.emit("downloaderror")
if not self.error:
self.finished = True self.finished = True
utils.emit("downloadfinished") utils.emit("downloadfinished")
download_lock.release() download_lock.release()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment