wip on sharing and unsharing of files server-side
This commit is contained in:
@ -1,22 +1,32 @@
|
||||
package com.muwire.webui;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.muwire.core.Core;
|
||||
import com.muwire.core.InfoHash;
|
||||
import com.muwire.core.SharedFile;
|
||||
import com.muwire.core.files.DirectoryUnsharedEvent;
|
||||
import com.muwire.core.files.FileDownloadedEvent;
|
||||
import com.muwire.core.files.FileHashedEvent;
|
||||
import com.muwire.core.files.FileHashingEvent;
|
||||
import com.muwire.core.files.FileListCallback;
|
||||
import com.muwire.core.files.FileLoadedEvent;
|
||||
import com.muwire.core.files.FileSharedEvent;
|
||||
import com.muwire.core.files.FileTree;
|
||||
import com.muwire.core.files.FileUnsharedEvent;
|
||||
|
||||
import net.i2p.data.Base64;
|
||||
|
||||
public class FileManager {
|
||||
|
||||
private final Core core;
|
||||
private final FileTree<SharedFile> fileTree = new FileTree<>();
|
||||
|
||||
private volatile String hashingFile;
|
||||
|
||||
public FileManager(Core core) {
|
||||
this.core = core;
|
||||
}
|
||||
@ -26,9 +36,14 @@ public class FileManager {
|
||||
}
|
||||
|
||||
public void onFileHashedEvent(FileHashedEvent e) {
|
||||
hashingFile = null;
|
||||
fileTree.add(e.getSharedFile().getFile(), e.getSharedFile());
|
||||
}
|
||||
|
||||
public void onFileHashingEvent(FileHashingEvent e) {
|
||||
hashingFile = e.getHashingFile().getPath();
|
||||
}
|
||||
|
||||
public void onFileDownloadedEvent(FileDownloadedEvent e) {
|
||||
if (core.getMuOptions().getShareDownloadedFiles())
|
||||
fileTree.add(e.getDownloadedFile().getFile(), e.getDownloadedFile());
|
||||
@ -37,4 +52,40 @@ public class FileManager {
|
||||
void list(File parent, FileListCallback<SharedFile> callback) {
|
||||
fileTree.list(parent, callback);
|
||||
}
|
||||
|
||||
String getHashingFile() {
|
||||
return hashingFile;
|
||||
}
|
||||
|
||||
int numSharedFiles() {
|
||||
return core.getFileManager().getFileToSharedFile().size();
|
||||
}
|
||||
|
||||
void share(String filePath) {
|
||||
File file = new File(filePath);
|
||||
FileSharedEvent event = new FileSharedEvent();
|
||||
event.setFile(file);
|
||||
core.getEventBus().publish(event);
|
||||
}
|
||||
|
||||
void unshareDirectory(String filePath) {
|
||||
File directory = new File(filePath);
|
||||
if (core.getMuOptions().getWatchedDirectories().contains(directory)) {
|
||||
DirectoryUnsharedEvent event = new DirectoryUnsharedEvent();
|
||||
event.setDirectory(directory);
|
||||
core.getEventBus().publish(event);
|
||||
}
|
||||
}
|
||||
|
||||
void unshareFile(String filePath) {
|
||||
File file = new File(filePath);
|
||||
SharedFile sf = core.getFileManager().getFileToSharedFile().get(file);
|
||||
if (sf == null)
|
||||
return;
|
||||
|
||||
fileTree.remove(file);
|
||||
FileUnsharedEvent event = new FileUnsharedEvent();
|
||||
event.setUnsharedFile(sf);
|
||||
core.getEventBus().publish(event);
|
||||
}
|
||||
}
|
||||
|
@ -21,25 +21,35 @@ public class FilesServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String section = req.getParameter("section");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
|
||||
sb.append("<Files>");
|
||||
ListCallback cb = new ListCallback(sb);
|
||||
String encodedPath = req.getParameter("path");
|
||||
File current = null;
|
||||
if (encodedPath != null) {
|
||||
String[] split = encodedPath.split(",");
|
||||
for (String element : split) {
|
||||
element = Base64.decodeToString(element);
|
||||
if (current == null) {
|
||||
current = new File(element);
|
||||
continue;
|
||||
if (section.equals("status")) {
|
||||
sb.append("<Status>");
|
||||
sb.append("<Count>").append(fileManager.numSharedFiles()).append("</Count>");
|
||||
String hashingFile = fileManager.getHashingFile();
|
||||
if (hashingFile != null)
|
||||
sb.append("<Hashing>").append(Util.escapeHTMLinXML(hashingFile)).append("</Hashing>");
|
||||
sb.append("</Status>");
|
||||
} else if (section.equals("files")) {
|
||||
sb.append("<Files>");
|
||||
ListCallback cb = new ListCallback(sb);
|
||||
String encodedPath = req.getParameter("path");
|
||||
File current = null;
|
||||
if (encodedPath != null) {
|
||||
String[] split = encodedPath.split(",");
|
||||
for (String element : split) {
|
||||
element = Base64.decodeToString(element);
|
||||
if (current == null) {
|
||||
current = new File(element);
|
||||
continue;
|
||||
}
|
||||
current = new File(current, element);
|
||||
}
|
||||
current = new File(current, element);
|
||||
}
|
||||
fileManager.list(current, cb);
|
||||
sb.append("</Files>");
|
||||
}
|
||||
fileManager.list(current, cb);
|
||||
sb.append("</Files>");
|
||||
resp.setContentType("text/xml");
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.setDateHeader("Expires", 0);
|
||||
@ -72,4 +82,24 @@ public class FilesServlet extends HttpServlet {
|
||||
sb.append("<Directory>").append(Util.escapeHTMLinXML(f.getName())).append("</Directory>");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String action = req.getParameter("action");
|
||||
if (action.equals("share")) {
|
||||
String file = Base64.decodeToString(req.getParameter("file"));
|
||||
fileManager.share(file);
|
||||
} else if (action.equals("unshareFile")) {
|
||||
String files = req.getParameter("files");
|
||||
for (String file : files.split(","))
|
||||
fileManager.unshareFile(Base64.decodeToString(file));
|
||||
String directories = req.getParameter("directories");
|
||||
if (directories != null) {
|
||||
for (String directory : directories.split(","))
|
||||
fileManager.unshareDirectory(Base64.decodeToString(directory));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import com.muwire.core.connection.DisconnectionEvent;
|
||||
import com.muwire.core.download.DownloadStartedEvent;
|
||||
import com.muwire.core.files.FileDownloadedEvent;
|
||||
import com.muwire.core.files.FileHashedEvent;
|
||||
import com.muwire.core.files.FileHashingEvent;
|
||||
import com.muwire.core.files.FileLoadedEvent;
|
||||
import com.muwire.core.search.UIResultBatchEvent;
|
||||
|
||||
@ -129,6 +130,7 @@ public class MuWireClient {
|
||||
core.getEventBus().register(FileLoadedEvent.class, fileManager);
|
||||
core.getEventBus().register(FileHashedEvent.class, fileManager);
|
||||
core.getEventBus().register(FileDownloadedEvent.class, fileManager);
|
||||
core.getEventBus().register(FileHashingEvent.class, fileManager);
|
||||
|
||||
servletContext.setAttribute("searchManager", searchManager);
|
||||
servletContext.setAttribute("downloadManager", downloadManager);
|
||||
|
@ -19,10 +19,5 @@
|
||||
<input type="hidden" name="action" value="share">
|
||||
<input type="submit" value="Share">
|
||||
</form>
|
||||
<form action="/MuWire/Files" method="post">
|
||||
<input type="text" name="file">
|
||||
<input type="hidden" name="action" value="unshare">
|
||||
<input type="submit" value="Unshare">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user