SyncedFolderUtils.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2020 Andy Scherzinger
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.utils;
  21. import com.owncloud.android.datamodel.MediaFolder;
  22. import com.owncloud.android.datamodel.MediaFolderType;
  23. import com.owncloud.android.datamodel.SyncedFolder;
  24. import java.io.File;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.HashSet;
  28. import java.util.List;
  29. import java.util.Locale;
  30. import java.util.Set;
  31. import androidx.annotation.Nullable;
  32. /**
  33. * Utility class with methods for processing synced folders.
  34. */
  35. public final class SyncedFolderUtils {
  36. private static final String[] DISQUALIFIED_MEDIA_DETECTION_SOURCE = new String[]{
  37. "cover.jpg", "cover.jpeg",
  38. "folder.jpg", "folder.jpeg"
  39. };
  40. private static final Set<String> DISQUALIFIED_MEDIA_DETECTION_FILE_SET =
  41. new HashSet<>(Arrays.asList(DISQUALIFIED_MEDIA_DETECTION_SOURCE));
  42. private static final Set<MediaFolderType> AUTO_QUALIFYING_FOLDER_TYPE_SET =
  43. new HashSet<>(Collections.singletonList(MediaFolderType.CUSTOM));
  44. private static final String THUMBNAIL_FOLDER_PREFIX = ".thumbnail";
  45. private static final String THUMBNAIL_DATA_FILE_PREFIX = ".thumbdata";
  46. private static final int SINGLE_FILE = 1;
  47. private SyncedFolderUtils() {
  48. // utility class -> private constructor
  49. }
  50. /**
  51. * analyzes a given media folder if its content qualifies for the folder to be handled as a media folder.
  52. *
  53. * @param mediaFolder media folder to analyse
  54. * @return <code>true</code> if it qualifies as a media folder else <code>false</code>
  55. */
  56. public static boolean isQualifyingMediaFolder(@Nullable MediaFolder mediaFolder) {
  57. if (mediaFolder == null) {
  58. return false;
  59. }
  60. // custom folders are always fine
  61. if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(mediaFolder.type)) {
  62. return true;
  63. }
  64. // thumbnail folder
  65. if (!isQualifiedFolder(mediaFolder.absolutePath)) {
  66. return false;
  67. }
  68. // filter media folders
  69. // no files
  70. if (mediaFolder.numberOfFiles < SINGLE_FILE) {
  71. return false;
  72. } // music album (just one cover-art image)
  73. else if (MediaFolderType.IMAGE == mediaFolder.type) {
  74. return containsQualifiedImages(mediaFolder.filePaths);
  75. }
  76. return true;
  77. }
  78. /**
  79. * analyzes a given synced folder if its content qualifies for the folder to be handled as a media folder.
  80. *
  81. * @param syncedFolder synced folder to analyse
  82. * @return <code>true</code> if it qualifies as a media folder else <code>false</code>
  83. */
  84. public static boolean isQualifyingMediaFolder(@Nullable SyncedFolder syncedFolder) {
  85. if (syncedFolder == null) {
  86. return false;
  87. }
  88. // custom folders are always fine
  89. if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(syncedFolder.getType())) {
  90. return true;
  91. }
  92. // thumbnail folder
  93. if (!isQualifiedFolder(syncedFolder.getLocalPath())) {
  94. return false;
  95. }
  96. // filter media folders
  97. File[] files = getFileList(new File(syncedFolder.getLocalPath()));
  98. // no files
  99. if (files.length < SINGLE_FILE) {
  100. return false;
  101. } // music album (just one cover-art image)
  102. else if (MediaFolderType.IMAGE == syncedFolder.getType()) {
  103. return containsQualifiedImages(files);
  104. }
  105. return true;
  106. }
  107. /**
  108. * analyzes a given folder based on a path-string and type if its content qualifies for the folder to be handled as
  109. * a media folder.
  110. *
  111. * @param folderPath String representation for a folder
  112. * @param folderType type of the folder
  113. * @return <code>true</code> if it qualifies as a media folder else <code>false</code>
  114. */
  115. public static boolean isQualifyingMediaFolder(String folderPath, MediaFolderType folderType) {
  116. // custom folders are always fine
  117. if (MediaFolderType.CUSTOM == folderType) {
  118. return true;
  119. }
  120. // custom folders are always fine
  121. if (AUTO_QUALIFYING_FOLDER_TYPE_SET.contains(folderType)) {
  122. return true;
  123. }
  124. // thumbnail folder
  125. if (!isQualifiedFolder(folderPath)) {
  126. return false;
  127. }
  128. // filter media folders
  129. File[] files = getFileList(new File(folderPath));
  130. // no files
  131. if (files.length < SINGLE_FILE) {
  132. return false;
  133. } // music album (just one cover-art image)
  134. else if (MediaFolderType.IMAGE == folderType) {
  135. return containsQualifiedImages(files);
  136. }
  137. return true;
  138. }
  139. /**
  140. * check if folder is qualified for auto upload.
  141. *
  142. * @param folderPath the folder's path string
  143. * @return code>true</code> if folder qualifies for auto upload else <code>false</code>
  144. */
  145. public static boolean isQualifiedFolder(String folderPath) {
  146. File folder = new File(folderPath);
  147. // check if folder starts with thumbnail praefix
  148. return folder.isDirectory() && !folder.getName().startsWith(THUMBNAIL_FOLDER_PREFIX);
  149. }
  150. /**
  151. * check if given list contains images that qualify as auto upload relevant files.
  152. *
  153. * @param filePaths list of file paths
  154. * @return <code>true</code> if at least one files qualifies as auto upload relevant else <code>false</code>
  155. */
  156. private static boolean containsQualifiedImages(List<String> filePaths) {
  157. for (String filePath : filePaths) {
  158. if (isFileNameQualifiedForAutoUpload(FileUtil.getFilenameFromPathString(filePath))
  159. && MimeTypeUtil.isImage(MimeTypeUtil.getMimeTypeFromPath(filePath))) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * check if given list of files contains images that qualify as auto upload relevant files.
  167. *
  168. * @param files list of files
  169. * @return <code>true</code> if at least one files qualifies as auto upload relevant else <code>false</code>
  170. */
  171. private static boolean containsQualifiedImages(File... files) {
  172. for (File file : files) {
  173. if (isFileNameQualifiedForAutoUpload(file.getName()) && MimeTypeUtil.isImage(file)) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /**
  180. * check if given file is auto upload relevant files.
  181. *
  182. * @param fileName file name to be checked
  183. * @return <code>true</code> if the file qualifies as auto upload relevant else <code>false</code>
  184. */
  185. public static boolean isFileNameQualifiedForAutoUpload(String fileName) {
  186. if (fileName != null) {
  187. return !DISQUALIFIED_MEDIA_DETECTION_FILE_SET.contains(fileName.toLowerCase(Locale.ROOT))
  188. && !fileName.startsWith(THUMBNAIL_DATA_FILE_PREFIX);
  189. } else {
  190. return false;
  191. }
  192. }
  193. /**
  194. * return list of files for given folder.
  195. *
  196. * @param localFolder folder to scan
  197. * @return sorted list of folder of given folder
  198. */
  199. public static File[] getFileList(File localFolder) {
  200. File[] files = localFolder.listFiles(pathname -> !pathname.isDirectory());
  201. if (files != null) {
  202. Arrays.sort(files, (f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
  203. } else {
  204. files = new File[]{};
  205. }
  206. return files;
  207. }
  208. }