working on file module
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user