Skip to content

Commit

Permalink
feat(ios): add app bundle and container path support for hpfile url
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Jul 9, 2024
1 parent a7dcbdb commit e4b4283
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 8 deletions.
8 changes: 7 additions & 1 deletion framework/ios/module/loader/HippyFileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ class HippyFileHandler : public VFSUriHandler {
std::shared_ptr<hippy::RequestJob> request,
std::function<void(std::shared_ptr<hippy::JobResponse>)> cb,
std::function<std::shared_ptr<UriHandler>()> next) override;


/// Convert relative addresses(such as hpfile://) to absolute paths
/// - Parameters:
/// - hippyFileUrl: file url
/// - hippySandboxDirectory: sandbox directory of hippy app
static NSURL *AbsoluteURLFromHippyFileURL(NSURL *hippyFileUrl, NSURL *hippySandboxDirectory);

virtual void RequestUntrustedContent(NSURLRequest *request,
NSDictionary *extraInfo,
NSOperationQueue *queue,
Expand Down
37 changes: 30 additions & 7 deletions framework/ios/module/loader/HippyFileHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#import "HippyBridge.h"
#import "HippyUtils.h"

#include "HippyFileHandler.h"
#include "footstone/logging.h"

Expand All @@ -42,6 +41,35 @@
FOOTSTONE_UNIMPLEMENTED();
}

NSURL *HippyFileHandler::AbsoluteURLFromHippyFileURL(NSURL *fileUrl, NSURL *hippySandboxDirectory) {
static NSString *defaultHippyLocalFileURLPrefix = @"hpfile://";
static NSString *hippyLocalRelativeFilePathPrefix = @"./";
static NSString *hippyLocalAppBundleFilePathPrefix = @"appbundle/";
static NSString *hippyLocalContainerFilePathPrefix = @"container/";

NSURL *absoluteURL = fileUrl;
NSString *urlString = [fileUrl absoluteString];
if ([urlString hasPrefix:defaultHippyLocalFileURLPrefix]) {
NSString *path = [urlString substringFromIndex:[defaultHippyLocalFileURLPrefix length]];

if ([path hasPrefix:hippyLocalRelativeFilePathPrefix]) {
// Hippy Sandbox Relative Path
NSString *relativePath = [path substringFromIndex:hippyLocalRelativeFilePathPrefix.length];
absoluteURL = [NSURL fileURLWithPath:relativePath relativeToURL:hippySandboxDirectory];
} else if ([path hasPrefix:hippyLocalAppBundleFilePathPrefix]) {
// App Bundle Path
NSString *relativePath = [path substringFromIndex:[hippyLocalAppBundleFilePathPrefix length]];
absoluteURL = [[NSBundle mainBundle] URLForResource:relativePath withExtension:nil];
} else if ([path hasPrefix:hippyLocalContainerFilePathPrefix]) {
// App Container Path
NSString *relativePath = [path substringFromIndex:[hippyLocalContainerFilePathPrefix length]];
NSString *containerPath = [NSHomeDirectory() stringByAppendingPathComponent:relativePath];
absoluteURL = [NSURL fileURLWithPath:containerPath];
}
}
return absoluteURL;
}

void HippyFileHandler::RequestUntrustedContent(NSURLRequest *request,
NSDictionary *extraInfo,
NSOperationQueue *queue,
Expand All @@ -62,12 +90,7 @@
return;
}

NSURL *absoluteURL = url;
static NSString *defaultHippyLocalFileURLPrefix = @"hpfile://.";
if ([[url absoluteString] hasPrefix:defaultHippyLocalFileURLPrefix]) {
NSString *path = [[url absoluteString] substringFromIndex:[defaultHippyLocalFileURLPrefix length] - 1];
absoluteURL = [NSURL fileURLWithPath:path relativeToURL:bridge.sandboxDirectory];
}
NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, bridge.sandboxDirectory);
if ([absoluteURL isFileURL] || [absoluteURL isFileReferenceURL]) {
void (^opBlock)() = ^{
NSError *error;
Expand Down
73 changes: 73 additions & 0 deletions tests/ios/HippyFileHandlerTest.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>
#import <hippy/HippyBridge.h>
#import <hippy/HippyFileHandler.h>

@interface HippyFileHandlerTest : XCTestCase

/// Test sandboxDirectory for file handler
@property (nonatomic, strong) NSURL *sandboxDirectory;

@end

@implementation HippyFileHandlerTest

- (void)setUp {
self.sandboxDirectory = [NSURL fileURLWithPath:@"/path/to/sandbox"];
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testAbsoluteURLFromHippyFileURL_AppBundlePath {
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://appbundle/testfile.txt"];
NSURL *expectedURL = [[NSBundle mainBundle] URLForResource:@"testfile" withExtension:@"txt"];
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for app bundle paths.");
}

- (void)testAbsoluteURLFromHippyFileURL_ContainerPath {
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://container/Documents/testfile.txt"];
NSString *containerPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/testfile.txt"];
NSURL *expectedURL = [NSURL fileURLWithPath:containerPath];
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for container paths.");
}

- (void)testAbsoluteURLFromHippyFileURL_SandboxRelativePath {
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://./testfile.txt"];
NSURL *expectedURL = [NSURL fileURLWithPath:@"testfile.txt" relativeToURL:self.sandboxDirectory];
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for sandbox relative paths.");
}

- (void)testAbsoluteURLFromHippyFileURL_InvalidPrefix {
NSURL *fileUrl = [NSURL URLWithString:@"invalid://testfile.txt"];
NSURL *expectedURL = fileUrl;
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for invalid prefixes.");
}

@end

0 comments on commit e4b4283

Please sign in to comment.