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

79 lines
2.3 KiB
Swift
Raw 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
}
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
)
)
}
}
/// OTG
enum TravelAlbumOTGUploadError: LocalizedError, Equatable {
case invalidAlbum
case localFileMissing
var errorDescription: String? {
switch self {
case .invalidAlbum:
return "请先选择有效的相册"
case .localFileMissing:
return "本地文件不存在,无法上传"
}
}
}