feat(drone-dock): 接入成片云端交付并精简准备页定位展示
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import Foundation
|
||||
|
||||
#if canImport(AlibabaCloudOSS)
|
||||
import AlibabaCloudOSS
|
||||
#endif
|
||||
|
||||
/// 将用户选择的视频上传至 OSS,返回可提交给张工成片接口的原始地址。
|
||||
protocol DroneDockFilmUploading: Sendable {
|
||||
/// 上传以文件为单位执行,进度在主线程回传;100 表示 OSS 已成功接收。
|
||||
@MainActor
|
||||
func upload(video: DroneDockDeliveryVideo, scenicID: Int,
|
||||
onProgress: @escaping @MainActor @Sendable (Int) -> Void) async throws -> String
|
||||
}
|
||||
|
||||
/// OSS 文件传输边界,生产使用 SDK,单元测试不访问外部存储。
|
||||
protocol DroneDockFilmTransporting: Sendable {
|
||||
/// 使用服务端临时配置和文件流上传,不把整个视频载入内存。
|
||||
@MainActor
|
||||
func put(fileURL: URL, objectKey: String, contentType: String, configuration: AliyunOSSResponse,
|
||||
onProgress: @escaping @MainActor @Sendable (Int) -> Void) async throws
|
||||
}
|
||||
|
||||
/// 云端成片上传器,复用 STS 服务与通用对象键规则,按配置返回原始 OSS 地址。
|
||||
@MainActor
|
||||
final class DroneDockFilmUploader: DroneDockFilmUploading {
|
||||
private let configService: any OSSConfigServing
|
||||
private let transport: any DroneDockFilmTransporting
|
||||
|
||||
/// 注入上传配置及传输实现;正式页面默认使用阿里云 SDK。
|
||||
init(configService: any OSSConfigServing, transport: (any DroneDockFilmTransporting)? = nil) {
|
||||
self.configService = configService
|
||||
self.transport = transport ?? DroneDockOSSFilmTransport()
|
||||
}
|
||||
|
||||
/// 文件与临时凭证都有效时才开始上传,取消或失败不会返回成功地址。
|
||||
func upload(video: DroneDockDeliveryVideo, scenicID: Int,
|
||||
onProgress: @escaping @MainActor @Sendable (Int) -> Void) async throws -> String {
|
||||
try Task.checkCancellation()
|
||||
guard scenicID > 0 else { throw DroneDockFilmError.invalidScope }
|
||||
let contentType = try DroneDockFilmPolicy.validate(video: video)
|
||||
let progress = DroneDockFilmProgress(onProgress: onProgress)
|
||||
defer { progress.close() }
|
||||
progress.report(1)
|
||||
let configuration = try await configService.aliyunOSSBucket(bucket: "default")
|
||||
try Task.checkCancellation()
|
||||
try validate(configuration: configuration)
|
||||
_ = try DroneDockFilmPolicy.validate(video: video)
|
||||
let objectKey = OSSUploadPolicy.objectKey(fileName: video.fileName, scenicId: scenicID, moduleType: "task_upload")
|
||||
var allowed = CharacterSet.urlPathAllowed
|
||||
allowed.remove(charactersIn: "?#%")
|
||||
guard let escapedKey = objectKey.addingPercentEncoding(withAllowedCharacters: allowed) else {
|
||||
throw DroneDockFilmError.invalidFileName
|
||||
}
|
||||
let fileURL = OSSUploadPolicy.joinURL(baseURL: configuration.baseUrl, objectKey: escapedKey)
|
||||
guard DroneDockFilmPolicy.remoteURL(fileURL) != nil else { throw DroneDockFilmError.invalidConfiguration }
|
||||
progress.report(10)
|
||||
try await transport.put(fileURL: video.url, objectKey: objectKey, contentType: contentType,
|
||||
configuration: configuration) { value in
|
||||
progress.report(10 + min(100, max(0, value)) * 89 / 100)
|
||||
}
|
||||
try Task.checkCancellation()
|
||||
progress.finish()
|
||||
return fileURL
|
||||
}
|
||||
|
||||
private func validate(configuration: AliyunOSSResponse) throws {
|
||||
guard let base = URLComponents(string: configuration.baseUrl),
|
||||
["https", "http"].contains(base.scheme?.lowercased() ?? ""),
|
||||
let host = base.host, !host.isEmpty,
|
||||
base.user == nil, base.password == nil, base.query == nil, base.fragment == nil,
|
||||
configuration.baseUrl.rangeOfCharacter(from: .whitespacesAndNewlines) == nil,
|
||||
!configuration.endpoint.isEmpty, !configuration.region.isEmpty, !configuration.bucket.isEmpty,
|
||||
configuration.expireSeconds > 0,
|
||||
!configuration.credentials.accessKeyId.isEmpty,
|
||||
!configuration.credentials.accessKeySecret.isEmpty,
|
||||
!configuration.credentials.securityToken.isEmpty else { throw DroneDockFilmError.invalidConfiguration }
|
||||
}
|
||||
}
|
||||
|
||||
/// 阿里云 SDK 的文件上传实现,与既有上传服务使用相同的临时凭证和 endpoint。
|
||||
@MainActor
|
||||
struct DroneDockOSSFilmTransport: DroneDockFilmTransporting {
|
||||
/// SDK 的 .file body 走 URLSession 文件上传,进度从网络回调切回主线程。
|
||||
func put(fileURL: URL, objectKey: String, contentType: String, configuration: AliyunOSSResponse,
|
||||
onProgress: @escaping @MainActor @Sendable (Int) -> Void) async throws {
|
||||
try Task.checkCancellation()
|
||||
#if canImport(AlibabaCloudOSS)
|
||||
let credentials = StaticCredentialsProvider(accessKeyId: configuration.credentials.accessKeyId,
|
||||
accessKeySecret: configuration.credentials.accessKeySecret, securityToken: configuration.credentials.securityToken)
|
||||
let sdkConfiguration = Configuration.default().withRegion(configuration.region).withCredentialsProvider(credentials)
|
||||
sdkConfiguration.withEndpoint(configuration.endpoint)
|
||||
let client = Client(sdkConfiguration)
|
||||
_ = try await client.putObject(PutObjectRequest(
|
||||
bucket: configuration.bucket, key: objectKey, contentType: contentType, body: .file(fileURL),
|
||||
progress: ProgressClosure { _, transferred, expected in
|
||||
guard expected > 0 else { return }
|
||||
let value = Int(min(100, max(0, Double(transferred) / Double(expected) * 100)))
|
||||
Task { @MainActor in onProgress(value) }
|
||||
}
|
||||
))
|
||||
try Task.checkCancellation()
|
||||
#else
|
||||
throw OSSUploadError.sdkUnavailable
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// 丢弃已结束上传的迟到回调,并防止进度倒退或上传失败提前显示 100%。
|
||||
@MainActor
|
||||
private final class DroneDockFilmProgress {
|
||||
private var latest = -1
|
||||
private var isClosed = false
|
||||
private let onProgress: @MainActor @Sendable (Int) -> Void
|
||||
|
||||
init(onProgress: @escaping @MainActor @Sendable (Int) -> Void) { self.onProgress = onProgress }
|
||||
|
||||
func report(_ value: Int) {
|
||||
guard !isClosed, value > latest else { return }
|
||||
latest = value
|
||||
onProgress(value)
|
||||
}
|
||||
|
||||
func finish() { report(100); close() }
|
||||
func close() { isClosed = true }
|
||||
}
|
||||
Reference in New Issue
Block a user