diff --git a/MP42/MP42File.m b/MP42/MP42File.m index 7d87623..442d047 100644 --- a/MP42/MP42File.m +++ b/MP42/MP42File.m @@ -14,6 +14,7 @@ #import "MP42Track+Private.h" #import "MP42PreviewGenerator.h" #import "MP42Metadata+Private.h" +#import "MP42RelatedItem.h" #import "mp4v2.h" @@ -731,56 +732,47 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { #pragma mark - Saving -- (NSURL *)tempURL { - NSURL *tempURL = nil; - #ifdef SB_SANDBOX - NSURL *folderURL = [self.URL URLByDeletingLastPathComponent]; - tempURL = [NSFileManager.defaultManager URLForDirectory:NSItemReplacementDirectory inDomain:NSUserDomainMask appropriateForURL:folderURL create:YES error:nil]; - #else - tempURL = [self.URL URLByDeletingLastPathComponent]; - #endif - - if (tempURL) { - tempURL = [tempURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.tmp", self.URL.lastPathComponent]]; - } - - return tempURL; -} - - (BOOL)optimize { __block BOOL noErr = NO; __block _Atomic int32_t done = 0; @autoreleasepool { - NSError *error = nil; - NSURL *tempURL = [self tempURL]; NSFileManager *fileManager = [[NSFileManager alloc] init]; dispatch_semaphore_t sem = dispatch_semaphore_create(0); - if (tempURL) { + MP42RelatedItem *item = [[MP42RelatedItem alloc] initWithURL:self.URL extension:@"sublertemp"]; + +#ifdef SB_SANDBOX + [NSFileCoordinator addFilePresenter:item]; + NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:item]; +#endif + + if (item && coordinator) { unsigned long long originalFileSize = [[[fileManager attributesOfItemAtPath:self.URL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ - noErr = MP4Optimize(self.URL.fileSystemRepresentation, tempURL.fileSystemRepresentation); + noErr = MP4Optimize(self.URL.fileSystemRepresentation, item.presentedItemURL.fileSystemRepresentation); done = 1; dispatch_semaphore_signal(sem); }); // Loop to check the progress while (!done) { - unsigned long long fileSize = [[[fileManager attributesOfItemAtPath:tempURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; + unsigned long long fileSize = [[[fileManager attributesOfItemAtPath:item.presentedItemURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; [self progressStatus:((double)fileSize / originalFileSize) * 100]; usleep(450000); } dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); + NSError *error; + // Additional check to see if we can open the optimized file - if (noErr && [[MP42File alloc] initWithURL:tempURL error:NULL]) { + if (noErr && [[MP42File alloc] initWithURL:item.presentedItemURL error:NULL]) { // Replace the original file NSURL *result = nil; noErr = [fileManager replaceItemAtURL:self.URL - withItemAtURL:tempURL + withItemAtURL:item.presentedItemURL backupItemName:nil options:NSFileManagerItemReplacementWithoutDeletingBackupItem resultingItemURL:&result error:&error]; @@ -795,8 +787,12 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { if (!noErr) { // Remove the temp file if the optimization didn't complete - [fileManager removeItemAtURL:tempURL error:NULL]; + [fileManager removeItemAtURL:item.presentedItemURL error:NULL]; } + +#ifdef SB_SANDBOX + [NSFileCoordinator removeFilePresenter:item]; +#endif } } @@ -1296,8 +1292,8 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { [coder encodeObject:bookmarkData forKey:@"bookmark"]; #else - if ([self.URL isFileReferenceURL]) { - [coder encodeObject:[self.URL filePathURL] forKey:@"fileUrl"]; + if (self.URL.isFileReferenceURL) { + [coder encodeObject:self.URL.filePathURL forKey:@"fileUrl"]; } else { [coder encodeObject:self.URL forKey:@"fileUrl"]; } diff --git a/MP42/MP42RelatedItem.h b/MP42/MP42RelatedItem.h new file mode 100644 index 0000000..8b2b5fb --- /dev/null +++ b/MP42/MP42RelatedItem.h @@ -0,0 +1,22 @@ +// +// MP42RelatedItem.h +// MP42Foundation +// +// Created by Damiano Galassi on 09/03/2019. +// Copyright © 2019 Damiano Galassi. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MP42RelatedItem : NSObject + +@property (nonatomic, readonly) NSURL *URL; +@property (nonatomic, readonly) NSString *extension; + +- (instancetype)initWithURL:(NSURL *)URL extension:(NSString *)extension; + +@end + +NS_ASSUME_NONNULL_END diff --git a/MP42/MP42RelatedItem.m b/MP42/MP42RelatedItem.m new file mode 100644 index 0000000..a6f431b --- /dev/null +++ b/MP42/MP42RelatedItem.m @@ -0,0 +1,48 @@ +// +// MP42RelatedItem.m +// MP42Foundation +// +// Created by Damiano Galassi on 09/03/2019. +// Copyright © 2019 Damiano Galassi. All rights reserved. +// + +#import "MP42RelatedItem.h" + +@interface MP42RelatedItem () + +@property (nonatomic, readonly) NSOperationQueue *queue; +@property (nonatomic, readonly) dispatch_queue_t dispatch_queue; + +@end + +@implementation MP42RelatedItem + +- (instancetype)initWithURL:(NSURL *)URL extension:(NSString *)extension +{ + self = [super init]; + if (self) { + _URL = URL; + _extension = extension; + _queue = [[NSOperationQueue alloc] init]; + _dispatch_queue = dispatch_queue_create("org.mp42foundation.relatedItem", DISPATCH_QUEUE_SERIAL); + _queue.underlyingQueue = _dispatch_queue; + } + return self; +} + +- (NSURL *)presentedItemURL +{ + return [self.URL.URLByDeletingPathExtension URLByAppendingPathExtension:self.extension]; +} + +- (NSURL *)primaryPresentedItemURL +{ + return self.URL; +} + +- (NSOperationQueue *)presentedItemOperationQueue +{ + return self.queue; +} + +@end diff --git a/MP42Foundation.xcodeproj/project.pbxproj b/MP42Foundation.xcodeproj/project.pbxproj index 04a5668..a12671c 100644 --- a/MP42Foundation.xcodeproj/project.pbxproj +++ b/MP42Foundation.xcodeproj/project.pbxproj @@ -82,6 +82,10 @@ A91711E9187D547D00459495 /* MP42PreviewGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = A91711E5187D547D00459495 /* MP42PreviewGenerator.m */; }; A917286619FD2BCE00348DF8 /* MP42Logging.h in Headers */ = {isa = PBXBuildFile; fileRef = A917286519FD2BCE00348DF8 /* MP42Logging.h */; settings = {ATTRIBUTES = (Public, ); }; }; A917286719FD2BCE00348DF8 /* MP42Logging.h in Headers */ = {isa = PBXBuildFile; fileRef = A917286519FD2BCE00348DF8 /* MP42Logging.h */; }; + A9195F362233DD38005C4D01 /* MP42RelatedItem.h in Headers */ = {isa = PBXBuildFile; fileRef = A9195F342233DD38005C4D01 /* MP42RelatedItem.h */; }; + A9195F372233DD38005C4D01 /* MP42RelatedItem.h in Headers */ = {isa = PBXBuildFile; fileRef = A9195F342233DD38005C4D01 /* MP42RelatedItem.h */; }; + A9195F382233DD38005C4D01 /* MP42RelatedItem.m in Sources */ = {isa = PBXBuildFile; fileRef = A9195F352233DD38005C4D01 /* MP42RelatedItem.m */; }; + A9195F392233DD38005C4D01 /* MP42RelatedItem.m in Sources */ = {isa = PBXBuildFile; fileRef = A9195F352233DD38005C4D01 /* MP42RelatedItem.m */; }; A91C3ACE1BF20CF1008BCF87 /* MP42FormatUtilites.mm in Sources */ = {isa = PBXBuildFile; fileRef = A91C3ACC1BF20CF1008BCF87 /* MP42FormatUtilites.mm */; }; A91C3ACF1BF20CF1008BCF87 /* MP42FormatUtilites.mm in Sources */ = {isa = PBXBuildFile; fileRef = A91C3ACC1BF20CF1008BCF87 /* MP42FormatUtilites.mm */; }; A91C3AD01BF20CF1008BCF87 /* MP42FormatUtilites.h in Headers */ = {isa = PBXBuildFile; fileRef = A91C3ACD1BF20CF1008BCF87 /* MP42FormatUtilites.h */; }; @@ -286,6 +290,8 @@ A91711E4187D547D00459495 /* MP42PreviewGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42PreviewGenerator.h; sourceTree = ""; }; A91711E5187D547D00459495 /* MP42PreviewGenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42PreviewGenerator.m; sourceTree = ""; }; A917286519FD2BCE00348DF8 /* MP42Logging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42Logging.h; sourceTree = ""; }; + A9195F342233DD38005C4D01 /* MP42RelatedItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MP42RelatedItem.h; sourceTree = ""; }; + A9195F352233DD38005C4D01 /* MP42RelatedItem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MP42RelatedItem.m; sourceTree = ""; }; A91C3ACC1BF20CF1008BCF87 /* MP42FormatUtilites.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MP42FormatUtilites.mm; sourceTree = ""; }; A91C3ACD1BF20CF1008BCF87 /* MP42FormatUtilites.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42FormatUtilites.h; sourceTree = ""; }; A925AAAD18379AF800BE84D4 /* MP42Utilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42Utilities.h; sourceTree = ""; }; @@ -697,6 +703,8 @@ A9B9C4A418239B7E00416A4E /* Utility */ = { isa = PBXGroup; children = ( + A9195F342233DD38005C4D01 /* MP42RelatedItem.h */, + A9195F352233DD38005C4D01 /* MP42RelatedItem.m */, A91711E4187D547D00459495 /* MP42PreviewGenerator.h */, A91711E5187D547D00459495 /* MP42PreviewGenerator.m */, A989F10D1823B3B40070AD8C /* MP42TextSample.h */, @@ -772,6 +780,7 @@ A90801161D4B83A3002B6950 /* MP42AudioEncoder.h in Headers */, A9ED5B161F824BC100E0E4FA /* MP42SSAImporter.h in Headers */, A9422FC81D4917AF000DB435 /* audio_resample.h in Headers */, + A9195F372233DD38005C4D01 /* MP42RelatedItem.h in Headers */, A917286719FD2BCE00348DF8 /* MP42Logging.h in Headers */, A941C95F1F82A9B900FC5E8D /* MP42SSAConverter.h in Headers */, A9ED5B0E1F824A9B00E0E4FA /* MP42SSAParser.h in Headers */, @@ -823,6 +832,7 @@ A9B9C2A31823923200416A4E /* MP42XMLReader.h in Headers */, A90801151D4B83A3002B6950 /* MP42AudioEncoder.h in Headers */, A9B9C28D1823923200416A4E /* MP42Muxer.h in Headers */, + A9195F362233DD38005C4D01 /* MP42RelatedItem.h in Headers */, A9B9C2AD1823923200416A4E /* mpeg4ip_bitstream.h in Headers */, A9B9C2A81823923200416A4E /* MatroskaFile.h in Headers */, A9B9C2991823923200416A4E /* MP42SubUtilities.h in Headers */, @@ -1030,6 +1040,7 @@ A910B61318394EB20064028F /* MP42AACImporter.mm in Sources */, A910B61518394EB20064028F /* MP42AC3Importer.m in Sources */, A90801141D4B83A3002B6950 /* MP42AudioDecoder.m in Sources */, + A9195F392233DD38005C4D01 /* MP42RelatedItem.m in Sources */, A9422FC61D4917AF000DB435 /* audio_resample.c in Sources */, A910B61818394EB20064028F /* MatroskaFile.c in Sources */, A9ED5B101F824A9B00E0E4FA /* MP42SSAParser.m in Sources */, @@ -1058,6 +1069,7 @@ A9B9C27E1823923200416A4E /* MP42FileImporter.m in Sources */, A9B9C27A1823923200416A4E /* MP42Fifo.m in Sources */, A941C9581F82996600FC5E8D /* MP42TextSubConverter.m in Sources */, + A9195F382233DD38005C4D01 /* MP42RelatedItem.m in Sources */, A9DFBDA3195FF5BA00410060 /* MP42Heap.m in Sources */, A9059DCC1D870B9C000D1B9A /* MP42ConversionSettings.m in Sources */, A9B9C26B1823923200416A4E /* MP42AudioConverter.m in Sources */, @@ -1253,6 +1265,7 @@ GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", + "SB_SANDBOX=1", "$(inherited)", ); GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; @@ -1317,6 +1330,7 @@ ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = c11; GCC_NO_COMMON_BLOCKS = YES; + GCC_PREPROCESSOR_DEFINITIONS = "SB_SANDBOX=1"; GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = NO; GCC_WARN_ABOUT_MISSING_NEWLINE = YES;