mirror of https://github.com/zlatinb/muwire
implement sidecar files
parent
cad3a88517
commit
ae651cb6bd
|
@ -28,6 +28,7 @@ import com.muwire.core.files.FileSharedEvent
|
||||||
import com.muwire.core.files.FileUnsharedEvent
|
import com.muwire.core.files.FileUnsharedEvent
|
||||||
import com.muwire.core.files.HasherService
|
import com.muwire.core.files.HasherService
|
||||||
import com.muwire.core.files.PersisterService
|
import com.muwire.core.files.PersisterService
|
||||||
|
import com.muwire.core.files.SideCarFileEvent
|
||||||
import com.muwire.core.files.UICommentEvent
|
import com.muwire.core.files.UICommentEvent
|
||||||
import com.muwire.core.files.UIPersistFilesEvent
|
import com.muwire.core.files.UIPersistFilesEvent
|
||||||
import com.muwire.core.files.AllFilesLoadedEvent
|
import com.muwire.core.files.AllFilesLoadedEvent
|
||||||
|
@ -221,6 +222,7 @@ public class Core {
|
||||||
eventBus.register(SearchEvent.class, fileManager)
|
eventBus.register(SearchEvent.class, fileManager)
|
||||||
eventBus.register(DirectoryUnsharedEvent.class, fileManager)
|
eventBus.register(DirectoryUnsharedEvent.class, fileManager)
|
||||||
eventBus.register(UICommentEvent.class, fileManager)
|
eventBus.register(UICommentEvent.class, fileManager)
|
||||||
|
eventBus.register(SideCarFileEvent.class, fileManager)
|
||||||
|
|
||||||
log.info("initializing mesh manager")
|
log.info("initializing mesh manager")
|
||||||
MeshManager meshManager = new MeshManager(fileManager, home, props)
|
MeshManager meshManager = new MeshManager(fileManager, home, props)
|
||||||
|
|
|
@ -25,6 +25,7 @@ class FileManager {
|
||||||
final Map<String, Set<File>> commentToFile = new HashMap<>()
|
final Map<String, Set<File>> commentToFile = new HashMap<>()
|
||||||
final SearchIndex index = new SearchIndex()
|
final SearchIndex index = new SearchIndex()
|
||||||
final FileTree negativeTree = new FileTree()
|
final FileTree negativeTree = new FileTree()
|
||||||
|
final Set<File> sideCarFiles = new HashSet<>()
|
||||||
|
|
||||||
FileManager(EventBus eventBus, MuWireSettings settings) {
|
FileManager(EventBus eventBus, MuWireSettings settings) {
|
||||||
this.settings = settings
|
this.settings = settings
|
||||||
|
@ -36,8 +37,15 @@ class FileManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
void onFileHashedEvent(FileHashedEvent e) {
|
void onFileHashedEvent(FileHashedEvent e) {
|
||||||
if (e.sharedFile != null)
|
if (e.sharedFile == null)
|
||||||
addToIndex(e.sharedFile)
|
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) {
|
void onFileLoadedEvent(FileLoadedEvent e) {
|
||||||
|
@ -49,6 +57,21 @@ class FileManager {
|
||||||
addToIndex(e.downloadedFile)
|
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) {
|
private void addToIndex(SharedFile sf) {
|
||||||
log.info("Adding shared file " + sf.getFile())
|
log.info("Adding shared file " + sf.getFile())
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.muwire.core.files
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
|
import com.muwire.core.Constants
|
||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
import com.muwire.core.MuWireSettings
|
import com.muwire.core.MuWireSettings
|
||||||
import com.muwire.core.SharedFile
|
import com.muwire.core.SharedFile
|
||||||
|
@ -33,7 +34,9 @@ class HasherService {
|
||||||
return
|
return
|
||||||
if (fileManager.fileToSharedFile.containsKey(canonical))
|
if (fileManager.fileToSharedFile.containsKey(canonical))
|
||||||
return
|
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)
|
executor.execute( { -> process(canonical) } as Runnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.muwire.core.files
|
||||||
|
|
||||||
|
import com.muwire.core.Event
|
||||||
|
|
||||||
|
class SideCarFileEvent extends Event {
|
||||||
|
File file
|
||||||
|
}
|
|
@ -10,4 +10,6 @@ public class Constants {
|
||||||
public static final int MAX_HEADERS = 16;
|
public static final int MAX_HEADERS = 16;
|
||||||
|
|
||||||
public static final int MAX_RESULTS = 0x1 << 16;
|
public static final int MAX_RESULTS = 0x1 << 16;
|
||||||
|
|
||||||
|
public static final int MAX_COMMENT_LENGTH = 0x1 << 15;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue