Commit 066e3adc authored by Tobias Conradi's avatar Tobias Conradi Committed by Felix Paul Kühne

add a property for library base path which is publicly accessable add a method...

add a property for library base path which is publicly accessable add a method to migrate the database and thumbnails to a new library base path
Signed-off-by: Felix Paul Kühne's avatarFelix Paul Kühne <fkuehne@videolan.org>
parent 49ec6306
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
@property (readonly) BOOL libraryNeedsUpgrade; @property (readonly) BOOL libraryNeedsUpgrade;
@property (nonatomic, strong) id delegate; @property (nonatomic, strong) id delegate;
// base path for the database and thumbnails
@property (nonatomic, copy) NSString *libraryBasePath;
@property (nonatomic, strong) NSURL *persistentStoreURL; @property (nonatomic, strong) NSURL *persistentStoreURL;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator; @property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
...@@ -51,6 +53,8 @@ ...@@ -51,6 +53,8 @@
- (void)save; - (void)save;
- (void)libraryDidDisappear; - (void)libraryDidDisappear;
- (void)libraryDidAppear; - (void)libraryDidAppear;
- (BOOL)migrateLibraryToBasePath:(NSString *)basePath error:(NSError **)migrationError;
@end @end
@protocol MLMediaLibrary <NSObject> @protocol MLMediaLibrary <NSObject>
......
...@@ -132,35 +132,33 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -132,35 +132,33 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return _managedObjectModel; return _managedObjectModel;
} }
- (NSString *)databaseFolderPath - (NSString *)libraryBasePath
{ {
if (_databaseFolderPath) { if (_libraryBasePath.length == 0) {
if (_databaseFolderPath.length > 0) int directory = NSLibraryDirectory;
return _databaseFolderPath; NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
} NSString *directoryPath = paths.firstObject;
int directory = NSLibraryDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
NSString *directoryPath = paths[0];
#if DELETE_LIBRARY_ON_EACH_LAUNCH #if DELETE_LIBRARY_ON_EACH_LAUNCH
[[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil]; [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil];
#endif #endif
_databaseFolderPath = directoryPath; _libraryBasePath = directoryPath;
}
return _libraryBasePath;
}
- (NSString *)databaseFolderPath
{
if (_databaseFolderPath.length == 0) {
_databaseFolderPath = self.libraryBasePath;
}
return _databaseFolderPath; return _databaseFolderPath;
} }
- (NSString *)thumbnailFolderPath - (NSString *)thumbnailFolderPath
{ {
if (_thumbnailFolderPath) { if (_thumbnailFolderPath.length == 0) {
if (_thumbnailFolderPath.length > 0) _thumbnailFolderPath = [self.libraryBasePath stringByAppendingPathComponent:@"Thumbnails"];
return _thumbnailFolderPath;
} }
int directory = NSLibraryDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
NSString *directoryPath = paths[0];
#if DELETE_LIBRARY_ON_EACH_LAUNCH
[[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil];
#endif
_thumbnailFolderPath = [directoryPath stringByAppendingPathComponent:@"Thumbnails"];
return _thumbnailFolderPath; return _thumbnailFolderPath;
} }
...@@ -172,18 +170,22 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -172,18 +170,22 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
} }
int directory = NSDocumentDirectory; int directory = NSDocumentDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES); NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
_documentFolderPath = [NSString stringWithFormat:@"file://%@", paths[0]]; _documentFolderPath = [NSString stringWithFormat:@"file://%@", paths[0]];
return _documentFolderPath; return _documentFolderPath;
} }
- (NSURL *)persistentStoreURL { - (NSURL *)persistentStoreURL
NSString *databaseFolderPath = [self databaseFolderPath]; {
NSString *path = [databaseFolderPath stringByAppendingPathComponent: @"MediaLibrary.sqlite"]; if (_persistentStoreURL == nil) {
return [NSURL fileURLWithPath:path]; NSString *databaseFolderPath = [self databaseFolderPath];
NSString *path = [databaseFolderPath stringByAppendingPathComponent: @"MediaLibrary.sqlite"];
_persistentStoreURL = [NSURL fileURLWithPath:path];
}
return _persistentStoreURL;
} }
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
...@@ -987,4 +989,48 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles"; ...@@ -987,4 +989,48 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
// Resume our work // Resume our work
[[MLThumbnailerQueue sharedThumbnailerQueue] resume]; [[MLThumbnailerQueue sharedThumbnailerQueue] resume];
} }
#pragma mark - path migrations
- (BOOL)migrateLibraryToBasePath:(NSString *)basePath error:(NSError *__autoreleasing *)migrationError
{
BOOL success = YES;
NSPersistentStoreCoordinator *coordinater = [self persistentStoreCoordinator];
NSURL *oldStoreURL = self.persistentStoreURL;
NSPersistentStore *oldStore = [coordinater persistentStoreForURL:oldStoreURL];
NSString *oldThumbnailPath = self.thumbnailFolderPath;
self.libraryBasePath = basePath;
_databaseFolderPath = nil;
_thumbnailFolderPath = nil;
self.persistentStoreURL = nil;
NSURL *newURL = self.persistentStoreURL;
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:[[newURL URLByDeletingLastPathComponent] path] withIntermediateDirectories:YES attributes:nil error:nil];
NSPersistentStore *newStore = [coordinater migratePersistentStore:oldStore
toURL:newURL
options:oldStore.options
withType:oldStore.type
error:&error];
if (!newStore) {
success = NO;
APLog(@"Failed to migrate library to new path with error: %@",error);
} else {
if (![[NSFileManager defaultManager] removeItemAtURL:oldStoreURL error:&error]) {
APLog(@"Failed to remove old library with error: %@",error);
}
}
NSString *newThumbnailPath = self.thumbnailFolderPath;
if (![[NSFileManager defaultManager] moveItemAtPath:oldThumbnailPath toPath:newThumbnailPath error:&error]) {
success = NO;
APLog(@"Failed to move thumbnails to new path with error: %@",error);
}
if (migrationError != nil && error) {
*migrationError = error;
}
return success;
}
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment