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

Commit

Permalink
Fix usage of file separators in file names on Windows (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Dec 6, 2017
1 parent ed080a7 commit 5d07649
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CreateCloudDirTask(Bucket bucket, Path path) {
@Override
public void run() {
try {
String dirName = Utils.getSyncDir().relativize(path).toString() + "/";
String dirName = Utils.getStorjName(path);
File storjDir = getCloudDir(dirName);
if (storjDir != null) {
DB.setSynced(storjDir, path);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/goobox/sync/storj/DeleteLocalFileTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public DeleteLocalFileTask(Path path) {

@Override
public void run() {
Path relPath = Utils.getSyncDir().relativize(path);
System.out.printf("Deleting local %s %s...\n", Files.isDirectory(path) ? "directory" : "file", relPath);
System.out.printf("Deleting local %s %s...\n",
Files.isDirectory(path) ? "directory" : "file",
Utils.getStorjName(path));

try {
boolean success = Files.deleteIfExists(path);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/goobox/sync/storj/UploadFileTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class UploadFileTask implements Runnable {

private Bucket bucket;
private Path path;
private String relPath;
private String fileName;

public UploadFileTask(Bucket bucket, Path path) {
this.bucket = bucket;
this.path = path;
this.relPath = Utils.getSyncDir().relativize(path).toString();
this.fileName = Utils.getStorjName(path);
}

@Override
Expand All @@ -49,9 +49,9 @@ public void run() {
return;
}

System.out.println("Uploading file " + relPath + "... ");
System.out.println("Uploading file " + fileName + "... ");

Storj.getInstance().uploadFile(bucket, relPath, path, new UploadFileCallback() {
Storj.getInstance().uploadFile(bucket, fileName, path, new UploadFileCallback() {
@Override
public void onProgress(String filePath, double progress, long uploadedBytes, long totalBytes) {
String progressMessage = String.format(" %3d%% %15d/%d bytes",
Expand Down Expand Up @@ -129,7 +129,7 @@ private void deleteIfExisting() throws InterruptedException {
public void onFilesReceived(File[] files) {
File storjFile = null;
for (File f : files) {
if (relPath.equals(f.getName())) {
if (fileName.equals(f.getName())) {
storjFile = f;
}
}
Expand All @@ -139,7 +139,7 @@ public void onFilesReceived(File[] files) {
repeat[0] = false;
latch.countDown();
} else {
System.out.print("Deleting old version of " + relPath + " on the cloud... ");
System.out.print("Deleting old version of " + fileName + " on the cloud... ");

Storj.getInstance().deleteFile(bucket, storjFile, new DeleteFileCallback() {
@Override
Expand All @@ -161,7 +161,7 @@ public void onError(String message) {
@Override
public void onError(String message) {
System.out.println(String.format("Error checking if file with name %s exists: %s. Trying again...",
relPath, message));
fileName, message));
latch.countDown();
}
});
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/goobox/sync/storj/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
Expand Down Expand Up @@ -62,6 +63,15 @@ public static long getTime(String storjTimestamp) throws ParseException {
return date.getTime();
}

public static String getStorjName(Path path) {
String name = getSyncDir().relativize(path).toString();
name = name.replace('\\', '/');
if (Files.isDirectory(path)) {
name += "/";
}
return name;
}

private static String getOsName() {
if (OS == null) {
OS = System.getProperty("os.name");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/goobox/sync/storj/db/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static String getName(File file) {
}

public static String getName(Path path) {
return Utils.getSyncDir().relativize(path).toString();
return Utils.getStorjName(path).replaceAll("/+$", ""); // remove trailing slash
}

public synchronized static void close() {
Expand Down

0 comments on commit 5d07649

Please sign in to comment.