Search in sources :

Example 1 with CancelableTask

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);
}
Also used : ContentEntry(se.light.assembly64.model.ContentEntry) CancelableTask(se.light.assembly64.model.CancelableTask)

Example 2 with CancelableTask

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);
}
Also used : NonReturningTask(se.light.assembly64.model.NonReturningTask) PlaylistEntry(se.light.assembly64.model.PlaylistEntry) ExecutorUtil(se.light.assembly64.util.ExecutorUtil) FileUtils(org.apache.commons.io.FileUtils) PlaylistInfo(se.light.assembly64.model.PlaylistInfo) Collectors(java.util.stream.Collectors) File(java.io.File) ChoiceBox(javafx.scene.control.ChoiceBox) UserService(se.light.assembly64.service.UserService) ArrayList(java.util.ArrayList) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GuiUtils(se.light.assembly64.util.GuiUtils) CancelableTask(se.light.assembly64.model.CancelableTask) WorkLocation(se.light.assembly64.model.WorkLocation) DownloadArtifactsService(se.light.assembly64.service.DownloadArtifactsService) PlaylistService(se.light.assembly64.service.PlaylistService) Collections(java.util.Collections) BaseGuiController(se.light.assembly64.model.BaseGuiController) ArrayList(java.util.ArrayList) NonReturningTask(se.light.assembly64.model.NonReturningTask) ArrayList(java.util.ArrayList) List(java.util.List) PlaylistInfo(se.light.assembly64.model.PlaylistInfo) PlaylistEntry(se.light.assembly64.model.PlaylistEntry) File(java.io.File) CancelableTask(se.light.assembly64.model.CancelableTask)

Example 3 with CancelableTask

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);
    }
}
Also used : OutputStream(java.io.OutputStream) ReturningTask(se.light.assembly64.model.ReturningTask) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) FTP(org.apache.commons.net.ftp.FTP) DecimalFormat(java.text.DecimalFormat) LoggerFactory(org.slf4j.LoggerFactory) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashSet(java.util.HashSet) IOUtils(org.apache.commons.io.IOUtils) ProgressControlWrapper(se.light.assembly64.fx.ProgressControlWrapper) Support(se.light.assembly64.Support) FTPClient(org.apache.commons.net.ftp.FTPClient) ExecutorUtil.executeWithRetry(se.light.assembly64.util.ExecutorUtil.executeWithRetry) CancelableTask(se.light.assembly64.model.CancelableTask) InputStream(java.io.InputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FTPClient(org.apache.commons.net.ftp.FTPClient) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CancelableTask(se.light.assembly64.model.CancelableTask)

Example 4 with CancelableTask

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);
    }
}
Also used : ArtifactDelta(se.light.assembly64.model.ArtifactDelta) ReturningTask(se.light.assembly64.model.ReturningTask) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Support.isRemoteArtifact(se.light.assembly64.Support.isRemoteArtifact) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LatestInstalledItemInfo(se.light.assembly64.model.LatestInstalledItemInfo) Support.getVersionAsInteger(se.light.assembly64.Support.getVersionAsInteger) ArtifactType(se.light.assembly64.model.ArtifactType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressDBController(se.light.assembly64.fx.ProgressDBController) GuiUtils(se.light.assembly64.util.GuiUtils) Map(java.util.Map) Support(se.light.assembly64.Support) Support.flush(se.light.assembly64.Support.flush) CancelableTask(se.light.assembly64.model.CancelableTask) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) Support.getObjectMapper(se.light.assembly64.Support.getObjectMapper) ContentEntry(se.light.assembly64.model.ContentEntry) GenericMessageDialogController(se.light.assembly64.fx.GenericMessageDialogController) Analytics(se.light.assembly64.Analytics) Logger(org.slf4j.Logger) ExecutorUtil(se.light.assembly64.util.ExecutorUtil) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Artifact(se.light.assembly64.model.Artifact) Collectors(java.util.stream.Collectors) LocalStorageUtil(se.light.assembly64.util.LocalStorageUtil) File(java.io.File) ReleaseInfo(se.light.assembly64.model.ReleaseInfo) Platform(javafx.application.Platform) ProgressControlWrapper(se.light.assembly64.fx.ProgressControlWrapper) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) SearchResultItem(se.light.assembly64.model.SearchResultItem) NullWindowOwner(se.light.assembly64.fx.NullWindowOwner) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) DOWNLOAD_STATUS(se.light.assembly64.service.FTPService.DOWNLOAD_STATUS) ProgressMessageBox(se.light.assembly64.ProgressMessageBox) FILE_ACTION(se.light.assembly64.Support.FILE_ACTION) Support.getTodayDateAsBasicIsoString(se.light.assembly64.Support.getTodayDateAsBasicIsoString) LocationAndInstallationStatus(se.light.assembly64.model.LocationAndInstallationStatus) ArrayList(java.util.ArrayList) ReleaseInfo(se.light.assembly64.model.ReleaseInfo) Support.getTodayDateAsBasicIsoString(se.light.assembly64.Support.getTodayDateAsBasicIsoString) File(java.io.File) CancelableTask(se.light.assembly64.model.CancelableTask)

Example 5 with CancelableTask

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);
    }
}
Also used : SearchResultItem(se.light.assembly64.model.SearchResultItem) Support.getTodayDateAsBasicIsoString(se.light.assembly64.Support.getTodayDateAsBasicIsoString) IOException(java.io.IOException) ContentEntry(se.light.assembly64.model.ContentEntry) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DateTimeFormatter(java.time.format.DateTimeFormatter) CancelableTask(se.light.assembly64.model.CancelableTask)

Aggregations

CancelableTask (se.light.assembly64.model.CancelableTask)11 File (java.io.File)9 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileUtils (org.apache.commons.io.FileUtils)4 ReturningTask (se.light.assembly64.model.ReturningTask)4 SearchResultItem (se.light.assembly64.model.SearchResultItem)4 DateTimeFormatter (java.time.format.DateTimeFormatter)3 Arrays (java.util.Arrays)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Collectors (java.util.stream.Collectors)3 Platform (javafx.application.Platform)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Support (se.light.assembly64.Support)3 Support.getTodayDateAsBasicIsoString (se.light.assembly64.Support.getTodayDateAsBasicIsoString)3 ContentEntry (se.light.assembly64.model.ContentEntry)3