diff --git a/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate b/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate index a6d1fbc..4a408e4 100644 Binary files a/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate and b/wake.xcodeproj/project.xcworkspace/xcuserdata/elliwood.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/wake/View/Components/Upload/ImageUploadService.swift b/wake/View/Components/Upload/ImageUploadService.swift index 0cc1dc6..b2f0ab0 100644 --- a/wake/View/Components/Upload/ImageUploadService.swift +++ b/wake/View/Components/Upload/ImageUploadService.swift @@ -140,36 +140,99 @@ public class ImageUploadService { /// 上传媒体文件(图片或视频) /// - Parameters: /// - media: 媒体类型,可以是图片或视频 - /// - compressionQuality: 缩略图/图片压缩质量 (0.0 到 1.0) - /// - progressHandler: 上传进度回调 (0.0 到 1.0) + /// - progress: 上传进度回调 (0.0 到 1.0) /// - completion: 完成回调,返回上传结果或错误 public func uploadMedia( _ media: MediaType, - compressionQuality: CGFloat = 0.7, - progress progressHandler: @escaping (UploadProgress) -> Void, + progress: @escaping (UploadProgress) -> Void, completion: @escaping (Result) -> Void ) { switch media { case .image(let image): - // 处理图片上传 - uploadCompressedImage( + print("🖼️ 开始处理图片上传") + uploadImage( image, - compressionQuality: compressionQuality, - progress: progressHandler, + progress: { progressInfo in + print("📊 图片上传进度: \(progressInfo.current)%") + progress(progressInfo) + }, completion: { result in - let mediaResult = result.map { MediaUploadResult.file($0) } - completion(mediaResult) + switch result { + case .success(let uploadResult): + print("✅ 图片上传完成, fileId: \(uploadResult.fileId)") + completion(.success(.file(uploadResult))) + case .failure(let error): + print("❌ 图片上传失败: \(error.localizedDescription)") + completion(.failure(error)) + } } ) - case .video(let videoURL, let thumbnail): - // 处理视频上传 - uploadVideoWithThumbnail( - videoURL: videoURL, - existingThumbnail: thumbnail, - compressionQuality: compressionQuality, - progress: progressHandler, - completion: completion + case .video(let videoURL, _): + print("🎥 开始处理视频上传: \(videoURL.lastPathComponent)") + + uploader.uploadVideo( + videoURL, + progress: { uploadProgress in + print("📊 视频上传进度: \(Int(uploadProgress * 100))%") + let progressInfo = UploadProgress( + current: Int(uploadProgress * 100), + total: 100, + progress: uploadProgress, + isOriginal: true + ) + progress(progressInfo) + }, + completion: { result in + switch result { + case .success(let videoResult): + print("✅ 视频文件上传完成, fileId: \(videoResult.fileId)") + print("🖼️ 开始提取视频缩略图...") + + MediaUtils.extractFirstFrame(from: videoURL) { thumbnailResult in + switch thumbnailResult { + case .success(let thumbnailImage): + print("🖼️ 视频缩略图提取成功") + + if let compressedThumbnail = thumbnailImage.resized(to: CGSize(width: 800, height: 800)) { + print("🖼️ 开始上传视频缩略图...") + + self.uploader.uploadImage( + compressedThumbnail, + progress: { _ in }, + completion: { thumbnailResult in + switch thumbnailResult { + case .success(let thumbnailUploadResult): + print("✅ 视频缩略图上传完成, fileId: \(thumbnailUploadResult.fileId)") + let result = MediaUploadResult.video( + video: videoResult, + thumbnail: thumbnailUploadResult + ) + completion(.success(result)) + + case .failure(let error): + print("❌ 视频缩略图上传失败: \(error.localizedDescription)") + completion(.failure(error)) + } + } + ) + } else { + let error = NSError(domain: "ImageUploadService", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to compress thumbnail"]) + print("❌ 视频缩略图压缩失败") + completion(.failure(error)) + } + + case .failure(let error): + print("❌ 视频缩略图提取失败: \(error.localizedDescription)") + completion(.failure(error)) + } + } + + case .failure(let error): + print("❌ 视频文件上传失败: \(error.localizedDescription)") + completion(.failure(error)) + } + } ) } } @@ -319,3 +382,23 @@ public class ImageUploadService { } } } + +// MARK: - UIImage Extension + +private extension UIImage { + func resized(to size: CGSize) -> UIImage? { + let widthRatio = size.width / self.size.width + let heightRatio = size.height / self.size.height + let ratio = min(widthRatio, heightRatio) + + let newSize = CGSize( + width: self.size.width * ratio, + height: self.size.height * ratio + ) + + let renderer = UIGraphicsImageRenderer(size: newSize) + return renderer.image { _ in + self.draw(in: CGRect(origin: .zero, size: newSize)) + } + } +}