Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Configurable sync folder location (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Jan 7, 2018
1 parent 647763d commit a6cdb0d
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 38 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>io.goobox</groupId>
<artifactId>goobox-sync-common</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
</dependency>
<dependency>
<groupId>io.storj</groupId>
Expand Down Expand Up @@ -94,6 +94,11 @@
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
63 changes: 58 additions & 5 deletions src/main/java/io/goobox/sync/storj/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liferay.nativity.control.NativityControl;
import com.liferay.nativity.control.NativityControlUtil;
import com.liferay.nativity.modules.fileicon.FileIconControl;
Expand All @@ -31,6 +40,7 @@
import io.goobox.sync.common.Utils;
import io.goobox.sync.common.systemtray.ShutdownListener;
import io.goobox.sync.common.systemtray.SystemTrayHelper;
import io.goobox.sync.storj.db.DB;
import io.goobox.sync.storj.overlay.OverlayHelper;
import io.storj.libstorj.Bucket;
import io.storj.libstorj.CreateBucketCallback;
Expand All @@ -40,16 +50,55 @@

public class App implements ShutdownListener {

private static final Logger logger = LoggerFactory.getLogger(App.class);

private static App instance;

private Path syncDir;

private Storj storj;
private Bucket gooboxBucket;
private TaskQueue tasks;
private TaskExecutor taskExecutor;
private FileWatcher fileWatcher;

public App() {
this.syncDir = Utils.getSyncDir();
}

public App(Path syncDir) {
this.syncDir = syncDir;
}

public static void main(String[] args) {
instance = new App();
Options opts = new Options();
opts.addOption(Option.builder()
.longOpt("reset-db")
.desc("reset sync DB")
.build());
opts.addOption(Option.builder()
.longOpt("sync-dir")
.hasArg()
.desc("set the sync dir")
.build());

try {
CommandLine cmd = new DefaultParser().parse(opts, args);

if (cmd.hasOption("reset-db")) {
DB.reset();
}

if (cmd.hasOption("sync-dir")) {
instance = new App(Paths.get(cmd.getParsedOptionValue("sync-dir").toString()));
} else {
instance = new App();
}
} catch (ParseException e) {
logger.error("Failed to parse command line options: {}", e.getMessage());
System.exit(1);
}

instance.init();

NativityControl nativityControl = NativityControlUtil.getNativityControl();
Expand All @@ -75,7 +124,7 @@ public int getIconForFile(String path) {

fileIconControl.enableFileIcons();

String testFilePath = Utils.getSyncDir().toString();
String testFilePath = instance.getSyncDir().toString();

if (OSDetector.isWindows()) {
// This id is determined when building the DLL
Expand All @@ -100,6 +149,10 @@ public static App getInstance() {
return instance;
}

public Path getSyncDir() {
return syncDir;
}

public Storj getStorj() {
return storj;
}
Expand All @@ -121,12 +174,12 @@ public FileWatcher getFileWatcher() {
}

private void init() {
SystemTrayHelper.setIdle();
SystemTrayHelper.init(syncDir);
SystemTrayHelper.setShutdownListener(this);

storj = new Storj();
storj.setConfigDirectory(StorjUtil.getStorjConfigDir().toFile());
storj.setDownloadDirectory(Utils.getSyncDir().toFile());
storj.setDownloadDirectory(syncDir.toFile());

if (!checkAndCreateSyncDir()) {
System.exit(1);
Expand Down Expand Up @@ -160,7 +213,7 @@ public void shutdown() {

private boolean checkAndCreateSyncDir() {
System.out.print("Checking if local Goobox sync folder exists... ");
return checkAndCreateFolder(Utils.getSyncDir());
return checkAndCreateFolder(getSyncDir());
}

private boolean checkAndCreateDataDir() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/goobox/sync/storj/CheckStateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private List<Path> getLocalPaths() {
Deque<Path> stack = new ArrayDeque<Path>();
List<Path> paths = new ArrayList<>();

stack.push(Utils.getSyncDir());
stack.push(App.getInstance().getSyncDir());

while (!stack.isEmpty()) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(stack.pop())) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/goobox/sync/storj/CreateLocalDirTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,7 +19,6 @@
import java.nio.file.Files;
import java.nio.file.Path;

import io.goobox.sync.common.Utils;
import io.goobox.sync.storj.db.DB;
import io.storj.libstorj.File;

Expand All @@ -36,7 +35,7 @@ public void run() {
System.out.print("Creating local directory " + storjDir.getName() + "... ");

try {
Path localDir = Files.createDirectories(Utils.getSyncDir().resolve(storjDir.getName()));
Path localDir = Files.createDirectories(App.getInstance().getSyncDir().resolve(storjDir.getName()));
System.out.println("done");
DB.setSynced(storjDir, localDir);
DB.commit();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/goobox/sync/storj/DeleteLocalFileTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,6 @@
import java.nio.file.Files;
import java.nio.file.Path;

import io.goobox.sync.common.Utils;
import io.goobox.sync.storj.db.DB;

public class DeleteLocalFileTask implements Runnable {
Expand Down Expand Up @@ -54,7 +53,7 @@ public void run() {

private void deleteParentIfEmpty() {
Path parent = path.getParent();
if (!parent.equals(Utils.getSyncDir())) {
if (!parent.equals(App.getInstance().getSyncDir())) {
try {
Files.deleteIfExists(parent);
DB.remove(parent);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/goobox/sync/storj/DownloadFileTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import io.goobox.sync.common.Utils;
import io.goobox.sync.storj.db.DB;
import io.storj.libstorj.Bucket;
import io.storj.libstorj.DownloadFileCallback;
Expand All @@ -42,7 +41,7 @@ public void run() {
System.out.println("Downloading file " + file.getName() + "... ");

try {
Files.createDirectories(Utils.getSyncDir().resolve(file.getName()).getParent());
Files.createDirectories(App.getInstance().getSyncDir().resolve(file.getName()).getParent());
} catch (IOException e) {
System.out.println("Failed creating parent directories: " + e.getMessage());
}
Expand All @@ -68,7 +67,7 @@ public void onComplete(String fileId, String localPath) {

@Override
public void onError(String fileId, String message) {
Path localPath = Utils.getSyncDir().resolve(file.getName());
Path localPath = App.getInstance().getSyncDir().resolve(file.getName());
try {
DB.setDownloadFailed(file, localPath);
DB.commit();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/goobox/sync/storj/FileWatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,7 +25,6 @@
import java.util.Timer;
import java.util.TimerTask;

import io.goobox.sync.common.Utils;
import io.methvin.watcher.DirectoryChangeEvent;
import io.methvin.watcher.DirectoryChangeListener;
import io.methvin.watcher.DirectoryWatcher;
Expand All @@ -38,7 +37,7 @@ public class FileWatcher extends Thread implements DirectoryChangeListener {
@Override
public void run() {
try {
DirectoryWatcher watcher = DirectoryWatcher.create(Utils.getSyncDir(), this);
DirectoryWatcher watcher = DirectoryWatcher.create(App.getInstance().getSyncDir(), this);
watcher.watchAsync();

Timer timer = new Timer();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/goobox/sync/storj/StorjUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -39,7 +39,7 @@ public static long getTime(String storjTimestamp) throws ParseException {
}

public static String getStorjName(Path path) {
String name = Utils.getSyncDir().relativize(path).toString();
String name = App.getInstance().getSyncDir().relativize(path).toString();
name = name.replace('\\', '/');
if (Files.isDirectory(path)) {
name += "/";
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/io/goobox/sync/storj/db/DB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,6 +25,8 @@
import org.dizitart.no2.objects.ObjectFilter;
import org.dizitart.no2.objects.ObjectRepository;
import org.dizitart.no2.objects.filters.ObjectFilters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.goobox.sync.common.Utils;
import io.goobox.sync.storj.StorjUtil;
Expand All @@ -33,6 +35,8 @@

public class DB {

private static final Logger logger = LoggerFactory.getLogger(DB.class);

private static Nitrite db;

private static Nitrite db() {
Expand All @@ -51,13 +55,25 @@ private static ObjectFilter withName(String fileName) {
}

private static Nitrite open() {
Path dbPath = Utils.getDataDir().resolve("sync.db");
return Nitrite.builder()
.compressed()
.filePath(dbPath.toFile())
.filePath(getDBPath().toFile())
.openOrCreate();
}

public static void reset() {
logger.info("Resetting sync DB");
try {
Files.deleteIfExists(getDBPath());
} catch (IOException e) {
logger.error("Failed deleting DB file", e);
}
}

private static Path getDBPath() {
return Utils.getDataDir().resolve("sync.db");
}

public static String getName(File file) {
return file.getName().replaceAll("/+$", ""); // remove trailing slash
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/io/goobox/sync/storj/overlay/OverlayHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Kaloyan Raev
* Copyright (C) 2017-2018 Kaloyan Raev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -36,7 +36,7 @@
import com.liferay.nativity.modules.fileicon.FileIconControlUtil;
import com.liferay.nativity.util.OSDetector;

import io.goobox.sync.common.Utils;
import io.goobox.sync.storj.App;
import io.goobox.sync.storj.db.DB;

public class OverlayHelper implements FileIconControlCallback, ContextMenuControlCallback {
Expand Down Expand Up @@ -90,15 +90,15 @@ public void shutdown() {
public void refresh(Path path) {
if (fileIconControl != null && path != null) {
String[] pathAndParents = Stream.iterate(path, p -> p.getParent())
.limit(Utils.getSyncDir().relativize(path).getNameCount())
.limit(App.getInstance().getSyncDir().relativize(path).getNameCount())
.map(Path::toString)
.toArray(String[]::new);
fileIconControl.refreshIcons(pathAndParents);
}
}

private void refresh() {
fileIconControl.refreshIcons(new String[] { Utils.getSyncDir().toString() });
fileIconControl.refreshIcons(new String[] { App.getInstance().getSyncDir().toString() });
}

private void init() {
Expand All @@ -110,10 +110,11 @@ private void init() {
nativityControl.connect();

// Setting filter folders is required for Mac's Finder Sync plugin
nativityControl.setFilterFolder(Utils.getSyncDir().toString());
nativityControl.setFilterFolder(App.getInstance().getSyncDir().toString());

// Make Goobox a system folder
DosFileAttributeView attr = Files.getFileAttributeView(Utils.getSyncDir(), DosFileAttributeView.class);
DosFileAttributeView attr = Files.getFileAttributeView(App.getInstance().getSyncDir(),
DosFileAttributeView.class);
try {
attr.setSystem(true);
} catch (IOException e) {
Expand All @@ -131,9 +132,9 @@ private void init() {
@Override
public int getIconForFile(String path) {
Path p = Paths.get(path);
if (!p.startsWith(Utils.getSyncDir())) {
if (!p.startsWith(App.getInstance().getSyncDir())) {
return OverlayIcon.NONE.id();
} else if (Utils.getSyncDir().equals(p)) {
} else if (App.getInstance().getSyncDir().equals(p)) {
return globalStateIconId;
} else {
try {
Expand Down
Loading

0 comments on commit a6cdb0d

Please sign in to comment.