|
|
|
@ -8,20 +8,25 @@ import de.srsoftware.umbrella.core.BaseHandler; |
|
|
|
import de.srsoftware.umbrella.core.ModuleRegistry; |
|
|
|
import de.srsoftware.umbrella.core.ModuleRegistry; |
|
|
|
import de.srsoftware.umbrella.core.api.FileService; |
|
|
|
import de.srsoftware.umbrella.core.api.FileService; |
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
|
|
|
|
import de.srsoftware.umbrella.core.model.Project; |
|
|
|
import de.srsoftware.umbrella.core.model.Token; |
|
|
|
import de.srsoftware.umbrella.core.model.Token; |
|
|
|
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
|
|
|
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.projectService; |
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.userService; |
|
|
|
import static de.srsoftware.umbrella.core.ModuleRegistry.userService; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.files.Constants.CONFIG_DATABASE; |
|
|
|
import static de.srsoftware.umbrella.files.Constants.CONFIG_DATABASE; |
|
|
|
import static de.srsoftware.umbrella.files.Constants.CONFIG_FILESTORE; |
|
|
|
import static de.srsoftware.umbrella.files.Constants.CONFIG_FILESTORE; |
|
|
|
|
|
|
|
|
|
|
|
@ -68,8 +73,50 @@ public class FileModule extends BaseHandler implements FileService { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) { |
|
|
|
private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
return false; |
|
|
|
var prjId = path.pop(); |
|
|
|
|
|
|
|
var projects = projectService(); |
|
|
|
|
|
|
|
if (prjId == null){ |
|
|
|
|
|
|
|
var projectList = projects.listUserProjects(user.id(),true); |
|
|
|
|
|
|
|
var map = projectList.values().stream().collect(Collectors.toMap(prj -> "/project/"+prj.id(),Project::name)); |
|
|
|
|
|
|
|
return sendContent(ex,Map.of("dirs",map)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long pid; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
pid = Long.parseLong(prjId); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
throw invalidFieldException(PROJECT_ID,"Long"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var project = projects.loadMembers(projects.load(pid)); |
|
|
|
|
|
|
|
var filename = "/project/"+pid; |
|
|
|
|
|
|
|
if (!path.empty()) filename += "/"+path; |
|
|
|
|
|
|
|
if (!project.hasMember(user) && !fileDb.isPermitted(user,filename)) throw forbidden("You are not allowed to access {0}",filename); |
|
|
|
|
|
|
|
var file = new File(baseDir+filename); |
|
|
|
|
|
|
|
if (!file.exists()) throw unprocessable("{0} does not exist!",filename); |
|
|
|
|
|
|
|
if (file.isDirectory()){ |
|
|
|
|
|
|
|
Map<String,Object> map = getDirectory(file); |
|
|
|
|
|
|
|
map.put("title",filename.replace("/project/"+prjId,project.name())); |
|
|
|
|
|
|
|
return sendContent(ex,map); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return getFile(ex, file); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean getFile(HttpExchange ex, File file) throws IOException { |
|
|
|
|
|
|
|
return sendContent(ex,new FileInputStream(file)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> getDirectory(File file) throws IOException { |
|
|
|
|
|
|
|
var children = file.listFiles(); |
|
|
|
|
|
|
|
var map = new HashMap<String, Object>(); |
|
|
|
|
|
|
|
if (children == null) return map; |
|
|
|
|
|
|
|
var prefixLen = baseDir.toString().length(); |
|
|
|
|
|
|
|
for (var child : children){ |
|
|
|
|
|
|
|
var o = map.computeIfAbsent(child.isDirectory() ? "dirs" : "files", k -> new HashMap<String, String>()); |
|
|
|
|
|
|
|
//noinspection unchecked
|
|
|
|
|
|
|
|
((Map<String,String>) o).put(child.toString().substring(prefixLen),child.getName()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return map; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean getUserFiles(Path path, HttpExchange ex, UmbrellaUser user) { |
|
|
|
private boolean getUserFiles(Path path, HttpExchange ex, UmbrellaUser user) { |
|
|
|
|