use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class SearchViewFilesController method downloadFilesAsync.
private void downloadFilesAsync(List<ContentEntry> selectedItems, String customPath) {
CancelableTask cancelTask = CancelableTask.of();
ProgressDBController controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, getStage(), new Object[] { selectedItems.size(), cancelTask, customPath + searchedItem.getCategoryShortName() + "/" + searchedItem.getName(), "Files were installed to " + customPath + searchedItem.getName() });
ReturningTask<Void> task = () -> {
int failureCtr = 0;
for (ContentEntry selectedItem : selectedItems) {
if (cancelTask.isCancelled()) {
break;
}
try {
downloadService.downloadItem(searchedItem.getId(), searchedItem.getCategory(), selectedItem.getId(), searchedItem.getName(), selectedItem.getName(), searchedItem.getCategoryShortName(), FILE_ACTION.DOWNLOAD, customPath);
} catch (Exception e) {
failureCtr++;
}
controller.setProgressLabel("Installing " + selectedItem.getName());
controller.increaseProgress();
}
if (failureCtr == 0) {
controller.progressDone();
} else {
controller.progressDone(failureCtr + " of " + selectedItems.size() + " could not be installed to " + customPath);
}
return null;
};
ExecutorUtil.executeAsyncWithRetry(task, 3);
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class SidifyExportPlaylistController method export.
public void export() throws Exception {
int idx = locations.getSelectionModel().getSelectedIndex();
List<String> exportLocations = new ArrayList<>();
if (idx != 0) {
exportLocations.addAll(Collections.singletonList(locations.getSelectionModel().getSelectedItem()));
} else {
exportLocations.addAll(locations.getItems().stream().filter(item -> !item.equals(ALL_LOCATIONS)).collect(Collectors.toList()));
}
int items = playlists.stream().map(item -> playlistService.getSongsForPlaylist(item)).map(item -> item.size()).mapToInt(i -> i.intValue()).sum() * exportLocations.size();
CancelableTask cancelTask = CancelableTask.of();
ProgressDBController controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, NullWindowOwner.of(), new Object[] { items, cancelTask, exportLocations.get(0) + MUSIC_SIDIFY, "Playlist exported to <location>/Music/Sidify" });
NonReturningTask t = () -> {
try {
for (PlaylistInfo playlist : playlists) {
List<PlaylistEntry> songsForPlaylist = playlistService.getSongsForPlaylist(playlist);
int padding = Integer.valueOf(String.valueOf(songsForPlaylist.size()).length());
for (String dir : exportLocations) {
File exportDir = new File(dir + MUSIC_SIDIFY + playlist.getName());
FileUtils.deleteQuietly(exportDir);
FileUtils.forceMkdir(exportDir);
int ctr = 1;
for (PlaylistEntry entry : playlistService.getSongsForPlaylist(playlist)) {
String pos = String.format("%0" + padding + "d", ctr);
try {
controller.increaseProgress();
controller.setProgressLabel("Exporting " + entry.getNameMasked());
FileUtils.copyFile(downloadService.getSid(entry), new File(exportDir + "/" + pos + "_" + entry.getNameMasked() + ".sid"));
ctr++;
} catch (Exception e) {
e.printStackTrace();
}
if (cancelTask.isCancelled()) {
FileUtils.deleteQuietly(exportDir);
break;
}
}
}
if (cancelTask.isCancelled()) {
break;
}
}
} finally {
controller.progressDone();
}
};
ExecutorUtil.executeAsync(t);
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class FTPService method getFileInternal.
private File getFileInternal(String remote, String file, File target, boolean updateGui) throws Exception {
LOGGER.info("Fetching file " + file);
CancelableTask cancelTask = CancelableTask.of();
boolean isUpgrading = GlobalRepoService.getInstance().contains("upgrading");
FTPClient client = getNewClient();
try {
String remotePath = remote + "/" + file;
long filesize = Arrays.stream(client.listFiles(remote)).filter(item -> item.getName().equals(file)).findFirst().get().getSize();
long ctr = 0;
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(target));
InputStream inputStream = client.retrieveFileStream(remotePath);
byte[] bytesArray = new byte[8192];
int bytesRead = -1;
int updateGuiCtr = 0;
while ((bytesRead = inputStream.read(bytesArray)) != -1 && cancelTask.isRunning()) {
outputStream2.write(bytesArray, 0, bytesRead);
ctr += bytesRead;
if (updateGui && !isUpgrading) {
if (updateGuiCtr % 50 == 0) {
double finished = (double) ((double) (((double) ctr / (double) filesize)));
ProgressControlWrapper.getInstance().setProgressLabel("Fetching file " + target.getName() + " ( " + df2.format((double) (finished * 100)).replace(",", ".") + "% )");
ProgressControlWrapper.getInstance().increaseCurrentProgressStep(finished);
updateGuiCtr = 0;
}
updateGuiCtr++;
}
}
outputStream2.close();
inputStream.close();
if (!cancelTask.isCancelled()) {
client.completePendingCommand();
if (updateGui) {
ProgressControlWrapper.getInstance().setProgressLabel("Downloaded file " + target.getName(), true);
}
} else {
if (updateGui) {
ProgressControlWrapper.getInstance().setProgressLabel("Cleaning up, please wait");
}
FileUtils.forceDelete(target);
}
return target;
} finally {
registerDownload(file, DOWNLOAD_STATUS.REMOVE);
close(client);
}
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class InstallationService method unpackDynamic.
private void unpackDynamic(File content, LocationAndInstallationStatus location, Artifact entry, ArtifactDelta delta) throws Exception {
CancelableTask cancelTask = CancelableTask.of();
LocalStorageUtil.createDir(pathService.getTmpDynFolder());
unzipService.extractZip(content, pathService.getTmpDynFolder(), entry);
if (cancelTask.isCancelled()) {
return;
}
File[] files = LocalStorageUtil.getSubdirOrFile(pathService.getTmpDynFolder(), entry.getFolder()).listFiles();
File base = LocalStorageUtil.getSubdirOrFile(location.getLocation(), entry.getPrefix() + "/" + entry.getFolder());
Optional<File> releaseLogFile = Arrays.stream(files).filter(item -> item.getName().equals("releaselog.json")).findFirst();
List<ReleaseInfo> releaseInfoList = null;
List<ReleaseInfo> matchedReleaseInfoList = new ArrayList<>();
if (releaseLogFile.isPresent()) {
releaseInfoList = Support.readList(ReleaseInfo.class, releaseLogFile.get()).stream().collect(Collectors.toList());
for (ReleaseInfo item : releaseInfoList) {
String p = item.getPath();
int start = p.indexOf("/", p.indexOf("/") + 1);
item.setPath(entry.getPrefix() + "/" + entry.getFolder() + "/" + p.substring(start + 1));
item.setStrippedPath(entry.getFolder() + "/" + p.substring(start + 1));
}
}
for (File f : files) {
if (f.isDirectory()) {
String dirname = f.getName().toUpperCase();
char firstChar = dirname.charAt(0);
boolean isDigit = Character.isDigit(firstChar);
boolean isAlpha = Character.isAlphabetic(firstChar);
String target = findDestination(firstChar, dirname, base);
if (target != null) {
File t = null;
if (isDigit) {
t = new File(base.getAbsolutePath() + "/0-9/" + target + "/" + f.getName());
} else if (isAlpha) {
t = new File(base.getAbsolutePath() + "/" + String.valueOf(firstChar) + "/" + target + "/" + f.getName());
} else {
t = new File(base.getAbsolutePath() + "/___MISC___/" + target + "/" + f.getName());
}
FileUtils.forceMkdir(t);
FileUtils.copyDirectory(f, t);
if (releaseInfoList != null) {
ReleaseInfo matchedReleaseInfo = null;
for (ReleaseInfo item : releaseInfoList) {
File[] subdirs = f.listFiles(File::isDirectory);
for (File dir : subdirs) {
if (dir.getPath().contains(item.getStrippedPath())) {
matchedReleaseInfo = item;
break;
}
}
}
if (matchedReleaseInfo != null) {
matchedReleaseInfo.setPath(t.getAbsolutePath().substring(base.getAbsolutePath().length() + 1));
matchedReleaseInfoList.add(matchedReleaseInfo);
}
}
ProgressControlWrapper.getInstance().setProgressLabel("Moved " + f.getName() + " to " + t.getAbsolutePath());
if (cancelTask.isCancelled()) {
break;
}
} else {
LOGGER.info("Unable to install " + dirname);
}
}
}
ProgressControlWrapper.getInstance().increaseProgress();
if (releaseLogFile.isPresent()) {
File t = new File(base.getAbsolutePath() + "/releaselog.json");
LOGGER.info("Flushing rebuildt releaselog.json to " + t.getAbsolutePath());
Support.flush(matchedReleaseInfoList, t);
}
}
use of se.light.assembly64.model.CancelableTask in project assembly64fx by freabemania.
the class InstallationService method downloadLatestFromDatabase.
public void downloadLatestFromDatabase() {
if (controller != null) {
controller.getStage().show();
return;
}
long nofDays = 0;
File latestDateFile = new File(Support.removeEndingSlash(userService.getPrimaryInstallation().getPath()) + "/.db/latest");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
try {
if (!latestDateFile.exists()) {
nofDays = LocalDBService.getInstance().getMagAgeIndexAsLong();
FileUtils.writeStringToFile(latestDateFile, formatter.format(LocalDate.now()));
} else {
nofDays = ChronoUnit.DAYS.between(LocalDate.parse(FileUtils.readFileToString(latestDateFile).substring(0, 8), formatter), LocalDate.now());
// just to be safe
nofDays++;
}
} catch (Exception e) {
nofDays = MAX_CONTENT_AGE_DAYS;
LOGGER.error("Error creating file", e);
}
String location = Support.removeEndingSlash(userService.getPrimaryInstallation().getPath()) + "/Misc/Latest-from-db";
try {
LOGGER.info("Fetching latest " + (int) nofDays + " days");
List<SearchResultItem> items = SearchService.getInstance().search2("***", "***", "***", "***", "***", "***", "***", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "n", (int) nofDays);
CancelableTask cancelTask = CancelableTask.of();
controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, NullWindowOwner.of(), new Object[] { items.size(), cancelTask, location, "Latest content from database installed to " + location });
ReturningTask<Void> task = () -> {
for (SearchResultItem it : items) {
if (cancelTask.isCancelled()) {
break;
}
List<ContentEntry> searchItems = SearchService.getInstance().getSearchItems(it.getId(), it.getCategory()).getContentEntry();
try {
for (ContentEntry entry : searchItems) {
if (cancelTask.isCancelled()) {
break;
}
try {
DownloadArtifactsService.getInstance().downloadItem(it.getId(), it.getCategory(), entry.getId(), it.getName(), entry.getName(), it.getCategoryShortName(), FILE_ACTION.DOWNLOAD, location);
} catch (Exception e) {
LOGGER.error("Error during contentdownload {}", entry.getId());
}
}
} catch (Exception e) {
LOGGER.error("Error during download {}", it.getId(), e);
}
controller.setProgressLabel("Installing " + it.getName());
controller.increaseProgress();
}
controller.progressDone();
Platform.runLater(() -> {
controller.getStage().show();
controller = null;
});
return null;
};
ExecutorUtil.executeAsyncWithRetry(task);
} catch (Exception e) {
LOGGER.error("Error during search of new items", e);
}
}
Aggregations