unshare individual files

This commit is contained in:
Zlatin Balevsky
2019-12-07 11:20:56 +00:00
parent 84375c0201
commit 8536353c26
3 changed files with 54 additions and 31 deletions

View File

@@ -77,25 +77,23 @@ public class FileManager {
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);
void unshareFile(File file) {
if (file.isFile()) {
SharedFile sf = core.getFileManager().getFileToSharedFile().get(file);
if (sf == null)
return;
fileTree.remove(file);
revision++;
FileUnsharedEvent event = new FileUnsharedEvent();
event.setUnsharedFile(sf);
core.getEventBus().publish(event);
} else {
if (core.getMuOptions().getWatchedDirectories().contains(file.getAbsolutePath())) {
DirectoryUnsharedEvent event = new DirectoryUnsharedEvent();
event.setDirectory(file);
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);
revision++;
FileUnsharedEvent event = new FileUnsharedEvent();
event.setUnsharedFile(sf);
core.getEventBus().publish(event);
}
}

View File

@@ -2,6 +2,9 @@ package com.muwire.webui;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -102,16 +105,18 @@ public class FilesServlet extends HttpServlet {
if (action.equals("share")) {
String file = 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));
resp.sendRedirect("/MuWire/Files.jsp");
} else if (action.equals("unshare")) {
String pathElements = req.getParameter("path");
File current = null;
for (String element : pathElements.split(",")) {
element = Base64.decodeToString(element);
if (current == null)
current = new File(element);
else
current = new File(current, element);
}
fileManager.unshareFile(current);
}
resp.sendRedirect("/MuWire/Files.jsp");
}
}

View File

@@ -13,7 +13,7 @@ class Node {
updateDiv() {
var div = document.getElementById(this.nodeId)
if (this.leaf) {
div.innerHTML = "<li>"+this.path+"</li>"
div.innerHTML = "<li>"+this.path+"<br/><a href='#' onclick='window.unshare(\"" + this.nodeId + "\");return false;'>Unshare</a></li>"
} else {
if (this.children.length == 0) {
div.innerHTML = "<li><span><a href='#' onclick='window.expand(\"" + this.nodeId + "\");return false'>" +
@@ -215,8 +215,7 @@ function initFiles() {
root.updateDiv()
}
function expand(nodeId) {
var node = nodesById.get(nodeId)
function encodedPathToRoot(node) {
var pathElements = []
var tmpNode = node
while(tmpNode.parent != null) {
@@ -227,7 +226,12 @@ function expand(nodeId) {
while(pathElements.length > 0)
reversedPath.push(pathElements.pop())
var encodedPath = reversedPath.join(",")
return encodedPath
}
function expand(nodeId) {
var node = nodesById.get(nodeId)
var encodedPath = encodedPathToRoot(node)
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
@@ -267,4 +271,20 @@ function collapse(nodeId) {
var node = nodesById.get(nodeId)
node.children = []
node.updateDiv()
}
function unshare(nodeId) {
var node = nodesById.get(nodeId)
var encodedPath = encodedPathToRoot(node)
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var parent = node.parent
collapse(parent.nodeId)
expand(parent.nodeId)
}
}
xmlhttp.open("POST", "/MuWire/Files", true)
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send("action=unshare&path="+encodedPath)
}