diff --git a/src/androidTest/java/com/owncloud/android/utils/SyncedFolderUtilsTest.kt b/src/androidTest/java/com/owncloud/android/utils/SyncedFolderUtilsTest.kt index 7038b82f6f..cde8de75f8 100644 --- a/src/androidTest/java/com/owncloud/android/utils/SyncedFolderUtilsTest.kt +++ b/src/androidTest/java/com/owncloud/android/utils/SyncedFolderUtilsTest.kt @@ -24,10 +24,12 @@ import com.owncloud.android.AbstractIT import com.owncloud.android.datamodel.MediaFolder import com.owncloud.android.datamodel.MediaFolderType import com.owncloud.android.datamodel.SyncedFolder +import org.apache.commons.io.FileUtils import org.junit.AfterClass import org.junit.Assert import org.junit.BeforeClass import org.junit.Test +import java.io.File import java.util.Arrays class SyncedFolderUtilsTest : AbstractIT() { @@ -41,6 +43,7 @@ class SyncedFolderUtilsTest : AbstractIT() { Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(FOLDER)) Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("Folder.jpeg")) Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection("FOLDER.jpg")) + Assert.assertFalse(SyncedFolderUtils.isFileNameQualifiedForMediaDetection(".thumbdata4--1967290299")) } @Test @@ -150,6 +153,50 @@ class SyncedFolderUtilsTest : AbstractIT() { Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder)) } + @Test + fun assertUnqualifiedSyncedFolder() { + val tempPath = File(FileStorageUtils.getTemporalPath(account.name) + File.pathSeparator + ".thumbnail") + if (!tempPath.exists()) { + Assert.assertTrue(tempPath.mkdirs()) + } + getDummyFile(".thumbnail/image.jpg") + val folder = SyncedFolder( + tempPath.absolutePath, + "", + true, + false, + false, + true, + account.name, + 1, + 1, + true, + 0L, + MediaFolderType.IMAGE, + false) + Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder)) + } + + @Test + fun assertUnqualifiedContentSyncedFolder() { + val image = getDummyFile(THUMBDATA_FOLDER + File.pathSeparator + IMAGE_JPEG) + val folder = SyncedFolder( + FileStorageUtils.getTemporalPath(account.name) + File.pathSeparator + THUMBDATA_FOLDER, + "", + true, + false, + false, + true, + account.name, + 1, + 1, + true, + 0L, + MediaFolderType.IMAGE, + false) + Assert.assertFalse(SyncedFolderUtils.isQualifyingMediaFolder(folder)) + } + companion object { private const val SELFIE = "selfie.png" private const val SCREENSHOT = "screenshot.JPG" @@ -160,10 +207,17 @@ class SyncedFolderUtilsTest : AbstractIT() { private const val SONG_TWO = "song2.mp3" private const val FOLDER = "folder.JPG" private const val COVER = "cover.jpg" + private const val THUMBDATA_FOLDER = "thumbdata_test"; + private const val THUMBDATA_FILE = "thumbdata_test"; private const val ITERATION = 100 @BeforeClass fun setUp() { + val tempPath = File(FileStorageUtils.getTemporalPath(account.name) + File.pathSeparator + THUMBDATA_FOLDER) + if (!tempPath.exists()) { + tempPath.mkdirs() + } + createFile(SELFIE, ITERATION) createFile(SCREENSHOT, ITERATION) createFile(IMAGE_JPEG, ITERATION) @@ -173,19 +227,13 @@ class SyncedFolderUtilsTest : AbstractIT() { createFile(SONG_TWO, ITERATION) createFile(FOLDER, ITERATION) createFile(COVER, ITERATION) + + createFile(THUMBDATA_FOLDER + File.pathSeparator + THUMBDATA_FILE, ITERATION) } @AfterClass fun tearDown() { - getDummyFile(SELFIE).delete() - getDummyFile(SCREENSHOT).delete() - getDummyFile(IMAGE_JPEG).delete() - getDummyFile(IMAGE_BITMAP).delete() - getDummyFile(SONG_ZERO).delete() - getDummyFile(SONG_ONE).delete() - getDummyFile(SONG_TWO).delete() - getDummyFile(FOLDER).delete() - getDummyFile(COVER).delete() + FileUtils.deleteDirectory(File(FileStorageUtils.getTemporalPath(account.name))) } } } diff --git a/src/main/java/com/owncloud/android/utils/SyncedFolderUtils.java b/src/main/java/com/owncloud/android/utils/SyncedFolderUtils.java index d62c9d2cdd..3a537d0c48 100644 --- a/src/main/java/com/owncloud/android/utils/SyncedFolderUtils.java +++ b/src/main/java/com/owncloud/android/utils/SyncedFolderUtils.java @@ -25,6 +25,7 @@ import com.owncloud.android.datamodel.SyncedFolder; import java.io.File; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -40,8 +41,12 @@ public final class SyncedFolderUtils { "cover.jpg", "cover.jpeg", "folder.jpg", "folder.jpeg" }; - private static final Set DISQUALIFIED_MEDIA_DETECTION_SET = + private static final Set DISQUALIFIED_MEDIA_DETECTION_FILE_SET = new HashSet<>(Arrays.asList(DISQUALIFIED_MEDIA_DETECTION_SOURCE)); + private static final Set AUTO_QUALIFYING_FOLDER_TYPE_SET = + new HashSet<>(Collections.singletonList(MediaFolderType.CUSTOM)); + private static final String THUMBNAIL_FOLDER_PREFIX = ".thumbnail"; + private static final String THUMBNAIL_DATA_FILE_PREFIX = ".thumbdata"; private static final int SINGLE_FILE = 1; private SyncedFolderUtils() { @@ -60,10 +65,15 @@ public final class SyncedFolderUtils { } // custom folders are always fine - if (MediaFolderType.CUSTOM == mediaFolder.type) { + if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(mediaFolder.type)) { return true; } + // thumbnail folder + if (isQualifiedFolder(mediaFolder.absolutePath)) { + return false; + } + // filter media folders // no files @@ -89,10 +99,15 @@ public final class SyncedFolderUtils { } // custom folders are always fine - if (MediaFolderType.CUSTOM == syncedFolder.getType()) { + if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(syncedFolder.getType())) { return true; } + // thumbnail folder + if (isQualifiedFolder(syncedFolder.getLocalPath())) { + return false; + } + // filter media folders File[] files = getFileList(new File(syncedFolder.getLocalPath())); @@ -121,6 +136,16 @@ public final class SyncedFolderUtils { return true; } + // custom folders are always fine + if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(folderType)) { + return true; + } + + // thumbnail folder + if (isQualifiedFolder(folderPath)) { + return false; + } + // filter media folders File[] files = getFileList(new File(folderPath)); @@ -135,6 +160,18 @@ public final class SyncedFolderUtils { return true; } + /** + * check if folder is qualified for auto upload. + * + * @param folderPath the folder's path string + * @return code>true if folder qualifies for auto upload else false + */ + private static boolean isQualifiedFolder(String folderPath) { + File folder = new File(folderPath); + // check if folder starts with thumbnail praefix + return !folder.isDirectory() || folder.getName() == null || !folder.getName().startsWith(THUMBNAIL_FOLDER_PREFIX); + } + /** * check if given list contains images that qualify as auto upload relevant files. * @@ -176,7 +213,8 @@ public final class SyncedFolderUtils { */ public static boolean isFileNameQualifiedForMediaDetection(String fileName) { if (fileName != null) { - return !DISQUALIFIED_MEDIA_DETECTION_SET.contains(fileName.toLowerCase(Locale.ROOT)); + return !DISQUALIFIED_MEDIA_DETECTION_FILE_SET.contains(fileName.toLowerCase(Locale.ROOT)) + && !fileName.startsWith(THUMBNAIL_DATA_FILE_PREFIX); } else { return false; }