Skip to content

Commit

Permalink
Fix issue #374 Not all downloaded images are counted in the statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas694 committed Mar 16, 2023
1 parent 8f9923b commit fd84e61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void ChangeCancellationToken(CancellationToken ct)
this.ct = ct;
}

protected virtual async Task<bool> DownloadBinaryFileAsync(string fileLocation, string url)
protected virtual async Task<(bool result, string fileLocation)> DownloadBinaryFileAsync(string fileLocation, string url)
{
try
{
Expand All @@ -90,7 +90,7 @@ protected virtual async Task<bool> DownloadBinaryFileAsync(string fileLocation,
catch (IOException ex) when ((ex.HResult & 0xFFFF) == 0x20)
{
// The process cannot access the file because it is being used by another process.", HRESULT: -2147024864 == 0xFFFFFFFF80070020
return true;
return (true, fileLocation);
}
catch (WebException webException) when (webException.Response != null)
{
Expand All @@ -106,7 +106,7 @@ protected virtual async Task<bool> DownloadBinaryFileAsync(string fileLocation,
}
}

return false;
return (false, fileLocation);
}
catch (TimeoutException timeoutException)
{
Expand All @@ -116,14 +116,14 @@ protected virtual async Task<bool> DownloadBinaryFileAsync(string fileLocation,
}
}

protected virtual async Task<bool> DownloadBinaryFileAsync(string fileLocation, string fileLocationUrlList, string url)
protected virtual async Task<(bool result, string fileLocation)> DownloadBinaryFileAsync(string fileLocation, string fileLocationUrlList, string url)
{
if (!blog.DownloadUrlList)
{
return await DownloadBinaryFileAsync(fileLocation, url);
}

return AppendToTextFile(fileLocationUrlList, url, false);
return (AppendToTextFile(fileLocationUrlList, url, false), fileLocation);
}

protected virtual bool AppendToTextFile(string fileLocation, string text, bool isJson)
Expand Down Expand Up @@ -222,8 +222,9 @@ private async Task DownloadPostAsync(TumblrPost downloadItem)
{
await DownloadPostCoreAsync(downloadItem);
}
catch
catch (Exception e)
{
Logger.Error("AbstractDownloader.DownloadPostAsync: {0}", e);
}
finally
{
Expand Down Expand Up @@ -303,7 +304,9 @@ protected virtual async Task<bool> DownloadBinaryPostAsync(TumblrPost downloadIt
string fileLocationUrlList = FileLocationLocalized(blogDownloadLocation, downloadItem.TextFileLocation);
DateTime postDate = PostDate(downloadItem);
UpdateProgressQueueInformation(Resources.ProgressDownloadImage, fileName);
if (!await DownloadBinaryFileAsync(fileLocation, fileLocationUrlList, Url(downloadItem)))
bool result;
(result, fileLocation) = await DownloadBinaryFileAsync(fileLocation, fileLocationUrlList, Url(downloadItem));
if (!result)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FileDownloader(AppSettings settings, CancellationToken ct, IWebRequestFac
// TODO: Needs a complete rewrite. Also a append/cache function for resuming incomplete files on the disk.
// Should be in separated class with support for events for downloadspeed, is resumable file?, etc.
// Should check if file is complete, else it will trigger an WebException -- 416 requested range not satisfiable at every request
public async Task<bool> DownloadFileWithResumeAsync(string url, string destinationPath)
public async Task<(bool result, string destinationPath)> DownloadFileWithResumeAsync(string url, string destinationPath)
{
long totalBytesReceived = 0;
var attemptCount = 0;
Expand All @@ -43,7 +43,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
var fileInfo = new FileInfo(destinationPath);
totalBytesReceived = fileInfo.Length;
var result = await CheckDownloadSizeAsync(url, destinationPath).TimeoutAfter(settings.TimeOut);
if (totalBytesReceived >= result.contentLength) return true;
if (totalBytesReceived >= result.contentLength) return (true, result.destinationPath);
if (destinationPath != result.destinationPath)
{
File.Delete(destinationPath);
Expand All @@ -53,7 +53,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
}
}

if (ct.IsCancellationRequested) return false;
if (ct.IsCancellationRequested) return (false, destinationPath);

var fileMode = totalBytesReceived > 0 ? FileMode.Append : FileMode.Create;

Expand All @@ -64,7 +64,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
{
attemptCount += 1;

if (attemptCount > settings.MaxNumberOfRetries) return false;
if (attemptCount > settings.MaxNumberOfRetries) return (false, destinationPath);

var requestRegistration = new CancellationTokenRegistration();

Expand Down Expand Up @@ -119,7 +119,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
{
// file in use
long win32ErrorCode = ioException.HResult & 0xFFFF;
if (win32ErrorCode == 0x21 || win32ErrorCode == 0x20) return false;
if (win32ErrorCode == 0x21 || win32ErrorCode == 0x20) return (false, destinationPath);

// retry (IOException: Received an unexpected EOF or 0 bytes from the transport stream)
}
Expand All @@ -140,7 +140,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
}
}

return true;
return (true, destinationPath);
}
finally
{
Expand Down

0 comments on commit fd84e61

Please sign in to comment.