Revert "[Video] Download videos" (#4945)

This commit is contained in:
Hailey 2024-08-15 16:29:16 -07:00 committed by GitHub
parent b6e515c664
commit a5af24b53b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 3 additions and 747 deletions

View file

@ -1,31 +0,0 @@
import ExpoModulesCore
public class ExpoHLSDownloadModule: Module {
public func definition() -> ModuleDefinition {
Name("ExpoHLSDownload")
Function("isAvailable") {
if #available(iOS 14.5, *) {
return true
}
return false
}
View(HLSDownloadView.self) {
Events([
"onStart",
"onError",
"onProgress",
"onSuccess"
])
Prop("downloaderUrl") { (view: HLSDownloadView, downloaderUrl: URL) in
view.downloaderUrl = downloaderUrl
}
AsyncFunction("startDownloadAsync") { (view: HLSDownloadView, sourceUrl: URL) in
view.startDownload(sourceUrl: sourceUrl)
}
}
}
}

View file

@ -1,148 +0,0 @@
import ExpoModulesCore
import WebKit
class HLSDownloadView: ExpoView, WKScriptMessageHandler, WKNavigationDelegate, WKDownloadDelegate {
var webView: WKWebView!
var downloaderUrl: URL?
private var onStart = EventDispatcher()
private var onError = EventDispatcher()
private var onProgress = EventDispatcher()
private var onSuccess = EventDispatcher()
private var outputUrl: URL?
public required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
// controller for post message api
let contentController = WKUserContentController()
contentController.add(self, name: "onMessage")
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
// create webview
let webView = WKWebView(frame: .zero, configuration: configuration)
// Use these for debugging, to see the webview itself
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.layer.masksToBounds = false
webView.backgroundColor = .clear
webView.contentMode = .scaleToFill
webView.navigationDelegate = self
self.addSubview(webView)
self.webView = webView
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - view functions
func startDownload(sourceUrl: URL) {
guard let downloaderUrl = self.downloaderUrl,
let url = URL(string: "\(downloaderUrl.absoluteString)?videoUrl=\(sourceUrl.absoluteString)") else {
self.onError([
"message": "Downloader URL is not set."
])
return
}
self.onStart()
self.webView.load(URLRequest(url: url))
}
// webview message handling
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let response = message.body as? String,
let data = response.data(using: .utf8),
let payload = try? JSONDecoder().decode(WebViewActionPayload.self, from: data) else {
self.onError([
"message": "Failed to decode JSON post message."
])
return
}
switch payload.action {
case .progress:
guard let progress = payload.messageFloat else {
self.onError([
"message": "Failed to decode JSON post message."
])
return
}
self.onProgress([
"progress": progress
])
case .error:
guard let messageStr = payload.messageStr else {
self.onError([
"message": "Failed to decode JSON post message."
])
return
}
self.onError([
"message": messageStr
])
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
guard #available(iOS 14.5, *) else {
return .cancel
}
if navigationAction.shouldPerformDownload {
return .download
} else {
return .allow
}
}
// MARK: - wkdownloaddelegate
@available(iOS 14.5, *)
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
download.delegate = self
}
@available(iOS 14.5, *)
func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
download.delegate = self
}
@available(iOS 14.5, *)
func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
let directory = NSTemporaryDirectory()
let fileName = "\(NSUUID().uuidString).mp4"
let url = NSURL.fileURL(withPathComponents: [directory, fileName])
self.outputUrl = url
completionHandler(url)
}
@available(iOS 14.5, *)
func downloadDidFinish(_ download: WKDownload) {
guard let url = self.outputUrl else {
return
}
self.onSuccess([
"uri": url.absoluteString
])
self.outputUrl = nil
}
}
struct WebViewActionPayload: Decodable {
enum Action: String, Decodable {
case progress, error
}
let action: Action
let messageStr: String?
let messageFloat: Float?
}