开放 Release 真实操作并保留 Debug 拦截;恢复前台 shooting 串行轮询;统一打印机巢请求与完整原值响应;优化长点位名称、未拍摄文案、报备失败及已结束任务提示,移除成功回执 UI。补充相关测试、离线策略检查和联调说明。
147 lines
7.6 KiB
Swift
147 lines
7.6 KiB
Swift
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)
|
|
// 对象名使用 ASCII,避免地址转义差异;原始文件名仍由成片保存接口记录。
|
|
let fileExtension = URL(fileURLWithPath: video.fileName).pathExtension.lowercased()
|
|
let objectKey = OSSUploadPolicy.objectKey(fileName: "film.\(fileExtension)", 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)
|
|
let trace = DroneDockFileTransferTrace(operation: "file_upload", method: "PUT",
|
|
remoteURL: URL(string: configuration.endpoint))
|
|
let fileSize = (try? fileURL.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
|
|
DroneDockDiagnostics.write(trace.message("request.started", fields: [
|
|
"bucket": configuration.bucket, "object_key": objectKey,
|
|
"bytes": fileSize, "content_type": contentType, "body": "<binary omitted>"
|
|
]))
|
|
do {
|
|
let result = 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) }
|
|
}
|
|
))
|
|
DroneDockDiagnostics.write(trace.message("response.received", fields: [
|
|
"http_status": result.statusCode, "oss_request_id": result.requestId,
|
|
"response_headers": result.headers ?? [:],
|
|
"body": DroneDockDiagnostics.payload(result.callbackResult)
|
|
]))
|
|
} catch {
|
|
let cancelled = error is CancellationError || (error as? URLError)?.code == .cancelled
|
|
DroneDockDiagnostics.write(trace.message(cancelled ? "request.cancelled" : "request.failed",
|
|
fields: DroneDockDiagnostics.errorFields(error)))
|
|
throw error
|
|
}
|
|
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 }
|
|
}
|