Skip to content

Commit

Permalink
updated Downloader with ifUpdateAvailable (#328)
Browse files Browse the repository at this point in the history
If the download location is specified (to()), the new (ifUpdateAvailable()) method can be used to trigger a download only if the resource available via the given URL is newer than the local file.

It is currently used to avoid repeated downloads of the available Wine versions json.
  • Loading branch information
plata authored Aug 19, 2017
1 parent 7d73400 commit d86d4bf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 8 additions & 3 deletions Engines/Wine/Engine/Object/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,18 @@ Wine.prototype.kill = function () {

/**
*
* @returns {Downloader}
* @returns available Wine versions
*/
Wine.prototype.getAvailableVersions = function () {
return new Downloader()
var versionsFile = this._wineEnginesDirectory + "/availableVersions.json";
touch(versionsFile);
new Downloader()
.wizard(this._wizard)
.url(this._wineWebServiceUrl)
.get()
.to(versionsFile)
.onlyIfUpdateAvailable(true)
.get();
return cat(versionsFile);
};

/**
Expand Down
6 changes: 6 additions & 0 deletions Utils/Functions/Filesystem/Files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ var remove = function(filePath) {
return Bean("fileUtilities").remove(new java.io.File(filePath));
};

var touch = function(filePath) {
if (!fileExists(filePath)) {
Bean("fileUtilities").writeToFile(new java.io.File(filePath), "");
}
};

var writeToFile = function(filePath, content) {
Bean("fileUtilities").writeToFile(new java.io.File(filePath), content);
};
Expand Down
19 changes: 16 additions & 3 deletions Utils/Functions/Net/Download/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var Downloader = function () {
that._localFile = localFile;
return that;
};
that.onlyIfUpdateAvailable = function (onlyIfUpdateAvailable) {
that._onlyIfUpdateAvailable = onlyIfUpdateAvailable;
return that;
}
that.get = function () {
if (!that._message) {
that._message = tr("Please wait while {0} is downloaded ...", that._fetchFileNameFromUrl(that._url));
Expand All @@ -41,9 +45,18 @@ var Downloader = function () {
var progressBar = that._wizard.progressBar(that._message);
}

if (that._onlyIfUpdateAvailable) {
if (!that._downloader.isUpdateAvailable(that._localFile, that._url)) {
print(that._localFile + " already up-to-date.");
return;
}
}

if (that._localFile) {
that._downloader.get(that._url, that._localFile, function (progressEntity) {
progressBar.accept(progressEntity);
if (progressBar) {
progressBar.accept(progressEntity);
}
});

if (that._checksum) {
Expand All @@ -64,10 +77,10 @@ var Downloader = function () {
}
} else {
return that._downloader.get(that._url, function (progressEntity) {
if(progressBar) {
if (progressBar) {
progressBar.accept(progressEntity);
}
});
}
}
};
};

0 comments on commit d86d4bf

Please sign in to comment.