Files
e-party-iOS/yana/Utils/TCCos/COSManagerAdapter.swift
edwinQQQ b966e24532 feat: 更新COSManager和相关视图以增强图片上传功能
- 修改COSManagerAdapter以支持新的TCCos组件,确保与腾讯云COS的兼容性。
- 在CreateFeedFeature中新增图片上传相关状态和Action,优化图片选择与上传逻辑。
- 更新CreateFeedView以整合图片上传功能,提升用户体验。
- 在多个视图中添加键盘状态管理,改善用户交互体验。
- 新增COS相关的测试文件,确保功能的正确性和稳定性。
2025-07-31 11:41:56 +08:00

196 lines
7.1 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.

//
// COSManagerAdapter.swift
// yana
//
// Created by P on 2025/7/31.
//
import Foundation
import UIKit
import ComposableArchitecture
// MARK: - COSManager
/// COSManager
///
/// COSManager 使 TCCos
/// COSManager
@MainActor
class COSManagerAdapter: ObservableObject {
static let shared = COSManagerAdapter()
private init() {
// 使 TCCos
self.tokenService = COSTokenService(apiService: LiveAPIService())
self.uploadService = COSUploadService(
tokenService: self.tokenService,
configurationService: COSConfigurationService()
)
self.configurationService = COSConfigurationService()
debugInfoSync("<EFBFBD><EFBFBD> COSManagerAdapter 已初始化,使用 TCCos 组件")
}
// MARK: - TCCos
private let tokenService: COSTokenServiceProtocol
private let uploadService: COSUploadServiceProtocol
private let configurationService: COSConfigurationServiceProtocol
// MARK: - COSManager
/// COS Token
/// - Parameter apiService: API
/// - Returns: Token nil
func getToken(apiService: any APIServiceProtocol & Sendable) async -> TcTokenData? {
do {
debugInfoSync("🔐 开始请求腾讯云 COS Token...")
let tokenData = try await tokenService.getValidToken()
debugInfoSync("✅ COS Token 获取成功")
debugInfoSync(" - 存储桶: \(tokenData.bucket)")
debugInfoSync(" - 地域: \(tokenData.region)")
debugInfoSync(" - 过期时间: \(tokenData.expirationDate)")
debugInfoSync(" - 剩余时间: \(tokenData.remainingTime)")
return tokenData
} catch {
debugErrorSync("❌ COS Token 获取失败: \(error.localizedDescription)")
return nil
}
}
/// Token
func refreshToken(apiService: any APIServiceProtocol & Sendable) async -> TcTokenData? {
do {
debugInfoSync("🔄 开始刷新腾讯云 COS Token...")
let tokenData = try await tokenService.refreshToken()
debugInfoSync("✅ COS Token 刷新成功")
debugInfoSync(" - 存储桶: \(tokenData.bucket)")
debugInfoSync(" - 地域: \(tokenData.region)")
debugInfoSync(" - 过期时间: \(tokenData.expirationDate)")
return tokenData
} catch {
debugErrorSync("❌ COS Token 刷新失败: \(error.localizedDescription)")
return nil
}
}
/// COS
/// - Parameters:
/// - imageData:
/// - apiService: API
/// - Returns: nil
func uploadImage(_ imageData: Data, apiService: any APIServiceProtocol & Sendable) async -> String? {
//
let fileExtension = "jpg"
let fileName = "images/\(UUID().uuidString).\(fileExtension)"
do {
debugInfoSync("🚀 开始上传图片,数据大小: \(imageData.count) bytes")
let url = try await uploadService.uploadImage(imageData, fileName: fileName)
debugInfoSync("✅ 图片上传成功: \(url)")
return url
} catch {
debugErrorSync("❌ 图片上传失败: \(error.localizedDescription)")
return nil
}
}
/// UIImage COS JPEG(0.7)
/// - Parameters:
/// - image: UIImage
/// - apiService: API
/// - Returns: nil
func uploadUIImage(_ image: UIImage, apiService: any APIServiceProtocol & Sendable) async -> String? {
//
let fileExtension = "jpg"
let fileName = "images/\(UUID().uuidString).\(fileExtension)"
do {
debugInfoSync("<EFBFBD><EFBFBD> 开始上传 UIImage自动压缩为 JPEG(0.7)")
let url = try await uploadService.uploadUIImage(image, fileName: fileName)
debugInfoSync("✅ UIImage 上传成功: \(url)")
return url
} catch {
debugErrorSync("❌ UIImage 上传失败: \(error.localizedDescription)")
return nil
}
}
// MARK: - COSManager
/// 访 Token
var token: TcTokenData? {
get async {
do {
return try await tokenService.getValidToken()
} catch {
debugErrorSync("❌ 获取 Token 失败: \(error.localizedDescription)")
return nil
}
}
}
// MARK: - COSManager
/// Token
func getTokenStatus() async -> String {
return await tokenService.getTokenStatus()
}
/// Token
func testTokenRetrieval(apiService: any APIServiceProtocol & Sendable) async {
#if DEBUG
debugInfoSync("\n<EFBFBD><EFBFBD> 开始测试腾讯云 COS Token 获取功能")
let token = await getToken(apiService: apiService)
if let tokenData = token {
debugInfoSync("✅ Token 获取成功")
debugInfoSync(" bucket: \(tokenData.bucket)")
debugInfoSync(" Expiration: \(tokenData.expireTime)")
debugInfoSync(" Token: \(tokenData.sessionToken.prefix(20))...")
debugInfoSync(" SecretId: \(tokenData.secretId.prefix(20))...")
} else {
debugInfoSync("❌ Token 获取失败")
}
let status = await getTokenStatus()
debugInfoSync("📊 Token 状态: \(status)")
debugInfoSync("✅ 腾讯云 COS Token 测试完成\n")
#endif
}
// MARK: - COSManager
/// Token
private func clearCachedToken() {
tokenService.clearCachedToken()
debugInfoSync("🗑️ 清除缓存的 COS Token")
}
}
// MARK: -
extension COSManagerAdapter {
/// 使
static func createWithDependencies(
tokenService: COSTokenServiceProtocol,
uploadService: COSUploadServiceProtocol,
configurationService: COSConfigurationServiceProtocol
) -> COSManagerAdapter {
let adapter = COSManagerAdapter()
// 使
// 使 shared
return adapter
}
}
// MARK: -
/// COSManager COSManagerAdapter
/// 使
typealias COSManager = COSManagerAdapter