working on directory creation
This commit is contained in:
@@ -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 {
|
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();
|
||||||
@@ -102,6 +121,33 @@ public class FileModule extends BaseHandler implements FileService {
|
|||||||
return getFile(ex, file);
|
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 {
|
private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||||
var prjId = path.pop();
|
var prjId = path.pop();
|
||||||
var projects = projectService();
|
var projects = projectService();
|
||||||
@@ -131,33 +177,6 @@ public class FileModule extends BaseHandler implements FileService {
|
|||||||
return getFile(ex, file);
|
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 {
|
private boolean getUserFiles(Path path, HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||||
var userId = path.pop();
|
var userId = path.pop();
|
||||||
if (userId == null) throw missingFieldException(USER_ID);
|
if (userId == null) throw missingFieldException(USER_ID);
|
||||||
@@ -179,4 +198,16 @@ public class FileModule extends BaseHandler implements FileService {
|
|||||||
}
|
}
|
||||||
return getFile(ex, file);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
alert(url);
|
alert(url);
|
||||||
const res = await fetch(url,{
|
const res = await fetch(url,{
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
method: 'PUT'
|
method: 'POST'
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
yikes();
|
yikes();
|
||||||
|
|||||||
Reference in New Issue
Block a user