feat(drone-dock): 接入成片云端交付并精简准备页定位展示

This commit is contained in:
2026-10-29 10:24:45 +08:00
parent 88fa1f9b77
commit 4d763ef5d1
29 changed files with 2870 additions and 100 deletions
@@ -0,0 +1,92 @@
import AVFoundation
import Foundation
import PhotosUI
import UniformTypeIdentifiers
/// 将用户选择的视频复制到本页独占的临时目录,避免相册临时文件提前失效。
enum DroneDockDeliveryVideoLoader {
/// 相册回调返回前完成文件复制,随后验证本地视频轨道。
@MainActor
static func load(provider: NSItemProvider) async throws -> DroneDockDeliveryVideo {
let suggestedName = provider.suggestedName
let copiedURL: URL = try await withCheckedThrowingContinuation { continuation in
provider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { source, error in
if let error { continuation.resume(throwing: error); return }
guard let source else { continuation.resume(throwing: VideoError.unavailable); return }
do { continuation.resume(returning: try copy(source: source, suggestedName: suggestedName)) }
catch { continuation.resume(throwing: error) }
}
}
return try await validate(copiedURL)
}
/// 文件选择器的安全作用域仅用于复制,上传不持有外部文件访问权限。
@MainActor
static func load(documentURL: URL) async throws -> DroneDockDeliveryVideo {
let accessing = documentURL.startAccessingSecurityScopedResource()
defer { if accessing { documentURL.stopAccessingSecurityScopedResource() } }
let copiedURL = try await Task.detached(priority: .userInitiated) {
try copy(source: documentURL, suggestedName: documentURL.lastPathComponent)
}.value
return try await validate(copiedURL)
}
/// 只清理本功能创建的独占目录,外部文件和测试文件不受影响。
static func discard(_ url: URL) {
let directory = url.deletingLastPathComponent().standardizedFileURL
guard directory.deletingLastPathComponent() == root.standardizedFileURL else { return }
try? FileManager.default.removeItem(at: directory)
}
private static var root: URL { FileManager.default.temporaryDirectory.appendingPathComponent("drone-dock-delivery", isDirectory: true) }
private static func copy(source: URL, suggestedName: String?) throws -> URL {
guard source.isFileURL else { throw VideoError.unavailable }
let sourceExtension = source.pathExtension.lowercased()
let candidate = (suggestedName ?? source.lastPathComponent).trimmingCharacters(in: .whitespacesAndNewlines)
var name = URL(fileURLWithPath: candidate.isEmpty ? "成片视频" : candidate).lastPathComponent
if URL(fileURLWithPath: name).pathExtension.isEmpty, !sourceExtension.isEmpty { name += "." + sourceExtension }
guard name.count <= 255 else { throw VideoError.longName }
guard ["mp4", "mov", "avi", "mkv", "flv", "wmv", "webm"].contains(URL(fileURLWithPath: name).pathExtension.lowercased()) else {
throw VideoError.unsupported
}
let values = try source.resourceValues(forKeys: [.fileSizeKey, .isRegularFileKey])
guard values.isRegularFile == true, let size = values.fileSize, size > 0 else { throw VideoError.unavailable }
guard size <= 2_048 * 1_024 * 1_024 else { throw VideoError.tooLarge }
let directory = root.appendingPathComponent(UUID().uuidString, isDirectory: true)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
let destination = directory.appendingPathComponent(name)
do { try FileManager.default.copyItem(at: source, to: destination); return destination }
catch { try? FileManager.default.removeItem(at: directory); throw error }
}
@MainActor
private static func validate(_ url: URL) async throws -> DroneDockDeliveryVideo {
do {
try Task.checkCancellation()
let asset = AVURLAsset(url: url)
let tracks = try await asset.loadTracks(withMediaType: .video)
guard !tracks.isEmpty else { throw VideoError.unsupported }
let values = try url.resourceValues(forKeys: [.fileSizeKey])
guard let size = values.fileSize, size > 0 else { throw VideoError.unavailable }
try Task.checkCancellation()
return DroneDockDeliveryVideo(url: url, fileName: url.lastPathComponent, fileSize: size)
} catch {
discard(url)
throw error
}
}
/// 文件准备失败时保留可操作的提示,不把无效文件交给上传队列。
private enum VideoError: LocalizedError {
case unavailable, unsupported, tooLarge, longName
var errorDescription: String? {
switch self {
case .unavailable: "无法读取视频文件,请重新选择"
case .unsupported: "请选择 MP4、MOV 等受支持的视频;M4V 请先导出为 MP4"
case .tooLarge: "视频不能超过 2 GB,请压缩后重试"
case .longName: "视频文件名过长,请缩短后重新选择"
}
}
}
}