diff --git a/android/build.gradle b/android/build.gradle index eea3382..9096420 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -73,6 +73,7 @@ repositories { dependencies { //noinspection GradleDynamicVersion implementation 'com.facebook.react:react-native:+' // From node_modules + implementation 'com.danikula:videocache:2.7.1' } def configureReactNativePom(def pom) { diff --git a/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java b/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java index 224a1a5..fbbfe66 100644 --- a/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java +++ b/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java @@ -1,13 +1,15 @@ package com.reactnative.videocache; +import com.danikula.videocache.HttpProxyCacheServer; +import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.Callback; public class VideoCacheModule extends ReactContextBaseJavaModule { private final ReactApplicationContext reactContext; + private HttpProxyCacheServer proxy; public VideoCacheModule(ReactApplicationContext reactContext) { super(reactContext); @@ -20,8 +22,12 @@ public class VideoCacheModule extends ReactContextBaseJavaModule { } @ReactMethod - public void sampleMethod(String stringArgument, int numberArgument, Callback callback) { - // TODO: Implement some actually useful functionality - callback.invoke("Received numberArgument: " + numberArgument + " stringArgument: " + stringArgument); + public void convert( + String url, + Promise promise) { + if (this.proxy == null) { + this.proxy = new HttpProxyCacheServer(this.reactContext); + } + promise.resolve(this.proxy.getProxyUrl(url)); } } diff --git a/example/App.js b/example/App.js index 4a24dd7..ff960e0 100644 --- a/example/App.js +++ b/example/App.js @@ -18,7 +18,7 @@ export default class App extends Component<{}> { message: '--' }; componentDidMount() { - VideoCache.sampleMethod('Testing', 123, (message) => { + VideoCache.convert('https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-720p.mp4').then((message) => { this.setState({ status: 'native callback received', message diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index f27ff21..8bfc08f 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,14 +1,20 @@ PODS: - boost-for-react-native (1.63.0) + - CocoaAsyncSocket (7.6.3) - DoubleConversion (1.1.6) - Folly (2018.10.22.00): - boost-for-react-native - DoubleConversion - glog - glog (0.3.5) + - KTVCocoaHTTPServer (1.0.0): + - CocoaAsyncSocket + - KTVHTTPCache (2.0.1): + - KTVCocoaHTTPServer - React (0.59.10): - React/Core (= 0.59.10) - react-native-video-cache (1.0.0): + - KTVHTTPCache (~> 2.0.0) - React - React/Core (0.59.10): - yoga (= 0.59.10.React) @@ -96,6 +102,9 @@ DEPENDENCIES: SPEC REPOS: trunk: - boost-for-react-native + - CocoaAsyncSocket + - KTVCocoaHTTPServer + - KTVHTTPCache EXTERNAL SOURCES: DoubleConversion: @@ -113,11 +122,14 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c + CocoaAsyncSocket: eafaa68a7e0ec99ead0a7b35015e0bf25d2c8987 DoubleConversion: bb338842f62ab1d708ceb63ec3d999f0f3d98ecd Folly: de497beb10f102453a1afa9edbf8cf8a251890de glog: aefd1eb5dda2ab95ba0938556f34b98e2da3a60d + KTVCocoaHTTPServer: df8d7b861e603ff8037e9b2138aca2563a6b768d + KTVHTTPCache: 588c3eb16f6bd1e6fde1e230dabfb7bd4e490a4d React: 36d0768f9e93be2473b37e7fa64f92c1d5341eef - react-native-video-cache: 50bf2a6db6f7a6b4290138418cb1de6aeac61e9f + react-native-video-cache: a9b3bb7c7ad2c33348a35ed5617ac93cf179c9f9 yoga: 684513b14b03201579ba3cee20218c9d1298b0cc PODFILE CHECKSUM: 8cb65c707639127200176815b46fe7b0846ce649 diff --git a/ios/VideoCache.m b/ios/VideoCache.m index ce2b0cb..8288aa4 100644 --- a/ios/VideoCache.m +++ b/ios/VideoCache.m @@ -1,14 +1,25 @@ #import "VideoCache.h" +#import @implementation VideoCache RCT_EXPORT_MODULE() -RCT_EXPORT_METHOD(sampleMethod:(NSString *)stringArgument numberParameter:(nonnull NSNumber *)numberArgument callback:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(convert:(NSString *)url + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { - // TODO: Implement some actually useful functionality - callback(@[[NSString stringWithFormat: @"numberArgument: %@ stringArgument: %@", numberArgument, stringArgument]]); + if (!KTVHTTPCache.proxyIsRunning) { + NSError *error; + [KTVHTTPCache proxyStart:&error]; + if (error) { + reject(@"init.error", @"failed to start proxy server", error); + return; + } + } + resolve([KTVHTTPCache proxyURLWithOriginalURL:[NSURL URLWithString:url]].absoluteString); } + @end diff --git a/react-native-video-cache.podspec b/react-native-video-cache.podspec index ddc2e82..0cb3526 100644 --- a/react-native-video-cache.podspec +++ b/react-native-video-cache.podspec @@ -20,6 +20,7 @@ Pod::Spec.new do |s| s.requires_arc = true s.dependency "React" + s.dependency 'KTVHTTPCache', '~> 2.0.0' # ... # s.dependency "..." end