From 2a6296579ee5cfe9f51c8bacf1c993e95eeaaa1b Mon Sep 17 00:00:00 2001 From: Kaloyan Raev Date: Thu, 7 Dec 2017 10:57:56 +0200 Subject: [PATCH] #6: Delete temp dir files (#54) --- .../goobox/sync/storj/CreateCloudDirTask.java | 22 +++++++++++++++++-- .../io/goobox/sync/storj/mocks/FilesMock.java | 12 ++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java b/src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java index 15122d1..83043e1 100644 --- a/src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java +++ b/src/main/java/io/goobox/sync/storj/CreateCloudDirTask.java @@ -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 + "... "); @@ -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 }; @@ -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(); @@ -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()); + } + } + } diff --git a/src/test/java/io/goobox/sync/storj/mocks/FilesMock.java b/src/test/java/io/goobox/sync/storj/mocks/FilesMock.java index 4e99af6..9c32a33 100644 --- a/src/test/java/io/goobox/sync/storj/mocks/FilesMock.java +++ b/src/test/java/io/goobox/sync/storj/mocks/FilesMock.java @@ -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; @@ -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);