From 35b12e617df71a33fab230f3fc19bbc54635ea9a Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 30 Oct 2020 19:09:17 +0000 Subject: [PATCH] builder for file collections --- .../collections/FileCollectionBuilder.groovy | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy diff --git a/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy b/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy new file mode 100644 index 00000000..8fc9ceb3 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/collections/FileCollectionBuilder.groovy @@ -0,0 +1,96 @@ +package com.muwire.core.collections + +import java.nio.file.Path +import java.nio.file.Paths + +import com.muwire.core.InfoHash +import com.muwire.core.Persona +import com.muwire.core.SharedFile +import com.muwire.core.files.FileTree + +import net.i2p.data.SigningPrivateKey + +class FileCollectionBuilder { + private long timestamp + private String root + private Persona author + private String comment = "" + private final Set sharedFiles = new LinkedHashSet<>() + private SigningPrivateKey spk + + public FileCollectionBuilder setRoot(String root) { + this.root = root + this + } + + public FileCollectionBuilder setTimestamp(long timestamp) { + this.timestamp = timestamp + this + } + + public FileCollectionBuilder setAuthor(Persona author) { + this.author = author + this + } + + public FileCollectionBuilder setComment(String comment) { + this.comment = comment + this + } + + public FileCollectionBuilder setSPK(SigningPrivateKey spk) { + this.spk = spk + this + } + + public FileCollectionBuilder addFile(SharedFile sf) { + this.sharedFiles.add(sf) + this + } + + public FileCollection build() { + // TODO: check constraints + if (root == null || timestamp == 0 || author == null || comment == null || spk == null || sharedFiles.isEmpty()) + throw new InvalidCollectionException() + + FileTree tree = new FileTree<>() + for(SharedFile sf : sharedFiles) + tree.add(sf.file, null) + File common = tree.commonAncestor() + + Map sfPaths = new LinkedHashMap<>() + for(SharedFile sf : sharedFiles) { + Path path = sf.file.toPath() + sfPaths.put(sf, path) + } + + if (common != null) { + Path commonPath = common.toPath() + for (SharedFile sf : sfPaths.keySet()) + sfPaths.put(sf, commonPath.relativize(sfPaths.get(sf))) + } + + Map> sfPathElements = new HashMap<>() + for (SharedFile sf : sfPaths.keySet()) { + LinkedList elements = new LinkedList() + for (Path p : sfPaths.get(sf)) + elements.add(p.toString()) + elements.addFirst(root) + sfPathElements.put(sf, elements) + } + + Set files = new LinkedHashSet<>() + for (SharedFile sf : sfPathElements.keySet()) { + String comment = sf.getComment() // TODO: check comment encoding + if (comment == null) + comment = "" + def item = new FileCollectionItem(new InfoHash(sf.root), comment, sfPathElements.get(sf)) + files.add(item) + } + + def rv = new FileCollection(timestamp, author, comment, files, spk) + rv + } + + +}