7 changed files with 146 additions and 9 deletions
@ -1,24 +1,78 @@ |
|||||||
package de.srsoftware.umbrella.files; |
package de.srsoftware.umbrella.files; |
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange; |
||||||
import de.srsoftware.configuration.Configuration; |
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.BaseHandler; |
||||||
import de.srsoftware.umbrella.core.ModuleRegistry; |
import de.srsoftware.umbrella.core.ModuleRegistry; |
||||||
import de.srsoftware.umbrella.core.api.FileService; |
import de.srsoftware.umbrella.core.api.FileService; |
||||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
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.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.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_DATABASE; |
||||||
|
import static de.srsoftware.umbrella.files.Constants.CONFIG_FILESTORE; |
||||||
|
|
||||||
public class FileModule extends BaseHandler implements FileService { |
public class FileModule extends BaseHandler implements FileService { |
||||||
|
|
||||||
FileDb fileDb; |
private final File baseDir; |
||||||
|
private final FileDb fileDb; |
||||||
|
|
||||||
public FileModule(Configuration config) throws UmbrellaException { |
public FileModule(Configuration config) throws UmbrellaException { |
||||||
super(); |
super(); |
||||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
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)); |
fileDb = new SqliteDb(connect(dbFile)); |
||||||
ModuleRegistry.add(this); |
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; |
package de.srsoftware.umbrella.files; |
||||||
|
|
||||||
|
import de.srsoftware.umbrella.core.BaseDb; |
||||||
|
|
||||||
import java.sql.Connection; |
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 implements FileDb { |
public class SqliteDb extends BaseDb implements FileDb { |
||||||
private final Connection db; |
|
||||||
|
|
||||||
public SqliteDb(Connection conn) { |
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); |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
@ -1,5 +1,58 @@ |
|||||||
<script> |
<script> |
||||||
import { t } from '../../translations.svelte'; |
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> |
</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} |
||||||
|
|||||||
Loading…
Reference in new issue