|
|
|
@ -13,6 +13,7 @@ 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; |
|
|
|
@ -23,9 +24,8 @@ import de.srsoftware.umbrella.core.model.Company; |
|
|
|
import de.srsoftware.umbrella.core.model.Project; |
|
|
|
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.FileInputStream; |
|
|
|
import java.io.*; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.net.URLDecoder; |
|
|
|
import java.net.URLDecoder; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.HashMap; |
|
|
|
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 { |
|
|
|
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 { |
|
|
|
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 (!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); |
|
|
|
var file = new File(baseDir+filename); |
|
|
|
var contentType = contentType(ex).orElse(null); |
|
|
|
if (contentType(ex).isPresent()) { // file upload
|
|
|
|
if (MIME_FORM_DATA.equals(contentType)) { // file upload
|
|
|
|
var out = new FileOutputStream(file); |
|
|
|
// TODO: create parent directory if it does not exist
|
|
|
|
var body = ex.getRequestBody(); |
|
|
|
// TODO: create file and write content
|
|
|
|
body.transferTo(out); |
|
|
|
return false; |
|
|
|
body.close(); |
|
|
|
} |
|
|
|
out.close(); |
|
|
|
|
|
|
|
} else { |
|
|
|
if (file.exists()) throw unprocessable("{0} already exists!",filename); |
|
|
|
if (file.exists()) throw unprocessable("{0} already exists!", filename); |
|
|
|
try { |
|
|
|
try { |
|
|
|
file.mkdirs(); |
|
|
|
file.mkdirs(); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw unprocessable("Failed to create {0}",filename); |
|
|
|
throw unprocessable("Failed to create {0}", filename); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Map<String,Object> map = getDirectory(file.getParentFile()); |
|
|
|
Map<String,Object> map = getDirectory(file.getParentFile()); |
|
|
|
map.put("title",filename); |
|
|
|
map.put("title",filename); |
|
|
|
|