Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): add app bundle and container path support for hpfile url #3951

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions framework/ios/module/loader/HippyFileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HippyFileHandler : public VFSUriHandler {
public:
HippyFileHandler() = delete;
HippyFileHandler(HippyBridge *bridge);

virtual void RequestUntrustedContent(
std::shared_ptr<hippy::RequestJob> request,
std::shared_ptr<hippy::JobResponse> response,
Expand All @@ -40,12 +40,19 @@ class HippyFileHandler : public VFSUriHandler {
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,
VFSHandlerProgressBlock progress,
VFSHandlerCompletionBlock completion,
VFSHandlerCompletionBlock completion,
VFSGetNextHandlerBlock next) override;

private:
__weak HippyBridge *bridge_;
};
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
Loading