Skip to content

Commit

Permalink
[Feat] #484 - downloadHandlers를 관리하는 manager 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
meltsplit committed Jan 24, 2025
1 parent d74fed5 commit 89e6ce1
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// WKDownloadManager.swift
// BaseFeatureDependency
//
// Created by 장석우 on 1/24/25.
// Copyright © 2025 SOPT-iOS. All rights reserved.
//

import Foundation
import UniformTypeIdentifiers
import WebKit

public final class WKDownloadManager {
private var downloadHandlers: [UTType: WKDownloadExecutable]
var webVC: SOPTWebViewControllable?

init(
downloadHandlers: [UTType : WKDownloadExecutable] = [:],
webVC: SOPTWebViewControllable? = nil
) {
self.downloadHandlers = downloadHandlers
self.webVC = webVC
}

public func register<T: WKDownloadExecutable>(_ object: T) {
self.downloadHandlers[T.key] = object
}

func download(
_ download: WKDownload,
decideDestinationUsing response: URLResponse,
suggestedFilename: String
) async -> URL? {
guard let mimeType = response.mimeType,
let utType = UTType(mimeType: mimeType)
else { return nil }

guard let handler = downloadHandlers.first(where: { utType.conforms(to: $0.key) })?.value
else { return nil }

return await handler.execute(download, response, suggestedFilename, webVC)
}

public static let `default`: WKDownloadManager = {
let manager = WKDownloadManager()
manager.register(WKImageDownloadHandler())
return manager
}()
}


0 comments on commit 89e6ce1

Please sign in to comment.