Browse Source

finished implementation offile upload

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/files
Stephan Richter 1 month ago
parent
commit
3248847f9c
  1. 84
      files/src/main/java/de/srsoftware/umbrella/files/FileModule.java

84
files/src/main/java/de/srsoftware/umbrella/files/FileModule.java

@ -1,7 +1,6 @@
/* © SRSoftware 2025 */ /* © SRSoftware 2025 */
package de.srsoftware.umbrella.files; 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.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.*; import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.ModuleRegistry.*; 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 com.sun.net.httpserver.HttpExchange;
import de.srsoftware.configuration.Configuration; import de.srsoftware.configuration.Configuration;
import de.srsoftware.tools.MimeType;
import de.srsoftware.tools.Path; import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken; import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler; 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<String,Object> map = getDirectory(file.getParentFile());
map.put("title",filename);
return sendContent(ex,map);
}
private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
var cpId = path.pop(); var cpId = path.pop();
var companies = companyService(); 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 { private boolean postCompanyDirectory(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
return false; var cpId = path.pop();
var companies = companyService();
Map<Long, Company> 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 { 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; var filename = "/project/"+pid;
if (!path.empty()) filename += "/"+URLDecoder.decode(path.toString(),UTF_8); 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); if (!project.hasMember(user) && !fileDb.isPermitted(user,filename)) throw forbidden("You are not allowed to access {0}",filename);
var file = new File(baseDir+filename); return doPost(ex,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<String,Object> map = getDirectory(file.getParentFile());
map.put("title",filename);
return sendContent(ex,map);
} }
private boolean postUserDirectory(Path path, HttpExchange ex, UmbrellaUser user) throws IOException { 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; var filename = "/user/"+uid;
if (!path.empty()) filename += "/"+URLDecoder.decode(path.toString(),UTF_8); 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); if (uid != user.id() && !fileDb.isPermitted(user,filename)) throw forbidden("You are not allowed to access {0}",filename);
var file = new File(baseDir+filename); return doPost(ex,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<String,Object> map = getDirectory(file.getParentFile());
map.put("title",filename);
return sendContent(ex,map);
} }
} }

Loading…
Cancel
Save