diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 3a21dd96..58d29364 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -28,6 +28,7 @@ import com.muwire.core.files.FileSharedEvent import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.files.HasherService import com.muwire.core.files.PersisterService +import com.muwire.core.files.SideCarFileEvent import com.muwire.core.files.UICommentEvent import com.muwire.core.files.UIPersistFilesEvent import com.muwire.core.files.AllFilesLoadedEvent @@ -221,6 +222,7 @@ public class Core { eventBus.register(SearchEvent.class, fileManager) eventBus.register(DirectoryUnsharedEvent.class, fileManager) eventBus.register(UICommentEvent.class, fileManager) + eventBus.register(SideCarFileEvent.class, fileManager) log.info("initializing mesh manager") MeshManager meshManager = new MeshManager(fileManager, home, props) diff --git a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy index cefd3958..81f02981 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -25,6 +25,7 @@ class FileManager { final Map> commentToFile = new HashMap<>() final SearchIndex index = new SearchIndex() final FileTree negativeTree = new FileTree() + final Set sideCarFiles = new HashSet<>() FileManager(EventBus eventBus, MuWireSettings settings) { this.settings = settings @@ -36,8 +37,15 @@ class FileManager { } void onFileHashedEvent(FileHashedEvent e) { - if (e.sharedFile != null) - addToIndex(e.sharedFile) + if (e.sharedFile == null) + return + File f = e.sharedFile.getFile() + if (sideCarFiles.remove(f)) { + File sideCar = new File(f.getParentFile(), f.getName() + ".mwcomment") + if (sideCar.exists()) + e.sharedFile.setComment(Base64.encode(DataUtil.encodei18nString(sideCar.text))) + } + addToIndex(e.sharedFile) } void onFileLoadedEvent(FileLoadedEvent e) { @@ -49,6 +57,21 @@ class FileManager { addToIndex(e.downloadedFile) } } + + void onSideCarFileEvent(SideCarFileEvent e) { + String name = e.file.getName() + name = name.substring(0, name.length() - ".mwcomment".length()) + File target = new File(e.file.getParentFile(), name) + SharedFile existing = fileToSharedFile.get(target) + if (existing == null) { + sideCarFiles.add(target) + return + } + String comment = Base64.encode(DataUtil.encodei18nString(e.file.text)) + String oldComment = existing.getComment() + existing.setComment(comment) + eventBus.publish(new UICommentEvent(oldComment : oldComment, sharedFile : existing)) + } private void addToIndex(SharedFile sf) { log.info("Adding shared file " + sf.getFile()) diff --git a/core/src/main/groovy/com/muwire/core/files/HasherService.groovy b/core/src/main/groovy/com/muwire/core/files/HasherService.groovy index 85947e84..fbdf3598 100644 --- a/core/src/main/groovy/com/muwire/core/files/HasherService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/HasherService.groovy @@ -3,6 +3,7 @@ package com.muwire.core.files import java.util.concurrent.Executor import java.util.concurrent.Executors +import com.muwire.core.Constants import com.muwire.core.EventBus import com.muwire.core.MuWireSettings import com.muwire.core.SharedFile @@ -33,7 +34,9 @@ class HasherService { return if (fileManager.fileToSharedFile.containsKey(canonical)) return - if (hashed.add(canonical)) + if (canonical.getName().endsWith(".mwcomment") && canonical.length() < Constants.MAX_COMMENT_LENGTH) + eventBus.publish(new SideCarFileEvent(file : canonical)) + else if (hashed.add(canonical)) executor.execute( { -> process(canonical) } as Runnable) } diff --git a/core/src/main/groovy/com/muwire/core/files/SideCarFileEvent.groovy b/core/src/main/groovy/com/muwire/core/files/SideCarFileEvent.groovy new file mode 100644 index 00000000..511314e1 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/SideCarFileEvent.groovy @@ -0,0 +1,7 @@ +package com.muwire.core.files + +import com.muwire.core.Event + +class SideCarFileEvent extends Event { + File file +} diff --git a/core/src/main/java/com/muwire/core/Constants.java b/core/src/main/java/com/muwire/core/Constants.java index b2c6e6ee..d8abe80c 100644 --- a/core/src/main/java/com/muwire/core/Constants.java +++ b/core/src/main/java/com/muwire/core/Constants.java @@ -10,4 +10,6 @@ public class Constants { public static final int MAX_HEADERS = 16; public static final int MAX_RESULTS = 0x1 << 16; + + public static final int MAX_COMMENT_LENGTH = 0x1 << 15; }