From 5083b53e3d66321aabec4a8d7c9d4961019fee8e Mon Sep 17 00:00:00 2001
From: Edgar Fouillet <vlc@edgar.fouillet.eu>
Date: Thu, 21 Nov 2019 14:54:28 +0100
Subject: [PATCH] Device Backup: Exclude the whole Document directory from
 backup

Closes #762
---
 SharedSources/MediaLibraryService.swift | 26 ++++++++++++++-----------
 Sources/VLCMediaFileDiscoverer.m        |  9 ---------
 2 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/SharedSources/MediaLibraryService.swift b/SharedSources/MediaLibraryService.swift
index 62a76b994..66a4cc783 100644
--- a/SharedSources/MediaLibraryService.swift
+++ b/SharedSources/MediaLibraryService.swift
@@ -151,6 +151,21 @@ private extension MediaLibraryService {
             assertionFailure("MediaLibraryService: Medialibrary failed to start.")
             return
         }
+
+        /* exclude Document directory from backup (QA1719) */
+        if let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
+            var excludeURL = URL(fileURLWithPath: documentPath)
+            var resourceValue = URLResourceValues()
+
+            resourceValue.isExcludedFromBackup = true
+
+            do {
+                try excludeURL.setResourceValues(resourceValue)
+            } catch let error {
+                assertionFailure("MediaLibraryService: start: \(error.localizedDescription)")
+            }
+        }
+
         medialib.reload()
         medialib.discover(onEntryPoint: "file://" + path)
     }
@@ -475,17 +490,6 @@ extension MediaLibraryService: VLCMediaFileDiscovererDelegate {
         guard !isLoading else {
             return
         }
-        /* exclude media files from backup (QA1719) */
-        var excludeURL = URL(fileURLWithPath: filePath)
-        var resourceValue = URLResourceValues()
-
-        resourceValue.isExcludedFromBackup = true
-
-        do {
-            try excludeURL.setResourceValues(resourceValue)
-        } catch let error {
-            assertionFailure("MediaLibraryService: VLCMediaFileDiscovererDelegate: \(error.localizedDescription)")
-        }
 
         reload()
     }
diff --git a/Sources/VLCMediaFileDiscoverer.m b/Sources/VLCMediaFileDiscoverer.m
index 5c8fdfeda..0ef77a815 100644
--- a/Sources/VLCMediaFileDiscoverer.m
+++ b/Sources/VLCMediaFileDiscoverer.m
@@ -263,7 +263,6 @@ const float MediaTimerInterval = 2.f;
     NSString *directoryPath = [self directoryPath];
     NSMutableArray *foundFiles = [NSMutableArray arrayWithArray:[[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil]];
     NSMutableArray *filePaths = [NSMutableArray array];
-    NSURL *fileURL;
     while (foundFiles.count) {
         NSString *fileName = foundFiles.firstObject;
         NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
@@ -276,17 +275,9 @@ const float MediaTimerInterval = 2.f;
             if (self.filterResultsForPlayability) {
                 if ([fileName isSupportedMediaFormat] || [fileName isSupportedAudioMediaFormat]) {
                     [filePaths addObject:filePath];
-
-                    /* exclude media files from backup (QA1719) */
-                    fileURL = [NSURL fileURLWithPath:filePath];
-                    [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
                 }
             } else {
                 [filePaths addObject:filePath];
-
-                /* exclude media files from backup (QA1719) */
-                fileURL = [NSURL fileURLWithPath:filePath];
-                [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
             }
         } else if (exists && isDirectory) {
             // add folders
-- 
GitLab