Skip to content

Commit

Permalink
Do not cache the core or the library because are too big
Browse files Browse the repository at this point in the history
and will be downloaded only one time
  • Loading branch information
Mattia Bertorello committed Jul 11, 2019
1 parent dde5668 commit a8c7184
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public DownloadableContributionsDownloader(File _stagingFolder) {
stagingFolder = _stagingFolder;
}

public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception {
return download(contribution, progress, statusText, progressListener, false);
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener, boolean allowCache) throws Exception {
return download(contribution, progress, statusText, progressListener, false, allowCache);
}

public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener, boolean noResume, boolean allowCache) throws Exception {
URL url = new URL(contribution.getUrl());
Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName());

Expand All @@ -75,7 +75,7 @@ public File download(DownloadableContribution contribution, Progress progress, f
while (true) {
// Need to download or resume downloading?
if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) {
download(url, outputFile.toFile(), progress, statusText, progressListener, noResume);
download(url, outputFile.toFile(), progress, statusText, progressListener, noResume, allowCache);
downloaded = true;
}

Expand Down Expand Up @@ -121,11 +121,11 @@ private boolean hasChecksum(DownloadableContribution contribution) {
return algo != null && !algo.isEmpty();
}

public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
download(url, tmpFile, progress, statusText, progressListener, false);
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean allowCache) throws Exception {
download(url, tmpFile, progress, statusText, progressListener, false, allowCache);
}

public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean noResume, boolean allowCache) throws Exception {
FileDownloader downloader = new FileDownloader(url, tmpFile);
downloader.addObserver((o, arg) -> {
FileDownloader me = (FileDownloader) o;
Expand All @@ -139,7 +139,7 @@ public void download(URL url, File tmpFile, Progress progress, String statusText
progress.setProgress(me.getProgress());
progressListener.onProgress(progress);
});
downloader.download(noResume);
downloader.download(noResume, allowCache);
if (!downloader.isCompleted()) {
throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError());
}
Expand All @@ -157,7 +157,7 @@ public void downloadIndexAndSignature(MultiStepProgress progress, URL packageInd
File packageIndexTemp = File.createTempFile(indexFileName, ".tmp");
try {
// Download package index
download(packageIndexUrl, packageIndexTemp, progress, statusText, progressListener, true);
download(packageIndexUrl, packageIndexTemp, progress, statusText, progressListener, true, true);

if (verifyDomain(packageIndexUrl)) {
URL signatureUrl = new URL(packageIndexUrl.toString() + ".sig");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ public GZippedJsonDownloader(DownloadableContributionsDownloader downloader, URL
this.gzippedUrl = gzippedUrl;
}

public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean allowCache) throws Exception {
File gzipTmpFile = null;
try {
String tmpFileName = FilenameUtils.getName(new URL(Constants.LIBRARY_INDEX_URL_GZ).getPath());
gzipTmpFile = File.createTempFile(tmpFileName, GzipUtils.getCompressedFilename(tmpFile.getName()));
// remove eventual leftovers from previous downloads
Files.deleteIfExists(gzipTmpFile.toPath());

new JsonDownloader(downloader, gzippedUrl).download(gzipTmpFile, progress, statusText, progressListener);
new JsonDownloader(downloader, gzippedUrl).download(gzipTmpFile, progress, statusText, progressListener, allowCache);
decompress(gzipTmpFile, tmpFile);
} catch (Exception e) {
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText, progressListener);
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText, progressListener, allowCache);
} finally {
if (gzipTmpFile != null) {
Files.deleteIfExists(gzipTmpFile.toPath());
Expand Down
4 changes: 2 additions & 2 deletions arduino-core/src/cc/arduino/contributions/JsonDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public JsonDownloader(DownloadableContributionsDownloader downloader, URL url) {
this.url = url;
}

public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean allowCache) throws Exception {
try {
downloader.download(url, tmpFile, progress, statusText, progressListener);
downloader.download(url, tmpFile, progress, statusText, progressListener, allowCache);
} catch (InterruptedException e) {
// Download interrupted... just exit
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
final String statusText = tr("Downloading libraries index...");
try {
GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader(downloader, libraryURL, new URL(Constants.LIBRARY_INDEX_URL_GZ));
gZippedJsonDownloader.download(libraryIndexTemp, progress, statusText, progressListener);
gZippedJsonDownloader.download(libraryIndexTemp, progress, statusText, progressListener, true);
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
Expand Down Expand Up @@ -118,7 +118,7 @@ public synchronized void install(ContributedLibrary lib, Optional<ContributedLib

// Step 1: Download library
try {
downloader.download(lib, progress, I18n.format(tr("Downloading library: {0}"), lib.getName()), progressListener);
downloader.download(lib, progress, I18n.format(tr("Downloading library: {0}"), lib.getName()), progressListener, false);
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public synchronized List<String> install(ContributedPlatform contributedPlatform
// Download all
try {
// Download platform
downloader.download(contributedPlatform, progress, tr("Downloading boards definitions."), progressListener);
downloader.download(contributedPlatform, progress, tr("Downloading boards definitions."), progressListener, false);
progress.stepDone();

// Download tools
int i = 1;
for (ContributedTool tool : tools) {
String msg = format(tr("Downloading tools ({0}/{1})."), i, tools.size());
i++;
downloader.download(tool.getDownloadableContribution(platform), progress, msg, progressListener);
downloader.download(tool.getDownloadableContribution(platform), progress, msg, progressListener, false);
progress.stepDone();
}
} catch (InterruptedException e) {
Expand Down
18 changes: 9 additions & 9 deletions arduino-core/src/cc/arduino/utils/network/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,12 @@ public void setStatus(Status status) {
notifyObservers();
}

public void download() throws InterruptedException {
download(false);
}

public void download(boolean noResume) throws InterruptedException {
public void download(boolean noResume, boolean allowCache) throws InterruptedException {
if ("file".equals(downloadUrl.getProtocol())) {
saveLocalFile();
} else {
downloadFile(noResume);
downloadFile(noResume, allowCache);
}
}

Expand All @@ -139,7 +136,7 @@ private void saveLocalFile() {
}
}

private void downloadFile(boolean noResume) throws InterruptedException {
private void downloadFile(boolean noResume, boolean allowCache) throws InterruptedException {
RandomAccessFile randomAccessOutputFile = null;

try {
Expand Down Expand Up @@ -220,9 +217,12 @@ private void downloadFile(boolean noResume) throws InterruptedException {
}
// Set the cache whe it finish to download the file
IOUtils.closeQuietly(randomAccessOutputFile);
if (fileCached.isPresent()) {
if (fileCached.isPresent() && allowCache) {
fileCached.get().updateCacheFile(outputFile);
}
if (!allowCache) {
log.info("The file {} was not cached because allow cache is false", downloadUrl);
}
setStatus(Status.COMPLETE);
} catch (InterruptedException e) {
setStatus(Status.CANCELLED);
Expand All @@ -232,12 +232,12 @@ private void downloadFile(boolean noResume) throws InterruptedException {
} catch (SocketTimeoutException e) {
setStatus(Status.CONNECTION_TIMEOUT_ERROR);
setError(e);
log.error(e);
log.error("The request went in socket timeout", e);

} catch (Exception e) {
setStatus(Status.ERROR);
setError(e);
log.error(e);
log.error("The request stop", e);

} finally {
IOUtils.closeQuietly(randomAccessOutputFile);
Expand Down

0 comments on commit a8c7184

Please sign in to comment.