Files
suixinkan_uikit/suixinkan/Features/TravelAlbum/OTG/Upload/TravelAlbumOTGUploader.swift

86 lines
2.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// TravelAlbumOTGUploader.swift
// suixinkan
//
import Foundation
/// OTG
@MainActor
protocol TravelAlbumOTGUploading {
///
func upload(
record: TravelAlbumOTGPhotoRecord,
scenicId: Int,
progress: @escaping (Int) -> Void
) async throws -> TravelAlbumMaterial
}
/// OTG OSS `upload-material`
@MainActor
final class TravelAlbumOTGUploader: TravelAlbumOTGUploading {
private let uploader: OSSUploadService
private let api: any TravelAlbumServing
/// OTG
init(
uploader: OSSUploadService? = nil,
api: (any TravelAlbumServing)? = nil
) {
self.uploader = uploader ?? NetworkServices.shared.ossUploadService
self.api = api ?? NetworkServices.shared.travelAlbumAPI
}
///
func upload(
record: TravelAlbumOTGPhotoRecord,
scenicId: Int,
progress: @escaping (Int) -> Void
) async throws -> TravelAlbumMaterial {
guard record.albumId > 0 else {
throw TravelAlbumOTGUploadError.invalidAlbum
}
guard !record.localPath.isEmpty,
FileManager.default.fileExists(atPath: record.localPath) else {
throw TravelAlbumOTGUploadError.localFileMissing
}
guard let clientPhotoId = TravelAlbumClientPhotoID.normalize(record.clientPhotoId) else {
throw TravelAlbumOTGUploadError.clientPhotoIdMissing
}
let data = try Data(contentsOf: URL(fileURLWithPath: record.localPath))
let remoteURL = try await uploader.uploadTravelAlbumMaterial(
data: data,
fileName: record.fileName,
scenicId: scenicId,
onProgress: progress
)
return try await api.uploadMaterial(
TravelAlbumUploadMaterialRequest(
userEquityTravelId: record.albumId,
fileName: record.fileName,
fileUrl: remoteURL,
clientPhotoId: clientPhotoId
)
)
}
}
/// OTG
enum TravelAlbumOTGUploadError: LocalizedError, Equatable {
case invalidAlbum
case localFileMissing
case clientPhotoIdMissing
var errorDescription: String? {
switch self {
case .invalidAlbum:
return "请先选择有效的相册"
case .localFileMissing:
return "本地文件不存在,无法上传"
case .clientPhotoIdMissing:
return "照片标识缺失,请重新导入后重试"
}
}
}