Skip to content

improve already downloaded contribution file check #2865

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -57,17 +57,18 @@ public File download(DownloadableContribution contribution,
// Ensure the existence of staging folder
stagingFolder.mkdirs();

// Need to download or resume downloading?
if (!outputFile.isFile() || (outputFile.length() < contribution.getSize())) {
download(url, outputFile, progress, statusText);
if (fileAlreadyDownloaded(outputFile, contribution)) {
contribution.setDownloaded(true);
contribution.setDownloadedFile(outputFile);
return outputFile;
}

download(url, outputFile, progress, statusText);

// Test checksum
progress.setStatus(_("Verifying archive integrity..."));
onProgress(progress);
String checksum = contribution.getChecksum();
String algo = checksum.split(":")[0];
if (!FileHash.hash(outputFile, algo).equals(checksum)) {
if (checksumMatches(outputFile, contribution)) {
throw new Exception(_("CRC doesn't match. File is corrupted."));
}

Expand All @@ -76,6 +77,29 @@ public File download(DownloadableContribution contribution,
return outputFile;
}

private boolean checksumMatches(File outputFile,
DownloadableContribution contribution)
throws Exception {
final String checksum = contribution.getChecksum();
final String algo = checksum.split(":")[0];

return FileHash.hash(outputFile, algo).equals(checksum);
}

private boolean fileAlreadyDownloaded
(File outputFile,
DownloadableContribution contribution) throws Exception {
if (!outputFile.isFile()) {
return false;
}

if (outputFile.length() != contribution.getSize()) {
return false;
}

return checksumMatches(outputFile, contribution);
}

public void download(URL url, File tmpFile, final Progress progress,
final String statusText) throws Exception {
FileDownloader downloader = new FileDownloader(url, tmpFile);
Expand Down