diff --git a/Playlist/PlaylistLoader.m b/Playlist/PlaylistLoader.m index 2165eed99..569127156 100644 --- a/Playlist/PlaylistLoader.m +++ b/Playlist/PlaylistLoader.m @@ -39,6 +39,8 @@ #import "RedundantPlaylistDataStore.h" +#import "SandboxBroker.h" + @import Firebase; extern NSMutableDictionary *kArtworkDictionary; @@ -394,6 +396,7 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc if([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDir]) { if(isDir == YES) { // Get subpaths + [[SandboxBroker sharedSandboxBroker] addFolderIfMissing:url]; [expandedURLs addObjectsFromArray:[self fileURLsAtPath:[url path]]]; } else { [expandedURLs addObject:[NSURL fileURLWithPath:[url path]]]; diff --git a/Utils/SandboxBroker.h b/Utils/SandboxBroker.h index 5272c466c..a5b658c9a 100644 --- a/Utils/SandboxBroker.h +++ b/Utils/SandboxBroker.h @@ -21,6 +21,8 @@ NS_ASSUME_NONNULL_BEGIN - (id)init; - (void)shutdown; +- (void)addFolderIfMissing:(NSURL *)folderUrl; + - (const void *)beginFolderAccess:(NSURL *)fileUrl; - (void)endFolderAccess:(const void *)handle; diff --git a/Utils/SandboxBroker.m b/Utils/SandboxBroker.m index 005db67f8..427d572ac 100644 --- a/Utils/SandboxBroker.m +++ b/Utils/SandboxBroker.m @@ -186,6 +186,47 @@ static SandboxBroker *kSharedSandboxBroker = nil; return nil; } +- (void)addFolderIfMissing:(NSURL *)folderUrl { + if(![folderUrl isFileURL]) return; + + @synchronized (self) { + SandboxEntry *_entry = nil; + + for(SandboxEntry *entry in storage) { + if(entry.path && [SandboxBroker isPath:folderUrl aSubdirectoryOf:[NSURL fileURLWithPath:entry.path]]) { + _entry = entry; + break; + } + } + + if(!_entry) { + _entry = [self recursivePathTest:folderUrl]; + } + + if(!_entry) { + NSError *err = nil; + NSData *bookmark = [folderUrl bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&err]; + if(!bookmark && err) { + ALog(@"Failed to add bookmark for URL: %@, with error: %@", folderUrl, [err localizedDescription]); + return; + } + + NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer]; + + SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext]; + + if(token) { + token.path = [folderUrl path]; + token.bookmark = bookmark; + [pc.viewContext save:&err]; + if(err) { + ALog(@"Error saving bookmark: %@", [err localizedDescription]); + } + } + } + } +} + - (const void *)beginFolderAccess:(NSURL *)fileUrl { NSURL *folderUrl = [[SandboxBroker urlWithoutFragment:fileUrl] URLByDeletingLastPathComponent]; if(![folderUrl isFileURL]) return NULL;