bezzad / Downloader

Fast, cross-platform and reliable multipart downloader with asynchronous progress events for .NET applications.
MIT License
1.34k stars 202 forks source link

Resuming download starts from the begging and appends data to temp file. #49

Closed Morasiu closed 3 years ago

Morasiu commented 3 years ago

Hello :)

I've found a weird bug.

When I resume my download it starts from the beginning but appends data to the temp file.

Process

image

image

As you can see here is a file with 41MB from 50 MB file downloaded. This is saved when I closed my app.

Not when I resume and download another 30 MB, as you can see package looks correct and the temp file is the same. But progress says that it's started from the beginning.

image

image

But the temp file is now 75 MB

image

And if I let him download all file it saves all bytes in temp file into the new so... downloaded file has now 125 MB!

image

Config

var config = new DownloadConfiguration {
  CheckDiskSizeBeforeDownload = true,
  OnTheFlyDownload = false,
  TempDirectory = Config.DownloadDirectory
};

Tested file: http://ipv4.download.thinkbroadband.com/50MB.zip (ignore the name 1.mp4, it is that 50MB.zip file)

Package serialized using BinarryFormatter

Version: 2.2.6

bezzad commented 3 years ago

Hi @Morasiu I can't download from your URL http://ipv4.download.thinkbroadband.com/50MB.zip: Screenshot 2021-03-30 220342

Does this always happen? Do other files have this problem in downloading?

I asked this because some servers do not have the ability to resume downloading. That's why I checked with the IDM and it also couldn't download. There is another case, that I live in an area that does not have quality internet and may not be able to download. If you can, download your file with IDM and stop and resume it to make sure this server has no case. Thank you

Morasiu commented 3 years ago

I have a problem with all my files resuming. That file was just an example to test it correctly. I think it happens in 100% of cases.

I've installed IDM and tried downloading this file. It downloaded without any problems and I could easily stop and resume download a few times.

But...

That there is still a problem with the library creating corrupted files with more MB than the original file.

bezzad commented 3 years ago

I tested your URL with a VPN and that's correct. I ran this scenario several times in my program and there was no problem and even unit tests on different servers were not a problem. I think this problem can only occur when the package file is not saved at the right time or changes after the temp files after saved. The only right time to save the package file is after the download is cancelled. Therefore, if the values stored in the files do not match the package values, the downloader will start downloading from the begging. Now, make sure that the package is saved right after the download is cancelled, which is the best place to make sure the inside is the DownloadFileCompleted events.

Morasiu commented 3 years ago

So... I'm writing an application that can download large files over not so stable connections. Can I use that feature of the library to resume downloading after someone computer shutdown unexpectedly or after their lost connection to the internet (I guess then event DownloadFileCompleted with some errors is fired?

Also, I'm not sure if I understand correctly. Can I save the package at any moment or not? If not how can I be sure, when to save the package? I'm currently saving a package every 10s of download using the DownloadProgress event.

Morasiu commented 3 years ago

I think it's still a bug if DonwloaderService is appending data over total bytes in the downloaded temp file.

bezzad commented 3 years ago

Yes, this is a bug in appending data to the same temp files after restarting the download. But if you store the Package file after the cancelled event this problem not occur. Let me find a way to save the package file whenever you want and resume whenever you want. Because for this scenario, only this method can work. Give me some time to implement this feature.

Morasiu commented 3 years ago

Sure. I'm not in hurry. Take your time to improve that already awesome library! ❤️

bezzad commented 3 years ago

@Morasiu Please test the new version 2.2.8 for this problem. Thanks

Morasiu commented 3 years ago

Resuming now works correctly. Thanks ❤️