Parcourir la source

migrate thumbnail cache from external to internal cache location

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky il y a 6 ans
Parent
commit
3527b2b7f7

+ 21 - 11
src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -115,19 +115,29 @@ public final class ThumbnailsCacheManager {
 
                 if (mThumbnailCache == null) {
                     try {
-                        // Check if media is mounted or storage is built-in, if so,
-                        // try and use external cache dir; otherwise use internal cache dir
-                        File cacheDir = MainApp.getAppContext().getExternalCacheDir();
-
-                        if (cacheDir != null) {
-                            String cachePath = cacheDir.getPath() + File.separator + CACHE_FOLDER;
-                            Log_OC.d(TAG, "create dir: " + cachePath);
-                            File diskCacheDir = new File(cachePath);
-                            mThumbnailCache = new DiskLruImageCache(diskCacheDir, DISK_CACHE_SIZE, mCompressFormat,
-                                    mCompressQuality);
-                        } else {
+                        File cacheDir = MainApp.getAppContext().getCacheDir();
+
+                        if (cacheDir == null) {
                             throw new FileNotFoundException("Thumbnail cache could not be opened");
                         }
+
+                        String cachePath = cacheDir.getPath() + File.separator + CACHE_FOLDER;
+                        Log_OC.d(TAG, "thumbnail cache dir: " + cachePath);
+                        File diskCacheDir = new File(cachePath);
+
+                        // migrate from external cache to internal cache
+                        File oldCacheDir = MainApp.getAppContext().getExternalCacheDir();
+
+                        if (oldCacheDir != null && oldCacheDir.exists()) {
+                            String cacheOldPath = oldCacheDir.getPath() + File.separator + CACHE_FOLDER;
+                            File diskOldCacheDir = new File(cacheOldPath);
+
+                            FileStorageUtils.copyDirs(diskOldCacheDir, diskCacheDir);
+                            FileStorageUtils.deleteRecursive(diskOldCacheDir);
+                        }
+
+                        mThumbnailCache = new DiskLruImageCache(diskCacheDir, DISK_CACHE_SIZE, mCompressFormat,
+                                                                mCompressQuality);
                     } catch (Exception e) {
                         Log_OC.d(TAG, e.getMessage());
                         mThumbnailCache = null;

+ 28 - 0
src/main/java/com/owncloud/android/utils/FileStorageUtils.java

@@ -345,6 +345,24 @@ public final class FileStorageUtils {
         }
     }
 
+    public static boolean copyDirs(File sourceFolder, File targetFolder) {
+        if (!targetFolder.mkdirs()) {
+            return false;
+        }
+
+        for (File f : sourceFolder.listFiles()) {
+            if (f.isDirectory()) {
+                if (!copyDirs(f, new File(targetFolder, f.getName()))) {
+                    return false;
+                }
+            } else if (!FileStorageUtils.copyFile(f, new File(targetFolder, f.getName()))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
     public static void deleteRecursively(File file, FileDataStorageManager storageManager) {
         if (file.isDirectory()) {
             for (File child : file.listFiles()) {
@@ -356,6 +374,16 @@ public final class FileStorageUtils {
         file.delete();
     }
 
+    public static boolean deleteRecursive(File file) {
+        boolean res = true;
+        if (file.isDirectory()) {
+            for (File c : file.listFiles()) {
+                res = deleteRecursive(c) && res;
+            }
+        }
+        return file.delete() && res;
+    }
+
     public static void checkIfFileFinishedSaving(OCFile file) {
         long lastModified = 0;
         long lastSize = 0;