Skip to content

Commit

Permalink
fix(ios): fix Ti.Filesystem.getAsset and getFile on devices
Browse files Browse the repository at this point in the history
- properly strip resources dir prefix from __filename/__dirname on device
  • Loading branch information
sgtcoolguy committed Aug 13, 2020
1 parent b3ff0a2 commit f57e938
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
49 changes: 26 additions & 23 deletions iphone/Classes/FilesystemModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,22 @@ - (NSString *)lineEnding

- (JSValue *)getFile
{
NSArray *args = [JSContext currentArguments];
NSArray *args = JSContext.currentArguments;
NSString *newpath = [self pathFromComponents:args];
TiFile *fileProxy = [self getFileProxy:newpath];
return [self NativeToJSValue:fileProxy];
}

- (TiFile *)getFileProxy:(NSString *)path
{
if ([path hasPrefix:[self resourcesDirectory]] && ([path hasSuffix:@".js"] || [path hasSuffix:@".json"])) {
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [TiUtils loadAppResource:url];
if (data != nil) {
return [[[TiFilesystemBlobProxy alloc] initWithURL:url data:data] autorelease];
if ([path hasSuffix:@".js"] || [path hasSuffix:@".json"]) {
NSString *resourcesDir = [self resourcesDirectory];
if ([path hasPrefix:resourcesDir] || [path hasPrefix:[resourcesDir stringByStandardizingPath]]) {
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [TiUtils loadAppResource:url];
if (data != nil) {
return [[[TiFilesystemBlobProxy alloc] initWithURL:url data:data] autorelease];
}
}
}

Expand All @@ -199,25 +202,25 @@ - (TiFile *)getFileProxy:(NSString *)path

- (TiBlob *)getAsset
{
NSArray *args = [JSContext currentArguments];
NSArray *args = JSContext.currentArguments;
NSString *newpath = [self pathFromComponents:args];

if ([newpath hasPrefix:[self resourcesDirectory]] && ([newpath hasSuffix:@".jpg"] || [newpath hasSuffix:@".png"])) {
UIImage *image = nil;
NSRange range = [newpath rangeOfString:@".app"];
NSString *imageArg = nil;
if (range.location != NSNotFound) {
imageArg = [newpath substringFromIndex:range.location + 5];
if ([newpath hasSuffix:@".jpg"] || [newpath hasSuffix:@".png"]) {
NSString *resourcesDir = [self resourcesDirectory];
if ([newpath hasPrefix:resourcesDir] || [newpath hasPrefix:[resourcesDir stringByStandardizingPath]]) {
NSRange range = [newpath rangeOfString:@".app"];
if (range.location != NSNotFound) {
NSString *imageArg = [newpath substringFromIndex:range.location + 5];
//remove suffixes.
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@3x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~iphone" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~ipad" withString:@""];

UIImage *image = [UIImage imageNamed:imageArg];

return [[[TiBlob alloc] initWithImage:image] autorelease];
}
}
//remove suffixes.
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@3x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~iphone" withString:@""];
imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~ipad" withString:@""];

image = [UIImage imageNamed:imageArg];

return [[[TiBlob alloc] initWithImage:image] autorelease];
}
return nil;
}
Expand Down
23 changes: 18 additions & 5 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ - (void)didStartNewContext:(KrollContext *)kroll
JSPropertyDescriptorConfigurableKey : @NO,
JSPropertyDescriptorValueKey : global
}];
// TODO: Move to real paths for __dirname/__filename, but that affects Android and may break users/debugging?
// NSString *dirname = [[TiHost resourcePath] stringByStandardizingPath];
// Set the __dirname and __filename for the app.js.
// For other files, it will be injected via the `TitaniumModuleRequireFormat` property
[global defineProperty:@"__dirname"
Expand All @@ -423,6 +425,7 @@ - (void)didStartNewContext:(KrollContext *)kroll
JSPropertyDescriptorConfigurableKey : @NO,
JSPropertyDescriptorValueKey : @"/"
}];
// NSString *filename = [dirname stringByAppendingString:@"/app.js"];
[global defineProperty:@"__filename"
descriptor:@{
JSPropertyDescriptorEnumerableKey : @NO,
Expand Down Expand Up @@ -672,12 +675,22 @@ - (id)krollObjectForProxy:(id)proxy

- (KrollWrapper *)loadCommonJSModule:(NSString *)code withSourceURL:(NSURL *)sourceURL
{
// FIXME: Can we skip all of this now? Doesn't we already properly resolve paths?
// FIXME: Can we skip this now? Don't we already properly resolve paths?
// This takes care of resolving paths like `../../foo.js`
sourceURL = [NSURL fileURLWithPath:[[sourceURL path] stringByStandardizingPath]];

// Get the relative path to the Resources directory
NSString *filename = [[sourceURL path] stringByReplacingOccurrencesOfString:[[[NSBundle mainBundle] resourceURL] path] withString:@""];
sourceURL = [sourceURL URLByStandardizingPath];
NSString *filename = [sourceURL path];

// TODO: We should likely move away from "faked" / root being resources dir
// And report the real full path in __filename/__dirname, but that may be a breaking change and would impact Android for parity
NSString *resourcesPath = [TiHost resourcePath];
NSString *standardized = [resourcesPath stringByStandardizingPath];
// Strip resources dir from prefix of file url (if it matches) for __filename
if ([filename hasPrefix:resourcesPath]) {
filename = [filename stringByReplacingOccurrencesOfString:resourcesPath withString:@""];
} else if (![resourcesPath isEqualToString:standardized] && [filename hasPrefix:standardized]) {
filename = [filename stringByReplacingOccurrencesOfString:standardized withString:@""];
}
// strip basename for __dirname
NSString *dirname = [filename stringByDeletingLastPathComponent];

NSString *js = [[NSString alloc] initWithFormat:TitaniumModuleRequireFormat, dirname, filename, code];
Expand Down
16 changes: 11 additions & 5 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -1560,15 +1560,21 @@ + (CGRect)viewPositionRect:(UIView *)view
+ (NSData *)loadAppResource:(NSURL *)url
{
BOOL app = [[url scheme] hasPrefix:@"app"];
BOOL isFileURL = [url isFileURL];

if ([url isFileURL] || app) {
if (isFileURL || app) {
BOOL leadingSlashRemoved = NO;
NSString *urlstring = [[url standardizedURL] path];
NSString *resourceurl = [[NSBundle mainBundle] resourcePath];
NSRange range = [urlstring rangeOfString:resourceurl];
NSString *appurlstr = urlstring;
if (range.location != NSNotFound) {
appurlstr = [urlstring substringFromIndex:range.location + range.length + 1];
if (isFileURL) {
// strip resources dir filepath prefix (if it's there)
NSString *resourceurl = [[NSBundle mainBundle] resourcePath];
NSString *standardized = [resourceurl stringByStandardizingPath];
if ([urlstring hasPrefix:resourceurl]) {
appurlstr = [urlstring stringByReplacingOccurrencesOfString:resourceurl withString:@""];
} else if (![resourceurl isEqualToString:standardized] && [urlstring hasPrefix:standardized]) {
appurlstr = [urlstring stringByReplacingOccurrencesOfString:standardized withString:@""];
}
}
if ([appurlstr hasPrefix:@"/"]) {
#ifndef __clang_analyzer__
Expand Down

0 comments on commit f57e938

Please sign in to comment.