implemented user file upload and project file upload
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -13,6 +13,7 @@ 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;
|
||||
@@ -23,9 +24,8 @@ 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;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
@@ -205,7 +205,43 @@ public class FileModule extends BaseHandler implements FileService {
|
||||
}
|
||||
|
||||
private boolean postProjectDirectory(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 += "/"+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<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 {
|
||||
@@ -221,18 +257,19 @@ public class FileModule extends BaseHandler implements FileService {
|
||||
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);
|
||||
var contentType = contentType(ex).orElse(null);
|
||||
if (MIME_FORM_DATA.equals(contentType)) { // file upload
|
||||
// TODO: create parent directory if it does not exist
|
||||
// TODO: create file and write content
|
||||
return false;
|
||||
}
|
||||
|
||||
if (file.exists()) throw unprocessable("{0} already exists!",filename);
|
||||
try {
|
||||
file.mkdirs();
|
||||
} catch (Exception e) {
|
||||
throw unprocessable("Failed to create {0}",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);
|
||||
|
||||
Reference in New Issue
Block a user