use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class UnzipService method extractZip.
public void extractZip(File source, File target, Artifact entry) throws Exception {
byte[] buffer = new byte[1024];
ZipInputStream zis = null;
CancelableTask cancelTask = CancelableTask.of();
int logCtr = 0;
try {
FileUtils.forceMkdir(target);
zis = new ZipInputStream(new FileInputStream(source));
ZipEntry ze = zis.getNextEntry();
if (ze == null) {
Analytics.sendEvent("invalid_zip", entry.getName());
throw new ZipException("Invalid zipentry");
}
int ctr = 0;
while (ze != null && cancelTask.isRunning()) {
String fileName = ze.getName();
File newFile = new File(target + File.separator + fileName);
ctr++;
try {
if (ze.isDirectory()) {
FileUtils.forceMkdir(newFile);
} else {
try {
FileUtils.forceMkdir(new File(newFile.getParent()));
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0 && cancelTask.isRunning()) {
fos.write(buffer, 0, len);
}
fos.close();
if (logCtr % 10 == 0) {
ProgressControlWrapper.getInstance().setProgressLabel("Installing " + newFile.getName());
double finished = (double) ((double) (((double) ctr / (double) entry.getEntries())));
ProgressControlWrapper.getInstance().increaseCurrentProgressStep(finished);
// lt 100k then give up
if (target.getFreeSpace() < 100000) {
GenericMessageDialogController.withErrorProps("Oopss...", "No space left on device!", true).showAndWait();
CancelTaskRegistry.getInstance().broadcastCancel();
}
}
} catch (Exception e) {
LOGGER.info("Unable to extract entry " + fileName);
}
}
} catch (Exception e) {
LOGGER.error("Unable to extract " + ze.getName());
}
ze = zis.getNextEntry();
}
if (cancelTask.isCancelled()) {
ProgressControlWrapper.getInstance().setProgressLabel("Cleaning out (may take a while), please wait...");
FileUtils.forceDelete(LocalStorageUtil.getSubdirOrFile(target, entry.getFolder()));
}
} finally {
try {
zis.closeEntry();
} catch (Exception e) {
LOGGER.error("Unable to close zip");
}
try {
zis.close();
} catch (Exception e) {
LOGGER.error("Unable to close zipstream");
}
}
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class SearchController method downloadFiles.
private void downloadFiles(List<SearchResultItem> selectedItems, String downloadPath) {
if (selectedItems.size() > MAX_DOWNLOADSPAN) {
GenericMessageDialogController.withErrorProps("Ooops...", "Please narrow downloadspan").showAndWait();
return;
}
CancelableTask cancelTask = CancelableTask.of();
ProgressDBController controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, getStage(), new Object[] { selectedItems.size(), cancelTask, downloadPath, "Files were installed to " + downloadPath });
ReturningTask<Void> task = () -> {
int failureCtr = 0;
for (SearchResultItem item : selectedItems) {
if (cancelTask.isCancelled()) {
break;
}
controller.setProgressLabel("Installing " + item.getName());
controller.increaseProgress();
boolean res = downloadEntryAndSubItems(item, downloadPath);
if (!res) {
failureCtr++;
}
}
if (failureCtr == 0) {
controller.progressDone();
} else {
controller.progressDone(failureCtr + " of " + selectedItems.size() + " could not be installed to " + downloadPath);
}
return null;
};
ExecutorUtil.executeAsyncWithRetry(task, 3);
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class InstallationService method installDelta.
private void installDelta(LocationAndInstallationStatus location, Artifact entry) throws Exception {
LOGGER.info("Updating " + entry.getName());
if (entry.isCreateDelta()) {
Integer latestUpdated = getVersionAsInteger(artifactsService.getLatestVersion(artifactsService.resolveDB(location, entry)));
List<ArtifactDelta> entries = entry.getDeltas().stream().filter(item -> {
return getVersionAsInteger(item.getVersion()) > latestUpdated;
}).collect(Collectors.toList());
CancelableTask cancelTask = CancelableTask.of();
for (ArtifactDelta e : entries) {
if (cancelTask.isRunning()) {
if (entry.isDynamicImport()) {
File file = installEntry(location, entry, entry.getBaseDir() + "/delta", e.getFile(), e.getVersion(), true, true);
unpackDynamic(file, location, entry, e);
if (!cancelTask.isCancelled()) {
updateDb(location, entry, e.getVersion());
} else {
break;
}
} else {
installEntry(location, entry, entry.getBaseDir() + "/delta", e.getFile(), e.getVersion());
}
} else {
ProgressControlWrapper.getInstance().setProgressLabel("Cancelling");
return;
}
}
} else {
ProgressControlWrapper.getInstance().setProgressLabel("Will delete current folder " + entry.getName() + ", please wait...");
FileUtils.deleteQuietly(LocalStorageUtil.getSubdirOrFile(location.getLocation(), entry.getAbsoluteInstallationPath()));
ProgressControlWrapper.getInstance().setProgressLabel("Installing new version");
installEntry(location, entry, entry.getBaseDir(), entry.getFile(), entry.getVersion());
}
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class InstallationService method install.
public void install(LocationAndInstallationStatus location, Artifact entry, boolean updateProgressWindow) throws Exception {
CancelableTask cancelTask = CancelableTask.of();
if (isRemoteArtifact(entry)) {
if (!artifactsService.isInstalled(location, entry)) {
Analytics.sendEvent("install_item", "artifact_" + entry.getName());
installFull(location, entry);
} else if (artifactsService.needsUpdate(location, entry)) {
Analytics.sendEvent("update_item", "artifact_" + entry.getName());
installDelta(location, entry);
}
} else {
ProgressControlWrapper.getInstance().setProgressLabel("Installing local folder!!!");
artifactsService.updateChecksumInPrivateFolder();
FileUtils.copyDirectory(new File(localDbService.getPrivateDir()), LocalStorageUtil.getSubdirOrFile(LocalStorageUtil.getSubdirOrFile(location.getLocation(), "Misc"), localDbService.getPrivateDirName()));
artifactsService.updateChecksumInInstallation(location);
}
if (updateProgressWindow) {
updateProgressDialog(cancelTask);
}
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class InstallationService method installEntry.
private File installEntry(LocationAndInstallationStatus installLocation, Artifact entry, String remotePath, String remoteName, String version, Boolean skipUnpack, Boolean updateProgressBar) throws Exception {
CancelableTask cancelTask = CancelableTask.of();
int ctr = 0;
// to keep track of the progressupdates if something fails
int progressUpdated = 0;
if (entry.getType() == ArtifactType.REMOTE) {
while (ctr < 3 && cancelTask.isRunning()) {
LOGGER.info("Installing " + entry.getName());
ProgressMessageBox.setMessage("Fetching " + entry.getName());
ProgressControlWrapper.getInstance().setProgressLabel("Fetching file " + remoteName);
File target = new File(pathService.getTmpFolderAsString() + remoteName);
if (target.exists() && ftpService.registerDownload(remoteName, DOWNLOAD_STATUS.CHECK)) {
ProgressControlWrapper.getInstance().setProgressLabel("Waiting for other worker to finish downloading " + remoteName);
ProgressControlWrapper.getInstance().increaseCurrentProgressStep(1);
while (true) {
if (!ftpService.registerDownload(remoteName, DOWNLOAD_STATUS.CHECK)) {
LOGGER.info(remoteName + " was downloaded by another thread");
break;
} else if (cancelTask.isCancelled()) {
return null;
}
Thread.sleep(500);
}
} else if (!target.exists()) {
if (!ftpService.registerDownload(remoteName, DOWNLOAD_STATUS.ADD)) {
ftpService.getFile(remotePath, remoteName, target);
if (cancelTask.isCancelled()) {
return null;
}
} else {
ProgressControlWrapper.getInstance().setProgressLabel("Waiting for other worker to finish downloading " + remoteName);
ProgressControlWrapper.getInstance().increaseCurrentProgressStep(1);
while (true) {
if (!ftpService.registerDownload(remoteName, DOWNLOAD_STATUS.CHECK)) {
LOGGER.info(remoteName + " was downloaded by another thread");
break;
} else if (cancelTask.isCancelled()) {
return null;
}
Thread.sleep(1000);
}
}
} else if (target.exists()) {
LOGGER.info("Target " + target.getAbsolutePath() + " was already downloaded");
}
if (progressUpdated < 2 && updateProgressBar) {
ProgressControlWrapper.getInstance().increaseProgress();
}
progressUpdated++;
if (cancelTask.isCancelled()) {
return null;
}
if (!skipUnpack) {
try {
ProgressMessageBox.setMessage("Installing file " + entry.getName() + "at " + LocalStorageUtil.getSubdirOrFile(installLocation.getLocation(), entry.getPrefix()));
unzipService.extractZip(target, LocalStorageUtil.getSubdirOrFile(installLocation.getLocation(), entry.getPrefix()), entry);
// unzipService.extractZip(target, installLocation.getLocation(), entry);
ProgressControlWrapper.getInstance().increaseProgress();
if (progressUpdated < 2 && updateProgressBar) {
ProgressControlWrapper.getInstance().setProgressLabel("Fetched file " + remoteName);
}
progressUpdated++;
if (!cancelTask.isCancelled()) {
updateDb(installLocation, entry, version);
}
ProgressControlWrapper.getInstance().setProgressLabel("Done");
return target;
} catch (Exception e) {
LOGGER.error("Something bad", e);
Analytics.sendEvent("invalid_zip", entry.getName());
FileUtils.forceDelete(target);
ctr++;
}
} else {
ProgressControlWrapper.getInstance().setProgressLabel("Done");
return target;
}
}
}
throw new IllegalStateException("U1nable to install entry " + remoteName);
}
Aggregations