DeploymentScanner.java

package server.restful.common.deployment;

import net.java.truevfs.access.TFile;
import ui.common.download.DownloadThread;
import ui.common.download.IDownloadListener;
import ui.entities.config.Configuration;
import ui.entities.config.SidPlay2Section;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static libsidutils.IOUtils.ioWrap;
import static ui.common.download.DownloadThread.ILLEGAL_FILENAME_CHARS;
import static ui.common.download.DownloadThread.REPLACEMENT_ILLEGAL_CHAR;

/**
 * Scans a deploy directory on server startup for *.deploy marker files.
 * <p>
 * Each marker file contains HTTP URLs (one per line) that are downloaded sequentially using the existing
 * {@link DownloadThread} infrastructure. Old files are cleaned from the target directory before downloading. On success
 * the marker file is deleted; on failure it is kept for retry on the next startup.
 * <p>
 * The deploy directory is {@code <tmpDir>/deploy/} (default: {@code ~/.jsidplay2/deploy/}).
 *
 * @author ken
 */
public class DeploymentScanner {

    private static final Logger LOG = Logger.getLogger(DeploymentScanner.class.getName());

    private static final String DEPLOY_DIR_NAME = "deploy";
    private static final String DEPLOY_FILE_PREFIX = "jsidplay2-server-";
    private static final String DEPLOY_FILE_EXTENSION = ".deploy";

    private static final Map<String, Function<SidPlay2Section, File>> COLLECTION_GETTERS;

    static {
        COLLECTION_GETTERS = new HashMap<>();
        COLLECTION_GETTERS.put("hvsc", SidPlay2Section::getHvsc);
        COLLECTION_GETTERS.put("gamebase64", SidPlay2Section::getGameBase64);
        COLLECTION_GETTERS.put("cgsc", SidPlay2Section::getCgsc);
        COLLECTION_GETTERS.put("demos", SidPlay2Section::getDemos);
        COLLECTION_GETTERS.put("mags", SidPlay2Section::getMags);
        COLLECTION_GETTERS.put("hvmec", SidPlay2Section::getHVMEC);
    }

    private static final Map<String, BiConsumer<SidPlay2Section, File>> COLLECTION_SETTERS;

    static {
        COLLECTION_SETTERS = new HashMap<>();
        COLLECTION_SETTERS.put("hvsc", SidPlay2Section::setHvsc);
        COLLECTION_SETTERS.put("gamebase64", SidPlay2Section::setGameBase64);
        COLLECTION_SETTERS.put("cgsc", SidPlay2Section::setCgsc);
        COLLECTION_SETTERS.put("demos", SidPlay2Section::setDemos);
        COLLECTION_SETTERS.put("mags", SidPlay2Section::setMags);
        COLLECTION_SETTERS.put("hvmec", SidPlay2Section::setHVMEC);
    }

    private final Configuration config;
    private final Runnable configSafer;

    public DeploymentScanner(Configuration config, Runnable configSafer) {
        this.config = config;
        this.configSafer = configSafer;
    }

    public void scan() {
        File deployDir = new File(config.getSidplay2Section().getTmpDir(), DEPLOY_DIR_NAME);
        if (!deployDir.exists()) {
            ioWrap(deployDir::mkdirs);
        }
        if (!deployDir.isDirectory()) {
            return;
        }
        File[] markerFiles = deployDir.listFiles((dir, name) -> name.endsWith(DEPLOY_FILE_EXTENSION));
        if (markerFiles == null) {
            return;
        }
        boolean configurationChanged = false;
        for (File markerFile : markerFiles) {
            configurationChanged |= processMarker(markerFile);
        }
        if (configurationChanged) {
            configSafer.run();
        }
    }

    private boolean processMarker(File markerFile) {
        LOG.info("Processing deployment marker: " + markerFile.getName());
        List<String> urls = readUrls(markerFile);
        if (urls.isEmpty()) {
            LOG.warning("No URLs found in marker file: " + markerFile.getName());
            return false;
        }
        for (String url : urls) {
            cleanOldFile(url);
        }
        File firstDownloaded = downloadSequentially(urls);
        if (firstDownloaded != null) {
            LOG.info("Deployment successful, removing marker: " + markerFile.getName());
            boolean configurationChanged = updateConfigIfAbsent(markerFile.getName(), firstDownloaded);
            ioWrap(markerFile::delete);
            return configurationChanged;
        } else {
            LOG.severe("Deployment failed, keeping marker for retry on next startup: " + markerFile.getName());
        }
        return false;
    }

    private List<String> readUrls(File markerFile) {
        List<String> urls = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(markerFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!line.isEmpty() && !line.startsWith("#")) {
                    urls.add(line);
                }
            }
        } catch (IOException e) {
            LOG.severe("Failed to read marker file " + markerFile.getName() + ": " + e.getMessage());
        }
        return urls;
    }

    private void cleanOldFile(String urlString) {
        try {
            URL url = new URI(urlString).toURL();
            String decoded = URLDecoder.decode(url.getFile(), UTF_8);
            String name = new File(decoded).getName().replaceAll(ILLEGAL_FILENAME_CHARS, REPLACEMENT_ILLEGAL_CHAR);
            File tmpDir = config.getSidplay2Section().getTmpDir();
            deleteIfExists(new File(tmpDir, name));
            // Also clean split chunk files (.001, .002, ...)
            int extIdx = name.lastIndexOf('.');
            if (extIdx != -1) {
                String baseName = name.substring(0, extIdx);
                for (int part = 1; part <= 999; part++) {
                    File chunk = new File(tmpDir, baseName + "." + String.format("%03d", part));
                    if (!chunk.exists()) {
                        break;
                    }
                    deleteIfExists(chunk);
                }
            }
        } catch (IOException | URISyntaxException e) {
            LOG.warning("Could not clean old file for URL " + urlString + ": " + e.getMessage());
        }
    }

    private void deleteIfExists(File file) throws IOException {
        if (!file.exists()) {
            return;
        }
        LOG.info("Cleaning: " + file.getAbsolutePath());
        if (file.isDirectory()) {
            try (Stream<Path> stream = Files.walk(file.toPath())) {
                stream.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(f -> ioWrap(f::delete));
            }
        } else {
            ioWrap(file::delete);
        }
    }

    private boolean updateConfigIfAbsent(String markerFileName, File downloadedFile) {
        String collectionKey = markerFileName
                .replace(DEPLOY_FILE_PREFIX, "")
                .replace(DEPLOY_FILE_EXTENSION, "")
                .toLowerCase();
        Function<SidPlay2Section, File> getter = COLLECTION_GETTERS.get(collectionKey);
        BiConsumer<SidPlay2Section, File> setter = COLLECTION_SETTERS.get(collectionKey);
        if (getter == null || setter == null) {
            return false;
        }
        SidPlay2Section s = config.getSidplay2Section();
        if (getter.apply(s) == null) {
            setter.accept(s, new TFile(downloadedFile));
            LOG.info("Auto-configured " + collectionKey + " → " + downloadedFile.getAbsolutePath());
            return true;
        }
        return false;
    }

    private File downloadSequentially(List<String> urls) {
        File firstDownloaded = null;
        for (int i = 0; i < urls.size(); i++) {
            String urlString = urls.get(i);
            LOG.info("Downloading (" + (i + 1) + "/" + urls.size() + "): " + urlString);
            File downloaded = downloadBlocking(urlString);
            if (downloaded == null) {
                LOG.severe("Download failed: " + urlString);
                return null;
            }
            LOG.info("Downloaded: " + downloaded.getAbsolutePath());
            if (firstDownloaded == null) {
                firstDownloaded = downloaded;
            }
        }
        return firstDownloaded;
    }

    private File downloadBlocking(String urlString) {
        File[] result = {null};
        CountDownLatch latch = new CountDownLatch(1);
        try {
            URL url = new URI(urlString).toURL();
            IDownloadListener listener = new IDownloadListener() {
                @Override
                public void downloadStep(int step) {
                    if (step % 10 == 0) {
                        LOG.info("  " + step + "%");
                    }
                }

                @Override
                public void downloadStop(File downloadedFile) {
                    result[0] = downloadedFile;
                    latch.countDown();
                }
            };
            new DownloadThread(config, listener, url, true).start();
            latch.await();
        } catch (IOException | URISyntaxException | InterruptedException e) {
            LOG.severe("Download error for " + urlString + ": " + e.getMessage());
            Thread.currentThread().interrupt();
        }
        return result[0];
    }
}