implement sidecar files

This commit is contained in:
Zlatin Balevsky
2019-10-30 21:07:59 +00:00
parent cad3a88517
commit ae651cb6bd
5 changed files with 40 additions and 3 deletions

View File

@ -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)

View File

@ -25,6 +25,7 @@ class FileManager {
final Map<String, Set<File>> commentToFile = new HashMap<>()
final SearchIndex index = new SearchIndex()
final FileTree negativeTree = new FileTree()
final Set<File> 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())

View File

@ -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)
}

View File

@ -0,0 +1,7 @@
package com.muwire.core.files
import com.muwire.core.Event
class SideCarFileEvent extends Event {
File file
}

View File

@ -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;
}