mirror of https://github.com/zlatinb/muwire
wip on sharing and unsharing of files server-side
parent
5f74abc944
commit
aa4fb14540
|
@ -1,22 +1,32 @@
|
||||||
package com.muwire.webui;
|
package com.muwire.webui;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.muwire.core.Core;
|
import com.muwire.core.Core;
|
||||||
|
import com.muwire.core.InfoHash;
|
||||||
import com.muwire.core.SharedFile;
|
import com.muwire.core.SharedFile;
|
||||||
|
import com.muwire.core.files.DirectoryUnsharedEvent;
|
||||||
import com.muwire.core.files.FileDownloadedEvent;
|
import com.muwire.core.files.FileDownloadedEvent;
|
||||||
import com.muwire.core.files.FileHashedEvent;
|
import com.muwire.core.files.FileHashedEvent;
|
||||||
|
import com.muwire.core.files.FileHashingEvent;
|
||||||
import com.muwire.core.files.FileListCallback;
|
import com.muwire.core.files.FileListCallback;
|
||||||
import com.muwire.core.files.FileLoadedEvent;
|
import com.muwire.core.files.FileLoadedEvent;
|
||||||
|
import com.muwire.core.files.FileSharedEvent;
|
||||||
import com.muwire.core.files.FileTree;
|
import com.muwire.core.files.FileTree;
|
||||||
|
import com.muwire.core.files.FileUnsharedEvent;
|
||||||
|
|
||||||
|
import net.i2p.data.Base64;
|
||||||
|
|
||||||
public class FileManager {
|
public class FileManager {
|
||||||
|
|
||||||
private final Core core;
|
private final Core core;
|
||||||
private final FileTree<SharedFile> fileTree = new FileTree<>();
|
private final FileTree<SharedFile> fileTree = new FileTree<>();
|
||||||
|
|
||||||
|
private volatile String hashingFile;
|
||||||
|
|
||||||
public FileManager(Core core) {
|
public FileManager(Core core) {
|
||||||
this.core = core;
|
this.core = core;
|
||||||
}
|
}
|
||||||
|
@ -26,9 +36,14 @@ public class FileManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onFileHashedEvent(FileHashedEvent e) {
|
public void onFileHashedEvent(FileHashedEvent e) {
|
||||||
|
hashingFile = null;
|
||||||
fileTree.add(e.getSharedFile().getFile(), e.getSharedFile());
|
fileTree.add(e.getSharedFile().getFile(), e.getSharedFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onFileHashingEvent(FileHashingEvent e) {
|
||||||
|
hashingFile = e.getHashingFile().getPath();
|
||||||
|
}
|
||||||
|
|
||||||
public void onFileDownloadedEvent(FileDownloadedEvent e) {
|
public void onFileDownloadedEvent(FileDownloadedEvent e) {
|
||||||
if (core.getMuOptions().getShareDownloadedFiles())
|
if (core.getMuOptions().getShareDownloadedFiles())
|
||||||
fileTree.add(e.getDownloadedFile().getFile(), e.getDownloadedFile());
|
fileTree.add(e.getDownloadedFile().getFile(), e.getDownloadedFile());
|
||||||
|
@ -37,4 +52,40 @@ public class FileManager {
|
||||||
void list(File parent, FileListCallback<SharedFile> callback) {
|
void list(File parent, FileListCallback<SharedFile> callback) {
|
||||||
fileTree.list(parent, 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
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
String section = req.getParameter("section");
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
|
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
|
||||||
sb.append("<Files>");
|
if (section.equals("status")) {
|
||||||
ListCallback cb = new ListCallback(sb);
|
sb.append("<Status>");
|
||||||
String encodedPath = req.getParameter("path");
|
sb.append("<Count>").append(fileManager.numSharedFiles()).append("</Count>");
|
||||||
File current = null;
|
String hashingFile = fileManager.getHashingFile();
|
||||||
if (encodedPath != null) {
|
if (hashingFile != null)
|
||||||
String[] split = encodedPath.split(",");
|
sb.append("<Hashing>").append(Util.escapeHTMLinXML(hashingFile)).append("</Hashing>");
|
||||||
for (String element : split) {
|
sb.append("</Status>");
|
||||||
element = Base64.decodeToString(element);
|
} else if (section.equals("files")) {
|
||||||
if (current == null) {
|
sb.append("<Files>");
|
||||||
current = new File(element);
|
ListCallback cb = new ListCallback(sb);
|
||||||
continue;
|
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.setContentType("text/xml");
|
||||||
resp.setCharacterEncoding("UTF-8");
|
resp.setCharacterEncoding("UTF-8");
|
||||||
resp.setDateHeader("Expires", 0);
|
resp.setDateHeader("Expires", 0);
|
||||||
|
@ -72,4 +82,24 @@ public class FilesServlet extends HttpServlet {
|
||||||
sb.append("<Directory>").append(Util.escapeHTMLinXML(f.getName())).append("</Directory>");
|
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.download.DownloadStartedEvent;
|
||||||
import com.muwire.core.files.FileDownloadedEvent;
|
import com.muwire.core.files.FileDownloadedEvent;
|
||||||
import com.muwire.core.files.FileHashedEvent;
|
import com.muwire.core.files.FileHashedEvent;
|
||||||
|
import com.muwire.core.files.FileHashingEvent;
|
||||||
import com.muwire.core.files.FileLoadedEvent;
|
import com.muwire.core.files.FileLoadedEvent;
|
||||||
import com.muwire.core.search.UIResultBatchEvent;
|
import com.muwire.core.search.UIResultBatchEvent;
|
||||||
|
|
||||||
|
@ -129,6 +130,7 @@ public class MuWireClient {
|
||||||
core.getEventBus().register(FileLoadedEvent.class, fileManager);
|
core.getEventBus().register(FileLoadedEvent.class, fileManager);
|
||||||
core.getEventBus().register(FileHashedEvent.class, fileManager);
|
core.getEventBus().register(FileHashedEvent.class, fileManager);
|
||||||
core.getEventBus().register(FileDownloadedEvent.class, fileManager);
|
core.getEventBus().register(FileDownloadedEvent.class, fileManager);
|
||||||
|
core.getEventBus().register(FileHashingEvent.class, fileManager);
|
||||||
|
|
||||||
servletContext.setAttribute("searchManager", searchManager);
|
servletContext.setAttribute("searchManager", searchManager);
|
||||||
servletContext.setAttribute("downloadManager", downloadManager);
|
servletContext.setAttribute("downloadManager", downloadManager);
|
||||||
|
|
|
@ -19,10 +19,5 @@
|
||||||
<input type="hidden" name="action" value="share">
|
<input type="hidden" name="action" value="share">
|
||||||
<input type="submit" value="Share">
|
<input type="submit" value="Share">
|
||||||
</form>
|
</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>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue