From 3248847f9c58f937e3a16623ee0a88aaa9cfecb6 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 1 Oct 2025 18:28:22 +0200 Subject: [PATCH] finished implementation offile upload Signed-off-by: Stephan Richter --- .../srsoftware/umbrella/files/FileModule.java | 84 ++++++++++--------- 1 file changed, 43 insertions(+), 41 deletions(-) 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 9c3cf0c..b401cf5 100644 --- a/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java +++ b/files/src/main/java/de/srsoftware/umbrella/files/FileModule.java @@ -1,7 +1,6 @@ /* © SRSoftware 2025 */ package de.srsoftware.umbrella.files; -import static de.srsoftware.tools.MimeType.MIME_FORM_DATA; import static de.srsoftware.umbrella.core.ConnectionProvider.connect; import static de.srsoftware.umbrella.core.Constants.*; import static de.srsoftware.umbrella.core.ModuleRegistry.*; @@ -13,7 +12,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.sun.net.httpserver.HttpExchange; import de.srsoftware.configuration.Configuration; -import de.srsoftware.tools.MimeType; import de.srsoftware.tools.Path; import de.srsoftware.tools.SessionToken; import de.srsoftware.umbrella.core.BaseHandler; @@ -91,6 +89,27 @@ public class FileModule extends BaseHandler implements FileService { } } + private boolean doPost(HttpExchange ex, String filename) throws IOException { + var file = new File(baseDir+filename); + if (contentType(ex).isPresent()) { // file upload + var out = new FileOutputStream(file); + var body = ex.getRequestBody(); + body.transferTo(out); + body.close(); + out.close(); + } else { + if (file.exists()) throw unprocessable("{0} already exists!", filename); + try { + file.mkdirs(); + } catch (Exception e) { + throw unprocessable("Failed to create {0}", filename); + } + } + Map map = getDirectory(file.getParentFile()); + map.put("title",filename); + return sendContent(ex,map); + } + private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { var cpId = path.pop(); var companies = companyService(); @@ -201,7 +220,26 @@ public class FileModule extends BaseHandler implements FileService { } private boolean postCompanyDirectory(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { - return false; + 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 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); + return doPost(ex,filename); } private boolean postProjectDirectory(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { @@ -223,25 +261,7 @@ public class FileModule extends BaseHandler implements FileService { var filename = "/project/"+pid; if (!path.empty()) filename += "/"+URLDecoder.decode(path.toString(),UTF_8); 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 (contentType(ex).isPresent()) { // file upload - var out = new FileOutputStream(file); - var body = ex.getRequestBody(); - body.transferTo(out); - body.close(); - out.close(); - } else { - if (file.exists()) throw unprocessable("{0} already exists!", filename); - try { - file.mkdirs(); - } catch (Exception e) { - throw unprocessable("Failed to create {0}", filename); - } - } - Map map = getDirectory(file.getParentFile()); - map.put("title",filename); - return sendContent(ex,map); - + return doPost(ex,filename); } private boolean postUserDirectory(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { @@ -256,24 +276,6 @@ public class FileModule extends BaseHandler implements FileService { var filename = "/user/"+uid; if (!path.empty()) filename += "/"+URLDecoder.decode(path.toString(),UTF_8); if (uid != user.id() && !fileDb.isPermitted(user,filename)) throw forbidden("You are not allowed to access {0}",filename); - var file = new File(baseDir+filename); - if (contentType(ex).isPresent()) { // file upload - var out = new FileOutputStream(file); - var body = ex.getRequestBody(); - body.transferTo(out); - body.close(); - out.close(); - } else { - if (file.exists()) throw unprocessable("{0} already exists!", filename); - try { - file.mkdirs(); - } catch (Exception e) { - throw unprocessable("Failed to create {0}", filename); - } - } - Map map = getDirectory(file.getParentFile()); - map.put("title",filename); - return sendContent(ex,map); - + return doPost(ex,filename); } }