Browse Source

working on directory creation

module/files
Stephan Richter 1 month ago
parent
commit
f57be465a9
  1. 85
      files/src/main/java/de/srsoftware/umbrella/files/FileModule.java
  2. 2
      frontend/src/routes/files/Index.svelte

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

@ -71,6 +71,25 @@ public class FileModule extends BaseHandler implements FileService { @@ -71,6 +71,25 @@ public class FileModule extends BaseHandler implements FileService {
}
}
@Override
public boolean doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head){
case COMPANY -> postCompanyDirectory(path, ex, user.get());
case PROJECT -> postProjectDirectory(path, ex, user.get());
case USER -> postUserDirectory(path, ex, user.get());
case null, default -> throw UmbrellaException.notFound("invalid location: {0}", head);
};
} catch (UmbrellaException e) {
return send(ex,e);
}
}
private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
var cpId = path.pop();
var companies = companyService();
@ -102,6 +121,33 @@ public class FileModule extends BaseHandler implements FileService { @@ -102,6 +121,33 @@ public class FileModule extends BaseHandler implements FileService {
return getFile(ex, file);
}
private Map<String,Object> getDirectory(File file) throws IOException {
var children = file.listFiles();
var map = new HashMap<String, Object>();
if (children == null) return map;
var prefixLen = baseDir.toString().length();
for (var child : children){
var o = map.computeIfAbsent(child.isDirectory() ? "dirs" : "files", k -> new HashMap<String, String>());
//noinspection unchecked
((Map<String,String>) o).put(child.toString().substring(prefixLen),child.getName());
}
return map;
}
private boolean getFile(HttpExchange ex, File file) throws IOException {
var headers = ex.getResponseHeaders();
var conn = file.toURI().toURL().openConnection();
var ct = conn.getContentType();
headers.add(CONTENT_TYPE, ct);
headers.add(CONTENT_DISPOSITION,"attachment; filename=\""+file.getName()+"\"");
ex.sendResponseHeaders(HTTP_OK, 0L);
try (var fos = new FileInputStream(file)){
fos.transferTo(ex.getResponseBody());
}
return true;
}
private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
var prjId = path.pop();
var projects = projectService();
@ -131,33 +177,6 @@ public class FileModule extends BaseHandler implements FileService { @@ -131,33 +177,6 @@ public class FileModule extends BaseHandler implements FileService {
return getFile(ex, file);
}
private boolean getFile(HttpExchange ex, File file) throws IOException {
var headers = ex.getResponseHeaders();
var conn = file.toURI().toURL().openConnection();
var ct = conn.getContentType();
headers.add(CONTENT_TYPE, ct);
headers.add(CONTENT_DISPOSITION,"attachment; filename=\""+file.getName()+"\"");
ex.sendResponseHeaders(HTTP_OK, 0L);
try (var fos = new FileInputStream(file)){
fos.transferTo(ex.getResponseBody());
}
return true;
}
private Map<String,Object> getDirectory(File file) throws IOException {
var children = file.listFiles();
var map = new HashMap<String, Object>();
if (children == null) return map;
var prefixLen = baseDir.toString().length();
for (var child : children){
var o = map.computeIfAbsent(child.isDirectory() ? "dirs" : "files", k -> new HashMap<String, String>());
//noinspection unchecked
((Map<String,String>) o).put(child.toString().substring(prefixLen),child.getName());
}
return map;
}
private boolean getUserFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
var userId = path.pop();
if (userId == null) throw missingFieldException(USER_ID);
@ -179,4 +198,16 @@ public class FileModule extends BaseHandler implements FileService { @@ -179,4 +198,16 @@ public class FileModule extends BaseHandler implements FileService {
}
return getFile(ex, file);
}
private boolean postCompanyDirectory(Path path, HttpExchange ex, UmbrellaUser umbrellaUser) {
return false;
}
private boolean postProjectDirectory(Path path, HttpExchange ex, UmbrellaUser umbrellaUser) {
return false;
}
private boolean postUserDirectory(Path path, HttpExchange ex, UmbrellaUser umbrellaUser) {
return false;
}
}

2
frontend/src/routes/files/Index.svelte

@ -39,7 +39,7 @@ @@ -39,7 +39,7 @@
alert(url);
const res = await fetch(url,{
credentials: 'include',
method: 'PUT'
method: 'POST'
});
if (res.ok) {
yikes();

Loading…
Cancel
Save