working on file module
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -30,7 +30,7 @@ public class ModuleRegistry {
|
||||
case BookmarkService bs: singleton.bookmarkService = bs; break;
|
||||
case CompanyService cs: singleton.companyService = cs; break;
|
||||
case DocumentService ds: singleton.documentService = ds; break;
|
||||
case FileService fs singleton.fileService = fs; breaK;
|
||||
case FileService fs: singleton.fileService = fs; break;
|
||||
case ItemService is: singleton.itemService = is; break;
|
||||
case MarkdownService ms: singleton.markdownService = ms; break;
|
||||
case NoteService ns: singleton.noteService = ns; break;
|
||||
|
||||
@@ -2,4 +2,7 @@ package de.srsoftware.umbrella.files;
|
||||
|
||||
public class Constants {
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.files.database";
|
||||
public static final String CONFIG_FILESTORE = "umbrella.modules.files.base_dir";
|
||||
public static final String FILE = "file";
|
||||
public static final String TABLE_FILE_SHARES = "file_shares";
|
||||
}
|
||||
|
||||
@@ -1,24 +1,78 @@
|
||||
package de.srsoftware.umbrella.files;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.api.FileService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
|
||||
import static de.srsoftware.umbrella.files.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.files.Constants.CONFIG_FILESTORE;
|
||||
|
||||
public class FileModule extends BaseHandler implements FileService {
|
||||
|
||||
FileDb fileDb;
|
||||
private final File baseDir;
|
||||
private final FileDb fileDb;
|
||||
|
||||
public FileModule(Configuration config) throws UmbrellaException {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var filestore = config.get(CONFIG_FILESTORE).orElseThrow(() -> missingFieldException(CONFIG_FILESTORE));
|
||||
baseDir = new File(filestore.toString());
|
||||
if (!baseDir.exists()) try {
|
||||
Files.createDirectories(baseDir.toPath());
|
||||
} catch (IOException e) {
|
||||
throw unprocessable("Failed to create {0}",baseDir);
|
||||
}
|
||||
if (!baseDir.isDirectory()) throw unprocessable("{0} is not a directory!");
|
||||
fileDb = new SqliteDb(connect(dbFile));
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(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 -> getCompanyFiles(path, ex, user.get());
|
||||
case PROJECT -> getProjectFiles(path, ex, user.get());
|
||||
case USER -> getUserFiles(path, ex, user.get());
|
||||
case null, default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getCompanyFiles(Path path, HttpExchange ex, UmbrellaUser user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean getProjectFiles(Path path, HttpExchange ex, UmbrellaUser user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean getUserFiles(Path path, HttpExchange ex, UmbrellaUser user) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,39 @@
|
||||
package de.srsoftware.umbrella.files;
|
||||
|
||||
import java.sql.Connection;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
|
||||
public class SqliteDb implements FileDb {
|
||||
private final Connection db;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.USER_ID;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.files.Constants.FILE;
|
||||
import static de.srsoftware.umbrella.files.Constants.TABLE_FILE_SHARES;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
public class SqliteDb extends BaseDb implements FileDb {
|
||||
|
||||
public SqliteDb(Connection conn) {
|
||||
this.db = conn;
|
||||
super(conn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int createTables() {
|
||||
int currentVersion = createSettingsTable();
|
||||
switch (currentVersion){
|
||||
case 0:
|
||||
createShareTable();
|
||||
}
|
||||
|
||||
return setCurrentVersion(1);
|
||||
}
|
||||
|
||||
private void createShareTable() {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS {0} ({1} VARCHAR(2048) NOT NULL, {2} INT NOT NULL, PRIMARY KEY({1}, {2}))";
|
||||
try {
|
||||
db.prepareStatement(format(sql, TABLE_FILE_SHARES,FILE,USER_ID)).execute();
|
||||
} catch (SQLException e){
|
||||
throw databaseException(e.getMessage()).causedBy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@
|
||||
<Route path="/document/add" component={AddDoc} />
|
||||
<Route path="/document/:id/send" component={SendDoc} />
|
||||
<Route path="/document/:id/view" component={ViewDoc} />
|
||||
<Route path="/files" component={FileIndex} />
|
||||
<Route path="/files/*" component={FileIndex} />
|
||||
<Route path="/message/settings" component={Messages} />
|
||||
<Route path="/notes" component={Notes} />
|
||||
<Route path="/project" component={ProjectList} />
|
||||
|
||||
@@ -1,5 +1,58 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { useTinyRouter } from 'svelte-tiny-router';
|
||||
import { api } from '../../urls.svelte';
|
||||
import { error } from '../../warn.svelte';
|
||||
import { t } from '../../translations.svelte';
|
||||
import { user } from '../../user.svelte';
|
||||
|
||||
const router = useTinyRouter();
|
||||
let children = $state({});
|
||||
|
||||
async function loadChildren(path){
|
||||
path = path.substring(6);
|
||||
if (path == '') path = '/';
|
||||
children = { dirs : {}};
|
||||
if (path == '/'){
|
||||
children.dirs[t('my_files')] = `user/${user.id}`;
|
||||
children.dirs[t('projects')] = `projects`;
|
||||
children.dirs[t('companies')] = `company`;
|
||||
} else {
|
||||
const url = api(`files${path}`);
|
||||
const res = await fetch(url,{credentials:'include'});
|
||||
if (res.ok){
|
||||
} else {
|
||||
error(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onclick(ev){
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
|
||||
var target = ev.target;
|
||||
while (target && !target.href) target=target.parentNode;
|
||||
let href = target.getAttribute('href');
|
||||
if (href) {
|
||||
router.navigate(href);
|
||||
loadChildren(href);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
onMount(() => loadChildren(window.location.pathname));
|
||||
</script>
|
||||
|
||||
<h1>{t('files')}</h1>
|
||||
<h1>{t('files')} – {router.path}</h1>
|
||||
|
||||
{#if children?.dirs}
|
||||
<ul>
|
||||
{#each Object.entries(children.dirs) as [k,v]}
|
||||
<li>
|
||||
<a href={router.fullPath+'/'+v} {onclick}>{k}</a>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
||||
@@ -25,7 +25,6 @@ public class CompanyLegacy extends BaseHandler {
|
||||
|
||||
public CompanyLegacy(Configuration config) {
|
||||
super();
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user