Skip to content

Commit

Permalink
Add incoming directory support
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Dec 15, 2023
1 parent aad4217 commit b8087b8
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 7 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/io/xeres/app/application/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

@Component
Expand Down Expand Up @@ -110,6 +113,8 @@ public void run(ApplicationArguments args)
return;
}

configureDefaults();

if (networkService.checkReadiness())
{
splashService.status("Starting network");
Expand Down Expand Up @@ -259,4 +264,29 @@ private void syncAutoStart()
}
}
}

/**
* Configures defaults that cannot be done on the database definition because
* they depend on some runtime parameters. This is not called in UI client
* only mode.
*/
private void configureDefaults()
{
if (!settingsService.hasIncomingDirectory() && dataDirConfiguration.getDataDir() != null) // Don't do it for tests
{
var incomingDirectory = Path.of(dataDirConfiguration.getDataDir(), "Incoming");
if (Files.notExists(incomingDirectory))
{
try
{
Files.createDirectory(incomingDirectory);
}
catch (IOException e)
{
throw new IllegalStateException("Couldn't create incoming directory: " + incomingDirectory + ", :" + e.getMessage());
}
}
settingsService.setIncomingDirectory(incomingDirectory.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class Settings

private boolean autoStartEnabled;

private String incomingDirectory;

protected Settings()
{
}
Expand Down Expand Up @@ -199,4 +201,14 @@ public void setAutoStartEnabled(boolean autoStartEnabled)
{
this.autoStartEnabled = autoStartEnabled;
}

public String getIncomingDirectory()
{
return incomingDirectory;
}

public void setIncomingDirectory(String incomingDirectory)
{
this.incomingDirectory = incomingDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public static SettingsDTO toDTO(Settings settings)
settings.isUpnpEnabled(),
settings.isBroadcastDiscoveryEnabled(),
settings.isDhtEnabled(),
settings.isAutoStartEnabled()
settings.isAutoStartEnabled(),
settings.getIncomingDirectory()
);
}

Expand All @@ -63,6 +64,7 @@ public static Settings fromDTO(SettingsDTO dto)
settings.setBroadcastDiscoveryEnabled(dto.broadcastDiscoveryEnabled());
settings.setDhtEnabled(dto.dhtEnabled());
settings.setAutoStartEnabled(dto.autoStartEnabled());
settings.setIncomingDirectory(dto.incomingDirectory());
return settings;
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/io/xeres/app/service/SettingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException;
import io.micrometer.common.util.StringUtils;
import io.xeres.app.application.events.SettingsChangedEvent;
import io.xeres.app.database.model.settings.Settings;
import io.xeres.app.database.model.settings.SettingsMapper;
Expand Down Expand Up @@ -265,4 +266,20 @@ public boolean isAutoStartEnabled()
{
return settings.isAutoStartEnabled();
}

public boolean hasIncomingDirectory()
{
return StringUtils.isNotEmpty(settings.getIncomingDirectory());
}

public String getIncomingDirectory()
{
return settings.getIncomingDirectory();
}

public void setIncomingDirectory(String directory)
{
settings.setIncomingDirectory(directory);
settingsRepository.save(settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--
-- Add incoming directory to settings
--
ALTER TABLE settings ADD COLUMN incoming_directory VARCHAR(1024) DEFAULT NULL AFTER auto_start_enabled;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public record SettingsDTO(
boolean upnpEnabled,
boolean broadcastDiscoveryEnabled,
boolean dhtEnabled,
boolean autoStartEnabled
boolean autoStartEnabled,
String incomingDirectory
)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static SettingsDTO create()
BooleanFakes.create(),
BooleanFakes.create(),
BooleanFakes.create(),
BooleanFakes.create());
BooleanFakes.create(),
"/foo/bar");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package io.xeres.ui.controller.settings;

import io.xeres.ui.JavaFxApplication;
import io.xeres.ui.model.settings.Settings;
import io.xeres.ui.support.util.TextFieldUtils;
import io.xeres.ui.support.util.UiUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
Expand All @@ -29,6 +31,9 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Path;

import static javafx.scene.control.Alert.AlertType.INFORMATION;

@Component
@FxmlView(value = "/view/settings/settings_transfer.fxml")
Expand All @@ -40,14 +45,23 @@ public class SettingsTransferController implements SettingsController
@FXML
private Button incomingDirectorySelector;

private Settings settings;

@Override
public void initialize() throws IOException
{
incomingDirectorySelector.setOnAction(event -> {
// XXX: this is all wrong. I cannot use that because the file system is actually REMOTE. have to write everything by hand (browsing, selection, etc...)
if (JavaFxApplication.isRemoteUiClient())
{
UiUtils.alert(INFORMATION, "Cannot chose a directory in remote mode");
return;
}
var directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Select Incoming Directory");
//directoryChooser.setInitialDirectory(Path.of()); ... how to get dataDir? have to ask the server... actually... the selection would be remote :) this is hard...
if (settings.hasIncomingDirectory())
{
directoryChooser.setInitialDirectory(Path.of(settings.getIncomingDirectory()).toFile());
}
var selectedDirectory = directoryChooser.showDialog(UiUtils.getWindow(event));
if (selectedDirectory != null && selectedDirectory.isDirectory())
{
Expand All @@ -59,12 +73,16 @@ public void initialize() throws IOException
@Override
public void onLoad(Settings settings)
{
this.settings = settings;

incomingDirectory.setText(settings.getIncomingDirectory());
}

@Override
public Settings onSave()
{
return null;
settings.setIncomingDirectory(TextFieldUtils.getString(incomingDirectory));

return settings;
}
}
19 changes: 19 additions & 0 deletions ui/src/main/java/io/xeres/ui/model/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package io.xeres.ui.model.settings;

import io.micrometer.common.util.StringUtils;

public class Settings implements Cloneable
{
private String torSocksHost;
Expand All @@ -35,6 +37,8 @@ public class Settings implements Cloneable

private boolean autoStartEnabled;

private String incomingDirectory;

public String getTorSocksHost()
{
return torSocksHost;
Expand Down Expand Up @@ -115,6 +119,21 @@ public void setAutoStartEnabled(boolean autoStartEnabled)
this.autoStartEnabled = autoStartEnabled;
}

public boolean hasIncomingDirectory()
{
return StringUtils.isNotEmpty(incomingDirectory);
}

public String getIncomingDirectory()
{
return incomingDirectory;
}

public void setIncomingDirectory(String incomingDirectory)
{
this.incomingDirectory = incomingDirectory;
}

@Override
public Settings clone()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static Settings fromDTO(SettingsDTO dto)
settings.setBroadcastDiscoveryEnabled(dto.broadcastDiscoveryEnabled());
settings.setDhtEnabled(dto.dhtEnabled());
settings.setAutoStartEnabled(dto.autoStartEnabled());
settings.setIncomingDirectory(dto.incomingDirectory());
return settings;
}

Expand All @@ -63,7 +64,8 @@ public static SettingsDTO toDTO(Settings settings)
settings.isUpnpEnabled(),
settings.isBroadcastDiscoveryEnabled(),
settings.isDhtEnabled(),
settings.isAutoStartEnabled()
settings.isAutoStartEnabled(),
settings.getIncomingDirectory()
);
}
}

0 comments on commit b8087b8

Please sign in to comment.