diff --git a/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java b/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java index 2dfd51c..056b67b 100644 --- a/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java +++ b/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java @@ -3,8 +3,7 @@ package de.srsoftware.umbrella.files; import static de.srsoftware.umbrella.core.ConnectionProvider.connect; 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.*; import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; import static de.srsoftware.umbrella.files.Constants.CONFIG_DATABASE; import static de.srsoftware.umbrella.files.Constants.CONFIG_FILESTORE; @@ -19,6 +18,7 @@ import de.srsoftware.umbrella.core.BaseHandler; import de.srsoftware.umbrella.core.ModuleRegistry; import de.srsoftware.umbrella.core.api.FileService; import de.srsoftware.umbrella.core.exceptions.UmbrellaException; +import de.srsoftware.umbrella.core.model.Company; import de.srsoftware.umbrella.core.model.Project; import de.srsoftware.umbrella.core.model.Token; import de.srsoftware.umbrella.core.model.UmbrellaUser; @@ -71,8 +71,35 @@ public class FileModule extends BaseHandler implements FileService { } } - private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) { - return false; + private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { + var cpId = path.pop(); + var companies = companyService(); + Map companyList; + if (cpId == null){ + companyList = companies.listCompaniesOf(user); + var map = companyList.values().stream().collect(Collectors.toMap(company -> "/company/"+company.id(), Company::name)); + return sendContent(ex,Map.of("dirs",map)); + } + + long cid; + try { + cid = Long.parseLong(cpId); + } catch (NumberFormatException e) { + throw invalidFieldException(COMPANY_ID,"Long"); + } + + var company = companies.get(cid); + var filename = "/company/"+cid; + if (!path.empty()) filename += "/"+URLDecoder.decode(path.toString(),UTF_8); + if (!companies.membership(cid,user.id()) && !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 map = getDirectory(file); + map.put("title",filename.replace("/company/"+cpId,company.name())); + return sendContent(ex,map); + } + return getFile(ex, file); } private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {