Skip to content

Commit 2ed379c

Browse files
committed
CB-8032 - File Plugin - Add nativeURL external method support for CDVFileSystem->makeEntryForPath:isDirectory: (closes apache#96)
1 parent 64bfc38 commit 2ed379c

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/ios/CDVAssetLibraryFilesystem.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2929
NSString* const kCDVAssetsLibraryScheme = @"assets-library";
3030

3131
@implementation CDVAssetLibraryFilesystem
32-
@synthesize name=_name;
32+
@synthesize name=_name, urlTransformer;
3333

3434

3535
/*
@@ -71,7 +71,12 @@ - (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir
7171
[dirEntry setObject:fullPath forKey:@"fullPath"];
7272
[dirEntry setObject:lastPart forKey:@"name"];
7373
[dirEntry setObject:self.name forKey: @"filesystemName"];
74-
dirEntry[@"nativeURL"] = [NSString stringWithFormat:@"assets-library:/%@",fullPath];
74+
75+
NSURL* nativeURL = [NSURL URLWithString:[NSString stringWithFormat:@"assets-library:/%@",fullPath]];
76+
if (self.urlTransformer) {
77+
nativeURL = self.urlTransformer(nativeURL);
78+
}
79+
dirEntry[@"nativeURL"] = [nativeURL absoluteString];
7580

7681
return dirEntry;
7782
}

src/ios/CDVFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef int CDVFileError;
8080
- (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir;
8181

8282
@property (nonatomic,strong) NSString *name;
83+
@property (nonatomic, copy) NSURL*(^urlTransformer)(NSURL*);
8384

8485
@optional
8586
- (NSString *)filesystemPathForURL:(CDVFilesystemURL *)localURI;

src/ios/CDVFile.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2121
#import "CDVFile.h"
2222
#import "CDVLocalFilesystem.h"
2323
#import "CDVAssetLibraryFilesystem.h"
24+
#import <objc/message.h>
2425

2526
CDVFile *filePlugin = nil;
2627

@@ -173,6 +174,24 @@ @implementation CDVFile
173174
@synthesize rootDocsPath, appDocsPath, appLibraryPath, appTempPath, userHasAllowed, fileSystems=fileSystems_;
174175

175176
- (void)registerFilesystem:(NSObject<CDVFileSystem> *)fs {
177+
__weak CDVFile* weakSelf = self;
178+
SEL sel = NSSelectorFromString(@"urlTransformer");
179+
// for backwards compatibility - we check if this property is there
180+
// we create a wrapper block because the urlTransformer property
181+
// on the commandDelegate might be set dynamically at a future time
182+
// (and not dependent on plugin loading order)
183+
if ([self.commandDelegate respondsToSelector:sel]) {
184+
fs.urlTransformer = ^NSURL*(NSURL* urlToTransform) {
185+
// grab the block from the commandDelegate
186+
NSURL* (^urlTransformer)(NSURL*) = ((id(*)(id, SEL))objc_msgSend)(weakSelf.commandDelegate, sel);
187+
// if block is not null, we call it
188+
if (urlTransformer) {
189+
return urlTransformer(urlToTransform);
190+
} else { // else we return the same url
191+
return urlToTransform;
192+
}
193+
};
194+
}
176195
[fileSystems_ addObject:fs];
177196
}
178197

src/ios/CDVLocalFilesystem.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2424
#import <sys/xattr.h>
2525

2626
@implementation CDVLocalFilesystem
27-
@synthesize name=_name, fsRoot=_fsRoot;
27+
@synthesize name=_name, fsRoot=_fsRoot, urlTransformer;
2828

2929
- (id) initWithName:(NSString *)name root:(NSString *)fsRoot
3030
{
@@ -78,7 +78,13 @@ - (NSDictionary*)makeEntryForPath:(NSString*)fullPath isDirectory:(BOOL)isDir
7878
[dirEntry setObject:fullPath forKey:@"fullPath"];
7979
[dirEntry setObject:lastPart forKey:@"name"];
8080
[dirEntry setObject:self.name forKey: @"filesystemName"];
81-
dirEntry[@"nativeURL"] = [[NSURL fileURLWithPath:[self filesystemPathForFullPath:fullPath]] absoluteString];
81+
82+
NSURL* nativeURL = [NSURL fileURLWithPath:[self filesystemPathForFullPath:fullPath]];
83+
if (self.urlTransformer) {
84+
nativeURL = self.urlTransformer(nativeURL);
85+
}
86+
87+
dirEntry[@"nativeURL"] = [nativeURL absoluteString];
8288

8389
return dirEntry;
8490
}

0 commit comments

Comments
 (0)