@@ -184,7 +184,7 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
184184
185185@implementation CDVFile
186186
187- @synthesize rootDocsPath, appDocsPath, appLibraryPath , appTempPath, userHasAllowed, fileSystems=fileSystems_;
187+ @synthesize appDocsPath, appDataPath, appSupportPath , appTempPath, appCachePath , userHasAllowed, fileSystems=fileSystems_;
188188
189189- (void )registerFilesystem : (NSObject <CDVFileSystem> *)fs {
190190 __weak CDVFile* weakSelf = self;
@@ -235,12 +235,12 @@ - (NSArray *)getExtraFileSystemsPreference:(NSWindowController *)vc
235235{
236236 NSString *filesystemsStr = nil ;
237237 if ([self .viewController isKindOfClass: [CDVViewController class ]]) {
238- CDVViewController *vc = (CDVViewController *) self.viewController ;
238+ CDVViewController *vc = self.viewController ;
239239 NSDictionary *settings = [vc settings ];
240- filesystemsStr = [settings[@" osxextrafilesystems " ] lowercaseString ];
240+ filesystemsStr = [settings[CDV_PREF_EXTRA_FILESYSTEM ] lowercaseString ];
241241 }
242242 if (!filesystemsStr) {
243- filesystemsStr = @" library,library-nosync,documents,documents-nosync,cache,bundle,root " ;
243+ filesystemsStr = CDV_FILESYSTEMS_DEFAULT ;
244244 }
245245 return [filesystemsStr componentsSeparatedByString: @" ," ];
246246}
@@ -260,13 +260,6 @@ - (void)registerExtraFileSystems:(NSArray *)filesystems fromAvailableSet:(NSDict
260260{
261261 NSMutableSet *installedFilesystems = [[NSMutableSet alloc ] initWithCapacity: 7 ];
262262
263- /* Build non-syncable directories as necessary */
264- for (NSString *nonSyncFS in @[@" library-nosync" , @" documents-nosync" ]) {
265- if ([filesystems containsObject: nonSyncFS]) {
266- [self makeNonSyncable: availableFileSystems[nonSyncFS]];
267- }
268- }
269-
270263 /* Register filesystems in order */
271264 for (NSString *fsName in filesystems) {
272265 if (![installedFilesystems containsObject: fsName]) {
@@ -283,88 +276,66 @@ - (void)registerExtraFileSystems:(NSArray *)filesystems fromAvailableSet:(NSDict
283276
284277- (NSDictionary *)getAvailableFileSystems
285278{
286- NSString *libPath = [NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES ) objectAtIndex: 0 ];
287- NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex: 0 ];
288279 return @{
289- @" library" : libPath,
290- @" library-nosync" : [libPath stringByAppendingPathComponent: @" NoCloud" ],
291- @" documents" : docPath,
292- @" documents-nosync" : [docPath stringByAppendingPathComponent: @" NoCloud" ],
293- @" cache" : [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES ) objectAtIndex: 0 ],
280+ @" documents" : self.appDocsPath ,
281+ @" cache" : self.appCachePath ,
294282 @" bundle" : [[NSBundle mainBundle ] bundlePath ],
295283 @" root" : @" /"
296284 };
297285}
298286
299287- (void )pluginInitialize
300- {
301- filePlugin = self;
288+ { filePlugin = self;
302289 [NSURLProtocol registerClass: [CDVFilesystemURLProtocol class ]];
303290
304291 fileSystems_ = [[NSMutableArray alloc ] initWithCapacity: 3 ];
305292
306- // Get the Library directory path
307- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES );
308- self.appLibraryPath = [[paths objectAtIndex: 0 ] stringByAppendingPathComponent: @" files" ];
309-
310293 // Get the Temporary directory path
311294 self.appTempPath = [NSTemporaryDirectory ()stringByStandardizingPath]; // remove trailing slash from NSTemporaryDirectory()
295+ [self registerFilesystem: [[CDVLocalFilesystem alloc ] initWithName: @" temporary" root: self .appTempPath]];
312296
313- // Get the Documents directory path
314- paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
315- self.rootDocsPath = [paths objectAtIndex: 0 ];
316- self.appDocsPath = [self .rootDocsPath stringByAppendingPathComponent: @" files" ];
297+ // ~/Library/Application Support/<bundle-id>
298+ self.appSupportPath = [self getSupportDirectoryFor: NSApplicationSupportDirectory pathComponents: nil ];
317299
300+ // ~/Library/Application Support/<bundle-id>/files
301+ self.appDataPath = [self getSupportDirectoryFor: NSApplicationSupportDirectory pathComponents: @[@" files" ]];
302+ [self registerFilesystem: [[CDVLocalFilesystem alloc ] initWithName: @" persistent" root: self .appDataPath]];
318303
319- NSString *location = nil ;
320- if ([self .viewController isKindOfClass: [CDVViewController class ]]) {
321- CDVViewController *vc = (CDVViewController *)self.viewController ;
322- NSMutableDictionary *settings = vc.settings ;
323- location = [[settings objectForKey: @" iospersistentfilelocation" ] lowercaseString ];
324- }
325- if (location == nil ) {
326- // Compatibilty by default (if the config preference is not set, or
327- // if we're not embedded in a CDVViewController somehow.)
328- location = @" compatibility" ;
329- }
304+ // ~/Documents/
305+ self.appDocsPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES )[0 ];
330306
331- NSError *error;
332- if ([[NSFileManager defaultManager ] createDirectoryAtPath: self .appTempPath
333- withIntermediateDirectories: YES
334- attributes: nil
335- error: &error]) {
336- [self registerFilesystem: [[CDVLocalFilesystem alloc ] initWithName: @" temporary" root: self .appTempPath]];
337- } else {
338- NSLog (@" Unable to create temporary directory: %@ " , error);
339- }
340- if ([location isEqualToString: @" library" ]) {
341- if ([[NSFileManager defaultManager ] createDirectoryAtPath: self .appLibraryPath
342- withIntermediateDirectories: YES
343- attributes: nil
344- error: &error]) {
345- [self registerFilesystem: [[CDVLocalFilesystem alloc ] initWithName: @" persistent" root: self .appLibraryPath]];
346- } else {
347- NSLog (@" Unable to create library directory: %@ " , error);
348- }
349- } else if ([location isEqualToString: @" compatibility" ]) {
350- /*
351- * Fall-back to compatibility mode -- this is the logic implemented in
352- * earlier versions of this plugin, and should be maintained here so
353- * that apps which were originally deployed with older versions of the
354- * plugin can continue to provide access to files stored under those
355- * versions.
356- */
357- [self registerFilesystem: [[CDVLocalFilesystem alloc ] initWithName: @" persistent" root: self .rootDocsPath]];
358- } else {
359- NSAssert (false ,
360- @" File plugin configuration error: Please set iosPersistentFileLocation in config.xml to one of \" library\" (for new applications) or \" compatibility\" (for compatibility with previous versions)" );
361- }
307+ // ~/Library/Caches/<bundle-id>/files
308+ self.appCachePath = [self getSupportDirectoryFor: NSCachesDirectory pathComponents: nil ];
362309
363310 [self registerExtraFileSystems: [self getExtraFileSystemsPreference: self .viewController]
364311 fromAvailableSet: [self getAvailableFileSystems ]];
312+ }
313+
314+ - (NSString *) getSupportDirectoryFor : (NSSearchPathDirectory ) directory pathComponents : (NSArray *) components {
315+ NSError * error;
316+ NSFileManager * fm = [NSFileManager defaultManager ];
317+ NSURL * supportDir = [fm URLForDirectory: directory inDomain: NSUserDomainMask appropriateForURL: nil create: YES error: &error];
318+ if (supportDir == nil ) {
319+ NSLog (@" unable to get support directory: %@ " , error);
320+ return nil ;
321+ }
365322
323+ NSString * bundleID = [[NSBundle mainBundle ] bundleIdentifier ];
324+ NSURL *dirPath = [supportDir URLByAppendingPathComponent: bundleID];
325+ for (NSString * pathComponent in components) {
326+ dirPath = [dirPath URLByAppendingPathComponent: pathComponent];
327+ }
328+
329+ if (![fm fileExistsAtPath: dirPath.path]) {
330+ if (![fm createDirectoryAtURL: dirPath withIntermediateDirectories: YES attributes: nil error: &error]) {
331+ NSLog (@" unable to create support directory: %@ " , error);
332+ return nil ;
333+ }
334+ }
335+ return dirPath.path ;
366336}
367337
338+
368339- (CDVFilesystemURL *)fileSystemURLforArg : (NSString *)urlArg
369340{
370341 CDVFilesystemURL* ret = nil ;
@@ -446,7 +417,7 @@ - (void)requestFileSystem:(CDVInvokedUrlCommand*)command
446417 } else {
447418 NSString * fullPath = @" /" ;
448419 // check for avail space for size request
449- NSNumber * pNumAvail = [self checkFreeDiskSpace: self .rootDocsPath ];
420+ NSNumber * pNumAvail = [self checkFreeDiskSpace: self .appSupportPath ];
450421 // NSLog(@"Free space: %@", [NSString stringWithFormat:@"%qu", [ pNumAvail unsignedLongLongValue ]]);
451422 if (pNumAvail && ([pNumAvail unsignedLongLongValue ] < size)) {
452423 result = [CDVPluginResult resultWithStatus: CDVCommandStatus_IO_EXCEPTION messageAsInt: QUOTA_EXCEEDED_ERR];
@@ -480,28 +451,14 @@ - (void)requestAllFileSystems:(CDVInvokedUrlCommand*)command
480451
481452- (void )requestAllPaths : (CDVInvokedUrlCommand*)command
482453{
483- NSString * libPath = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES )[0 ];
484- NSString * libPathSync = [libPath stringByAppendingPathComponent: @" Cloud" ];
485- NSString * libPathNoSync = [libPath stringByAppendingPathComponent: @" NoCloud" ];
486- NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES )[0 ];
487- NSString * storagePath = [libPath stringByDeletingLastPathComponent ];
488- NSString * cachePath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES )[0 ];
489-
490- // Create the directories if necessary.
491- [[NSFileManager defaultManager ] createDirectoryAtPath: libPathSync withIntermediateDirectories: YES attributes: nil error: nil ];
492- [[NSFileManager defaultManager ] createDirectoryAtPath: libPathNoSync withIntermediateDirectories: YES attributes: nil error: nil ];
493- // Mark NoSync as non-iCloud.
494- [[NSURL fileURLWithPath: libPathNoSync] setResourceValue: [NSNumber numberWithBool: YES ]
495- forKey: NSURLIsExcludedFromBackupKey error: nil ];
496-
497454 NSDictionary * ret = @{
498455 @" applicationDirectory" : [[NSURL fileURLWithPath: [[NSBundle mainBundle ] bundlePath ]] absoluteString ],
499- @" applicationStorageDirectory" : [[NSURL fileURLWithPath: storagePath ] absoluteString ],
500- @" dataDirectory" : [[NSURL fileURLWithPath: libPathNoSync ] absoluteString ],
501- @" syncedDataDirectory " : [[NSURL fileURLWithPath: libPathSync ] absoluteString ],
502- @" documentsDirectory " : [[NSURL fileURLWithPath: docPath ] absoluteString ],
503- @" cacheDirectory " : [[NSURL fileURLWithPath: cachePath ] absoluteString ],
504- @" tempDirectory " : [[ NSURL fileURLWithPath: NSTemporaryDirectory ()] absoluteString ]
456+ @" applicationStorageDirectory" : [[NSURL fileURLWithPath: self .appSupportPath ] absoluteString ],
457+ @" dataDirectory" : [[NSURL fileURLWithPath: self .appDataPath ] absoluteString ],
458+ @" documentsDirectory " : [[NSURL fileURLWithPath: self .appDocsPath ] absoluteString ],
459+ @" cacheDirectory " : [[NSURL fileURLWithPath: self .appCachePath ] absoluteString ],
460+ @" tempDirectory " : [[NSURL fileURLWithPath: self .appTempPath ] absoluteString ],
461+ @" rootDirectory " : @" file:/// " ,
505462 };
506463
507464 CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: ret];
0 commit comments