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

Commit

Permalink
#6: Delete temp dir files (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloyan-raev authored Dec 7, 2017
1 parent 5d07649 commit 2a62965
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void run() {
DB.setSynced(storjDir, path);
DB.commit();
} else {
Path tmp = Files.createTempFile("storj", "dir");
Files.write(tmp, "/".getBytes());
final Path tmp = createTempDirFile();

System.out.println("Creating cloud directory " + dirName + "... ");

Expand All @@ -62,6 +61,8 @@ public void onProgress(String filePath, double progress, long uploadedBytes, lon

@Override
public void onComplete(final String filePath, final String fileId) {
deleteTempDirFile(tmp);

final CountDownLatch latch = new CountDownLatch(1);
final boolean repeat[] = { true };

Expand Down Expand Up @@ -112,8 +113,11 @@ public void onError(String message) {
@Override
public void onError(String filePath, String message) {
try {
deleteTempDirFile(tmp);

DB.setUploadFailed(path);
DB.commit();

System.out.println(" " + message);
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -162,4 +166,18 @@ public void onError(String message) {
return result[0];
}

private Path createTempDirFile() throws IOException {
Path tmp = Files.createTempFile("storj", "dir");
Files.write(tmp, "/".getBytes());
return tmp;
}

private void deleteTempDirFile(Path tmp) {
try {
Files.deleteIfExists(tmp);
} catch (IOException e) {
System.out.println("Failed deleting temp file: " + e.getMessage());
}
}

}
12 changes: 12 additions & 0 deletions src/test/java/io/goobox/sync/storj/mocks/FilesMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -131,6 +133,16 @@ public Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOExce
throw new IllegalStateException();
}

@Mock
public Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException {
return Paths.get(prefix + System.currentTimeMillis() + suffix);
}

@Mock
public Path write(Path path, byte[] bytes, OpenOption... options) throws IOException {
return path;
}

public void modifyFile(FileMock oldFile, FileMock newFile) {
if (files.contains(oldFile)) {
files.remove(oldFile);
Expand Down

0 comments on commit 2a62965

Please sign in to comment.