Skip to content

Commit

Permalink
Fixes #84
Browse files Browse the repository at this point in the history
  • Loading branch information
Milchreis committed Jun 30, 2019
1 parent 8e4d55b commit c043209
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 190 deletions.
7 changes: 7 additions & 0 deletions phobox-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
<version>1.18.4</version>
</dependency>

<!-- Mapping from yaml to POJO -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.2</version>
</dependency>

<!-- Embedded database -->
<dependency>
<groupId>com.h2database</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.milchreis.phobox;

import java.io.File;
import java.io.IOException;

import de.milchreis.phobox.core.config.PreferencesManager;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
Expand All @@ -16,7 +18,7 @@
@Slf4j
public class CLIManager {

public static void parse(String[] args) throws ParseException {
public static void parse(String[] args) throws ParseException, IOException {

PhoboxModel model = Phobox.getModel();
CommandLineParser parser = new DefaultParser();
Expand Down Expand Up @@ -45,6 +47,9 @@ public static void parse(String[] args) throws ParseException {

if(line.hasOption("storage")) {
model.setStoragePath(line.getOptionValue("storage"));
} else {
PreferencesManager.init();
model.setStoragePath(PreferencesManager.getStoragePath().getAbsolutePath());
}

if(line.hasOption("watchDirectory")) {
Expand All @@ -56,7 +61,7 @@ public static void parse(String[] args) throws ParseException {
}

if(line.hasOption("port")) {
model.setPort(Integer.parseInt(line.getOptionValue("port")));
model.getStorageConfiguration().setPhoboxPort(Integer.parseInt(line.getOptionValue("port")));
}

if(line.hasOption("help")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.milchreis.phobox.core.Phobox;
import de.milchreis.phobox.core.config.PreferencesManager;
import de.milchreis.phobox.core.config.StorageConfiguration;
import de.milchreis.phobox.core.events.BasicEvent;
import de.milchreis.phobox.core.events.UpdateDatabaseEvent;
import de.milchreis.phobox.core.model.PhoboxModel;
Expand All @@ -18,6 +19,8 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
Expand All @@ -34,8 +37,12 @@ public static void main(String[] args) {
// CLI
try {
CLIManager.parse(args);

} catch (ParseException e) {
log.error("Could not parse the commandline arguments.");

} catch (IOException e) {
log.error("Could not load storage configuration file");
}

gui = StartupHelper.createGui();
Expand All @@ -44,23 +51,24 @@ public static void main(String[] args) {

PhoboxModel model = Phobox.getModel();

// Ask for default Storage-Path on first run (no properties file found)
if(StartupHelper.isFirstRun()) {
if (model.isActiveGui()) {
gui.showStorageInitalization();
} else {
StorageAsk.askWithCLI();
try {
// Ask for default Storage-Path on first run (no properties file found)
if(StartupHelper.isFirstRun()) {
if (model.isActiveGui()) {
gui.showStorageInitalization();
} else {
StorageAsk.askWithCLI();
}
}
}

if(model.isActiveGui()) {
gui.showSplash();
}
if(model.isActiveGui()) {
gui.showSplash();
}

try {
// Update the storage path
model.setStoragePath(PreferencesManager.get(PreferencesManager.STORAGE_PATH));
model.setImportFormat(PreferencesManager.get(PreferencesManager.IMPORT_FORMAT));
StorageConfiguration config = Phobox.getModel().getStorageConfiguration();

// Setup the spring params depending on storage path
System.setProperty("server.port", config.getPhoboxPort()+"");
System.setProperty("phobox.logging.path", model.getPhoboxPath().getAbsolutePath());

SpringApplication.run(PhoboxServerApplication.class, args);
Expand Down Expand Up @@ -90,8 +98,11 @@ public void run(String... args) {
Phobox.getEventRegistry().onCreation();

if(Phobox.getModel().isActiveGui()) {

gui.showUpload();
Browser.open("http://localhost:"+Phobox.getModel().getPort());

StorageConfiguration config = Phobox.getModel().getStorageConfiguration();
Browser.open("http://localhost:" + config.getPhoboxPort());
}
}
}
20 changes: 9 additions & 11 deletions phobox-server/src/main/java/de/milchreis/phobox/core/Phobox.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;

import de.milchreis.phobox.core.config.PreferencesManager;
import de.milchreis.phobox.core.config.StorageConfiguration;
import de.milchreis.phobox.core.events.EventRegistry;
import de.milchreis.phobox.core.file.FileProcessor;
import de.milchreis.phobox.core.model.PhoboxModel;
Expand Down Expand Up @@ -77,26 +78,23 @@ public static void addPathToScanQueue(File path) {

public static void startSchedules() {
Phobox phobox = getInstance();

StorageConfiguration config = getModel().getStorageConfiguration();

// Initialize the scheduler for importing and scanning new files
phobox.importScheduler = new ImportScheduler(3000, 100);
phobox.copyScheduler = new CopyScheduler(3000);
phobox.storageScanScheduler = new StorageScanScheduler(24, getModel().getStoragePathAsFile(), BeanUtil.getBean(ItemRepository.class), true);
phobox.scanQueue = new StorageScanQueue(BeanUtil.getBean(ItemRepository.class));

if(config.hasValidStorageScanTime()) {
int[] storageScantime = config.getStorageScantime();
phobox.storageScanScheduler = new StorageScanScheduler(storageScantime[0], storageScantime[1], 24, getModel().getStoragePathAsFile(), BeanUtil.getBean(ItemRepository.class), true);
phobox.storageScanScheduler.start();
}

phobox.copyScheduler.start();
phobox.storageScanScheduler.start();
phobox.importScheduler.start();
}

public static void changeStoragePath(File path) {
PreferencesManager.set(PreferencesManager.STORAGE_PATH, path.getAbsolutePath());

// TODO: Currently not working
// getModel().setStoragePath(path.getAbsolutePath());
// BeanUtil.getBean(StaticResourceConfiguration.class).update();
}

public static ThumbnailOperations getThumbnailOperations() {
return getOperations().getThumbnailOperations();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void process(File file, LoopInfo info) {
}

try {
ImportFormatter importFormatter = new ImportFormatter(model.getImportFormat());
ImportFormatter importFormatter = new ImportFormatter(model.getStorageConfiguration().getImportFormat());
File dirStructure = importFormatter.createPath(file);
File target = new File(storage, dirStructure.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void process(File file, LoopInfo info) {
int orientation = 1;

try {
ImportFormatter importFormatter = new ImportFormatter(model.getImportFormat());
ImportFormatter importFormatter = new ImportFormatter(model.getStorageConfiguration().getImportFormat());
File dirStructure = importFormatter.createPath(file);
thumbPath = new File(thumbPath, dirStructure.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Properties;
import java.util.TreeMap;

import lombok.Getter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,62 +22,33 @@
@Slf4j
public class PreferencesManager {

public static final String STORAGE_PATH = "storage.path";
public static final String USERS = "users";
public static final String PASSWORDS = "passwords";
public static final String IMPORT_FORMAT = "storage.import.format";

@Getter
private static File storagePath;
private static File file = new File(System.getProperty("user.home"), ".phobox.properties");
private static Properties props = init();

public static void unset(String key) {
props.remove(key);
save();
}


public static String get(String key) {
return props.getProperty(key);
}

public static void set(String key, String value) {
props.setProperty(key, value);

public static void setStoragePath(File path) {
storagePath = path;
save();
}


private static void save() {
try(OutputStream os = new FileOutputStream(file)) {
Properties props = new Properties();
props.put("storage.path", storagePath.getAbsolutePath());
props.store(os, " Preferences for the Phobox image application");

} catch (Exception e) {
log.error("Error while saving properties");
}
}

public static Map<String, String> getUserMap() {
Map<String, String> userPassMap = new TreeMap<>();

String users = get(USERS);
String passwords = get(PASSWORDS);

if(users != null && passwords != null) {
String[] userArray = users.replace(", ", ",").split(",");
String[] passArray = passwords.replace(", ", ",").split(",");

for(int i=0; i<userArray.length; i++) {
userPassMap.put(userArray[i], passArray[i]);
}
}

return userPassMap;
}

private static Properties init() {

public static Properties init() {
Properties props = new Properties();

try(InputStream is = new FileInputStream(file)) {
props.load(is);
storagePath = new File(props.getProperty("storage.path"));

} catch (Exception e) {
try {
file.createNewFile();
Expand All @@ -86,34 +58,4 @@ private static Properties init() {
}
return props;
}

public static void addUser(String username, String password) {
if(username == null && password == null)
return;

String users = get(USERS);
String passwords = get(USERS);

Map<String, String> userMap = getUserMap();

if(userMap.containsKey(username))
return;

if(users == null) {
users = username;
} else {
users += "," + username;
}

BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

if(passwords == null) {
passwords = bCryptPasswordEncoder.encode(password);
} else {
passwords += "," + bCryptPasswordEncoder.encode(password);
}

set(USERS, users);
set(PASSWORDS, passwords);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.milchreis.phobox.core.config;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import de.milchreis.phobox.core.model.UserCredentials;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
@Data
public class StorageConfiguration {

private String importFormat = "%Y/%Y-%M/%Y-%M-%D";
private List<UserCredentials> loginCredentials;
private String storageScan = "23:00";
private Integer phoboxPort = 8080;

public static StorageConfiguration load(File storageConfigFile) throws IOException {
if(storageConfigFile == null || !storageConfigFile.exists()) {
return new StorageConfiguration();

} else {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return mapper.readValue(storageConfigFile, StorageConfiguration.class);
}
}

public void write(File storageConfigFile) throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.writeValue(storageConfigFile, this);
}

public boolean hasValidStorageScanTime() {

if(storageScan == null || storageScan.isEmpty() || !storageScan.contains(":"))
return false;

try {
getStorageScantime();
return true;
} catch (Exception e) {
log.error("StorageScan property as no valid format: " + storageScan + " - Expects HH:MM", e);
return false;
}
}

@JsonIgnore
public int[] getStorageScantime() {
try {
String[] parts = storageScan.split(":");
int hours = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);

return new int[]{hours, minutes};

} catch (Exception e) {
throw new IllegalArgumentException("StorageScan property as no valid format: " + storageScan + " - Expects HH:MM");
}
}

}
Loading

0 comments on commit c043209

Please sign in to comment.