Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Don't save incomplete downloaded files #258

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected virtual async Task<bool> DownloadBinaryPostAsync(TumblrPost downloadIt
string fileName = FileName(downloadItem);
UpdateProgressQueueInformation(Resources.ProgressSkipFile, fileName);
}
else if (!shellService.Settings.LoadAllDatabases && blog.CheckDirectoryForFiles && (blog.CheckIfBlogShouldCheckDirectory(FileNameUrl(downloadItem), FileNameNew(downloadItem))
/*else if (!shellService.Settings.LoadAllDatabases && blog.CheckDirectoryForFiles && (blog.CheckIfBlogShouldCheckDirectory(FileNameUrl(downloadItem), FileNameNew(downloadItem))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing functionality here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invisible options are a hidden danger. I removed it to reduce testing noise since it AddFileToDb, this should probably be reverted but needs to be retested/rewritten.

|| blog.CheckIfBlogShouldCheckDirectory(FileName(downloadItem), FileNameNew(downloadItem))))
{
string fileName = AddFileToDb(downloadItem);
Expand All @@ -299,7 +299,7 @@ protected virtual async Task<bool> DownloadBinaryPostAsync(TumblrPost downloadIt
{
string fileName = AddFileToDb(downloadItem);
UpdateProgressQueueInformation(Resources.ProgressSkipFile, fileName);
}
}*/
else
{
string blogDownloadLocation = blog.DownloadLocation();
Expand All @@ -310,6 +310,7 @@ protected virtual async Task<bool> DownloadBinaryPostAsync(TumblrPost downloadIt
UpdateProgressQueueInformation(Resources.ProgressDownloadImage, fileName);
if (!await DownloadBinaryFileAsync(fileLocation, fileLocationUrlList, Url(downloadItem)))
{
RemoveFileFromDb(FileNameUrl(downloadItem)); // TODO: Temp plan, the addition should happen only when the download is successful.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be a way to do it. But keep in mind that AddFileToDb is called early, because this url shall not be downloaded from another thread while this one is still working on it, and it also determines the final filename in case of using filename templates.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, I have no better way to improve/staticize the AddFileToDb function. I'm not sure what you mean, but this PR is a proposal and not a final solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood that it's a draft and work in progress (WIP). Everything is ok, I only wanted to give more information as input.

return false;
}

Expand Down Expand Up @@ -352,6 +353,10 @@ protected string AddFileToDb(TumblrPost downloadItem)
}
return files.AddFileToDb(FileNameUrl(downloadItem), downloadItem.Filename, AppendTemplate);
}
protected void RemoveFileFromDb(string fileNameUrl)
{
files.RemoveFileFromDb(fileNameUrl);
}

public bool CheckIfFileExistsInDB(string filenameUrl)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati

if (ct.IsCancellationRequested) return false;

var fileMode = totalBytesReceived > 0 ? FileMode.Append : FileMode.Create;
var fileMode = File.Exists(destinationPath) ? FileMode.Append : FileMode.Create;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's another (unnecessary) filesystem access. But besides that I just see an error and an optimization possibility in the previous block (old code).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the previous code is working fine for a created file of 0 bytes. Testcase may be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was unclear, I meant the lines 41-54 of the old code (not your changes).
I would try to avoid another filesystem access (File.Exists). But as said WIP...


var fileStream = new FileStream(destinationPath, fileMode, FileAccess.Write, FileShare.Read, bufferSize, true);
try
Expand Down Expand Up @@ -131,6 +131,8 @@ public async Task<bool> DownloadFileWithResumeAsync(string url, string destinati
}
else
{
fileStream?.Dispose();
File.Delete(destinationPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not delete the file in all cases. What about a video download that timeouts short before the end? It prevents resume functionality.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely enough, as of now, I don't see resume to be working, probably due to skipping any existing files in filesystem or DB (index).

throw;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/TumblThree/TumblThree.Domain/Models/Files/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public void AddFileToDb(string fileNameUrl, string fileName)
isDirty = true;
}
}
public void RemoveFileFromDb(string fileNameUrl)
{
lock (_lockList)
{
entries.RemoveWhere(i => i.Link == fileNameUrl);
isDirty = true;
}
}

public string AddFileToDb(string fileNameUrl, string fileName, string appendTemplate)
{
Expand Down
1 change: 1 addition & 0 deletions src/TumblThree/TumblThree.Domain/Models/Files/IFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IFiles : INotifyPropertyChanged
void AddFileToDb(string fileNameUrl, string fileName);

string AddFileToDb(string fileNameUrl, string fileName, string appendTemplate);
void RemoveFileFromDb(string fileNameUrl);

bool CheckIfFileExistsInDB(string filenameUrl);

Expand Down