mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
16.34.2018 1f9b179749dfdf3c9f0c103d83f3b9ef9613f50a
Filtering (text search and max result size) works now for file lists.
1 files added
4 files modified
91 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java 26 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java 30 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java 22 ●●●●● patch | view | raw | blame | history
borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java 11 ●●●● patch | view | raw | blame | history
borgbutler-webapp/src/components/views/archives/FileListPanel.jsx 2 ●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
@@ -2,6 +2,7 @@
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.data.FileSystemFilter;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import de.micromata.borgbutler.utils.ReplaceUtils;
import lombok.Getter;
@@ -54,19 +55,26 @@
    /**
     * Will load and touch the archive file if exist. The file will be touched (last modified time will be set to now)
     * for pruning oldest cache files. The last modified time will be the time of the last usage.
     *
     * @param repoConfig
     * @param archive
     * @param filter     If given, only file items matching this filter are returned.
     * @return
     */
    public List<BorgFilesystemItem> load(BorgRepoConfig repoConfig, Archive archive, int maxSize) {
    public List<BorgFilesystemItem> load(BorgRepoConfig repoConfig, Archive archive, FileSystemFilter filter) {
        File file = getFile(repoConfig, archive);
        if (!file.exists()) {
            return null;
        }
        return load(file, maxSize);
        return load(file, filter);
    }
    public List<BorgFilesystemItem> load(File file, int maxSize) {
    /**
     * @param file
     * @param filter If given, only file items matching this filter are returned.
     * @return
     */
    public List<BorgFilesystemItem> load(File file, FileSystemFilter filter) {
        if (file.exists() == false) {
            log.error("File '" + file.getAbsolutePath() + "' doesn't exist. Can't get archive content files.");
            return null;
@@ -86,14 +94,16 @@
                return null;
            }
            int size = (Integer) obj;
            if (maxSize > 0 && maxSize < size) {
                size = maxSize;
            }
            int maxSize = filter != null ? filter.getMaxResultSize() : -1;
            list = new ArrayList<>();
            int counter = 0;
            for (int i = 0; i < size; i++) {
                obj = inputStream.readObject();
                if (obj instanceof BorgFilesystemItem) {
                    list.add((BorgFilesystemItem) obj);
                    if (filter == null || filter.matches(((BorgFilesystemItem) obj))) {
                        list.add((BorgFilesystemItem) obj);
                        if (maxSize > 0 && counter++ >= maxSize) break;
                    }
                } else {
                    log.error("Can't load archive content. FilesystemItem expected, but received: "
                            + (obj != null ? obj.getClass() : "null")
@@ -187,7 +197,7 @@
    File getFile(BorgRepoConfig repoConfig, Archive archive) {
        return new File(cacheDir, ReplaceUtils.encodeFilename(CACHE_ARCHIVE_LISTS_BASENAME + archive.getTime()
                + "-" + repoConfig.getRepo() + "-" + archive.getName() + CACHE_FILE_GZIP_EXTENSION,
                        + "-" + repoConfig.getRepo() + "-" + archive.getName() + CACHE_FILE_GZIP_EXTENSION,
                true));
    }
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -5,6 +5,7 @@
import de.micromata.borgbutler.config.Configuration;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.data.FileSystemFilter;
import de.micromata.borgbutler.data.Repository;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import org.apache.commons.collections4.CollectionUtils;
@@ -180,16 +181,16 @@
    }
    public List<BorgFilesystemItem> getArchiveContent(String archiveId) {
        return getArchiveContent(archiveId, true, -1);
        return getArchiveContent(archiveId, true, null);
    }
    /**
     * @param archiveId
     * @param forceLoad If false, the file list will only get if not yet loaded.
     * @param maxSize
     * @param filter If not null, the result will be filtered.
     * @return
     */
    public List<BorgFilesystemItem> getArchiveContent(String archiveId, boolean forceLoad, int maxSize) {
    public List<BorgFilesystemItem> getArchiveContent(String archiveId, boolean forceLoad, FileSystemFilter filter) {
        Archive archive = null;
        outerLoop:
        for (Repository repository : getAllRepositories()) {
@@ -207,7 +208,7 @@
            return null;
        }
        BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepoConfig(archive.getRepoId());
        return getArchiveContent(repoConfig, archive, forceLoad, maxSize);
        return getArchiveContent(repoConfig, archive, forceLoad, filter);
    }
    /**
@@ -216,29 +217,34 @@
     * @return
     */
    public List<BorgFilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive) {
        return getArchiveContent(repoConfig, archive, true, -1);
        return getArchiveContent(repoConfig, archive, true, null);
    }
    /**
     * @param repoConfig
     * @param archive
     * @param maxSize    Max result size (default is -1 meaning all).
     * @param filter    If given, only the items matching this filter are returned..
     * @return
     */
    public List<BorgFilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive, boolean forceLoad, int maxSize) {
    public List<BorgFilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive, boolean forceLoad,
                                                      FileSystemFilter filter) {
        if (archive == null || StringUtils.isBlank(archive.getName())) {
            return null;
        }
        List<BorgFilesystemItem> items = archiveFilelistCache.load(repoConfig, archive, maxSize);
        List<BorgFilesystemItem> items = archiveFilelistCache.load(repoConfig, archive, filter);
        if (items == null && forceLoad) {
            List<BorgFilesystemItem> list = BorgCommands.listArchiveContent(repoConfig, archive.getName());
            if (CollectionUtils.isNotEmpty(list)) {
                archiveFilelistCache.save(repoConfig, archive, list);
                items = new ArrayList<>();
                int i = 0;
                int counter = 0;
                boolean search = filter != null && StringUtils.isNotBlank(filter.getSearchString());
                int maxSize = filter != null ? filter.getMaxResultSize() : -1;
                for (BorgFilesystemItem item : list) {
                    if (++i > maxSize) break;
                    items.add(item);
                    if (filter == null || filter.matches(item)) {
                        items.add(item);
                        if (maxSize > 0 && counter++ >= maxSize) break;
                    }
                }
            }
        }
@@ -249,7 +255,7 @@
    }
    public List<BorgFilesystemItem> getArchiveContent(File file) {
        return archiveFilelistCache.load(file, -1);
        return archiveFilelistCache.load(file, null);
    }
    public void shutdown() {
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
New file
@@ -0,0 +1,22 @@
package de.micromata.borgbutler.data;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
public class FileSystemFilter {
    @Getter
    @Setter
    private String searchString;
    @Getter
    @Setter
    private int maxResultSize;
    public boolean matches(BorgFilesystemItem item) {
        if (searchString == null || searchString.length() == 0) {
            return true;
        }
        return StringUtils.containsIgnoreCase(item.getPath(), searchString);
    }
}
borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/ArchivesRest.java
@@ -2,6 +2,7 @@
import de.micromata.borgbutler.cache.ButlerCache;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.data.FileSystemFilter;
import de.micromata.borgbutler.data.Repository;
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
@@ -50,14 +51,18 @@
     * @see JsonUtils#toJson(Object, boolean)
     */
    public String getArchiveFileLIst(@QueryParam("archiveId") String archiveId,
                                     @QueryParam("search") String search,
                                     @QueryParam("searchString") String searchString,
                                     @QueryParam("maxResultSize") String maxResultSize,
                                     @QueryParam("force") boolean force,
                                     @QueryParam("prettyPrinter") boolean prettyPrinter) {
        int maxSize = NumberUtils.toInt(maxResultSize, 50);
        List<BorgFilesystemItem> items = ButlerCache.getInstance().getArchiveContent(archiveId, force, maxSize);
        FileSystemFilter filter = new FileSystemFilter()
                .setSearchString(searchString)
                .setMaxResultSize(maxSize);
        List<BorgFilesystemItem> items = ButlerCache.getInstance().getArchiveContent(archiveId, force,
                filter);
        if (items == null) {
            return "[]";
            return "[{patch: 'notLoaded'}]";
        }
        return JsonUtils.toJson(items, prettyPrinter);
    }
borgbutler-webapp/src/components/views/archives/FileListPanel.jsx
@@ -38,7 +38,7 @@
        fetch(getRestServiceUrl('archives/filelist', {
            archiveId: this.props.archiveId,
            force: forceReload,
            search: this.state.filter.search,
            searchString: this.state.filter.search,
            maxResultSize: this.state.filter.maxSize
        }), {
            method: 'GET',