Skip to content

Commit 64f94ae

Browse files
committed
feat(ios): add app bundle and container path support for hpfile url
1 parent c5a676c commit 64f94ae

File tree

3 files changed

+112
-9
lines changed

3 files changed

+112
-9
lines changed

framework/ios/module/loader/HippyFileHandler.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class HippyFileHandler : public VFSUriHandler {
3030
public:
3131
HippyFileHandler() = delete;
3232
HippyFileHandler(HippyBridge *bridge);
33-
33+
3434
virtual void RequestUntrustedContent(
3535
std::shared_ptr<hippy::RequestJob> request,
3636
std::shared_ptr<hippy::JobResponse> response,
@@ -40,12 +40,19 @@ class HippyFileHandler : public VFSUriHandler {
4040
std::function<void(std::shared_ptr<hippy::JobResponse>)> cb,
4141
std::function<std::shared_ptr<UriHandler>()> next) override;
4242

43+
/// Convert relative addresses(such as hpfile://) to absolute paths
44+
/// - Parameters:
45+
/// - hippyFileUrl: file url
46+
/// - hippySandboxDirectory: sandbox directory of hippy app
47+
static NSURL *AbsoluteURLFromHippyFileURL(NSURL *hippyFileUrl, NSURL *hippySandboxDirectory);
48+
4349
virtual void RequestUntrustedContent(NSURLRequest *request,
4450
NSDictionary *extraInfo,
4551
NSOperationQueue *queue,
4652
VFSHandlerProgressBlock progress,
47-
VFSHandlerCompletionBlock completion,
53+
VFSHandlerCompletionBlock completion,
4854
VFSGetNextHandlerBlock next) override;
55+
4956
private:
5057
__weak HippyBridge *bridge_;
5158
};

framework/ios/module/loader/HippyFileHandler.mm

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#import "HippyBridge.h"
2424
#import "HippyUtils.h"
25-
2625
#include "HippyFileHandler.h"
2726
#include "footstone/logging.h"
2827

@@ -42,6 +41,35 @@
4241
FOOTSTONE_UNIMPLEMENTED();
4342
}
4443

44+
NSURL *HippyFileHandler::AbsoluteURLFromHippyFileURL(NSURL *fileUrl, NSURL *hippySandboxDirectory) {
45+
static NSString *defaultHippyLocalFileURLPrefix = @"hpfile://";
46+
static NSString *hippyLocalRelativeFilePathPrefix = @"./";
47+
static NSString *hippyLocalAppBundleFilePathPrefix = @"appbundle/";
48+
static NSString *hippyLocalContainerFilePathPrefix = @"container/";
49+
50+
NSURL *absoluteURL = fileUrl;
51+
NSString *urlString = [fileUrl absoluteString];
52+
if ([urlString hasPrefix:defaultHippyLocalFileURLPrefix]) {
53+
NSString *path = [urlString substringFromIndex:[defaultHippyLocalFileURLPrefix length]];
54+
55+
if ([path hasPrefix:hippyLocalRelativeFilePathPrefix]) {
56+
// Hippy Sandbox Relative Path
57+
NSString *relativePath = [path substringFromIndex:hippyLocalRelativeFilePathPrefix.length];
58+
absoluteURL = [NSURL fileURLWithPath:relativePath relativeToURL:hippySandboxDirectory];
59+
} else if ([path hasPrefix:hippyLocalAppBundleFilePathPrefix]) {
60+
// App Bundle Path
61+
NSString *relativePath = [path substringFromIndex:[hippyLocalAppBundleFilePathPrefix length]];
62+
absoluteURL = [[NSBundle mainBundle] URLForResource:relativePath withExtension:nil];
63+
} else if ([path hasPrefix:hippyLocalContainerFilePathPrefix]) {
64+
// App Container Path
65+
NSString *relativePath = [path substringFromIndex:[hippyLocalContainerFilePathPrefix length]];
66+
NSString *containerPath = [NSHomeDirectory() stringByAppendingPathComponent:relativePath];
67+
absoluteURL = [NSURL fileURLWithPath:containerPath];
68+
}
69+
}
70+
return absoluteURL;
71+
}
72+
4573
void HippyFileHandler::RequestUntrustedContent(NSURLRequest *request,
4674
NSDictionary *extraInfo,
4775
NSOperationQueue *queue,
@@ -62,12 +90,7 @@
6290
return;
6391
}
6492

65-
NSURL *absoluteURL = url;
66-
static NSString *defaultHippyLocalFileURLPrefix = @"hpfile://.";
67-
if ([[url absoluteString] hasPrefix:defaultHippyLocalFileURLPrefix]) {
68-
NSString *path = [[url absoluteString] substringFromIndex:[defaultHippyLocalFileURLPrefix length] - 1];
69-
absoluteURL = [NSURL fileURLWithPath:path relativeToURL:bridge.sandboxDirectory];
70-
}
93+
NSURL *absoluteURL = AbsoluteURLFromHippyFileURL(url, bridge.sandboxDirectory);
7194
if ([absoluteURL isFileURL] || [absoluteURL isFileReferenceURL]) {
7295
void (^opBlock)() = ^{
7396
NSError *error;

tests/ios/HippyFileHandlerTest.mm

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*!
2+
* iOS SDK
3+
*
4+
* Tencent is pleased to support the open source community by making
5+
* Hippy available.
6+
*
7+
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
8+
* All rights reserved.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
#import <XCTest/XCTest.h>
24+
#import <hippy/HippyBridge.h>
25+
#import <hippy/HippyFileHandler.h>
26+
27+
@interface HippyFileHandlerTest : XCTestCase
28+
29+
/// Test sandboxDirectory for file handler
30+
@property (nonatomic, strong) NSURL *sandboxDirectory;
31+
32+
@end
33+
34+
@implementation HippyFileHandlerTest
35+
36+
- (void)setUp {
37+
self.sandboxDirectory = [NSURL fileURLWithPath:@"/path/to/sandbox"];
38+
}
39+
40+
- (void)tearDown {
41+
// Put teardown code here. This method is called after the invocation of each test method in the class.
42+
}
43+
44+
- (void)testAbsoluteURLFromHippyFileURL_AppBundlePath {
45+
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://appbundle/testfile.txt"];
46+
NSURL *expectedURL = [[NSBundle mainBundle] URLForResource:@"testfile" withExtension:@"txt"];
47+
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
48+
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for app bundle paths.");
49+
}
50+
51+
- (void)testAbsoluteURLFromHippyFileURL_ContainerPath {
52+
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://container/Documents/testfile.txt"];
53+
NSString *containerPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/testfile.txt"];
54+
NSURL *expectedURL = [NSURL fileURLWithPath:containerPath];
55+
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
56+
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for container paths.");
57+
}
58+
59+
- (void)testAbsoluteURLFromHippyFileURL_SandboxRelativePath {
60+
NSURL *fileUrl = [NSURL URLWithString:@"hpfile://./testfile.txt"];
61+
NSURL *expectedURL = [NSURL fileURLWithPath:@"testfile.txt" relativeToURL:self.sandboxDirectory];
62+
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
63+
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for sandbox relative paths.");
64+
}
65+
66+
- (void)testAbsoluteURLFromHippyFileURL_InvalidPrefix {
67+
NSURL *fileUrl = [NSURL URLWithString:@"invalid://testfile.txt"];
68+
NSURL *expectedURL = fileUrl;
69+
NSURL *resultURL = HippyFileHandler::AbsoluteURLFromHippyFileURL(fileUrl,self.sandboxDirectory);
70+
XCTAssertEqualObjects(resultURL, expectedURL, @"The URLs should be equal for invalid prefixes.");
71+
}
72+
73+
@end

0 commit comments

Comments
 (0)