diff --git a/ConversationsClassic/AppCore/Actions/FileActions.swift b/ConversationsClassic/AppCore/Actions/FileActions.swift index 2e7e5c4..a8dc48e 100644 --- a/ConversationsClassic/AppCore/Actions/FileActions.swift +++ b/ConversationsClassic/AppCore/Actions/FileActions.swift @@ -8,7 +8,8 @@ enum FileAction: Stateable { case createAttachmentThumbnail(messageId: String, localName: String) case attachmentThumbnailCreated(messageId: String, thumbnailName: String) - case copyFileForUploading(messageId: String, fileData: Data, thumbnailData: Data?) + case copyGalleryItemsForUploading(items: [SharingGalleryItem]) + case galleryItemsCopiedForUploading(newMessageIds: [String], localNames: [String]) case fetchItemsFromGallery case itemsFromGalleryFetched(items: [SharingGalleryItem]) diff --git a/ConversationsClassic/AppCore/Files/FileProcessing.swift b/ConversationsClassic/AppCore/Files/FileProcessing.swift index 48131b4..a624d4d 100644 --- a/ConversationsClassic/AppCore/Files/FileProcessing.swift +++ b/ConversationsClassic/AppCore/Files/FileProcessing.swift @@ -94,6 +94,48 @@ final class FileProcessing { } } } + + // This function also creates new ids for messages for each new attachment + func copyGalleryItemsForUploading(items: [SharingGalleryItem]) -> [(String, String)] { + let assets = syncGalleryEnumerate(items.map { $0.id }) + return assets + .compactMap { asset in + let newMessageId = UUID().uuidString + let fileId = UUID().uuidString + if asset.mediaType == .image { + return syncGalleryProcessImage(asset) { image in + let localName = "\(newMessageId)_\(fileId).jpg" + let localUrl = FileProcessing.fileFolder.appendingPathComponent(localName) + if let data = image.jpegData(compressionQuality: 1.0) { + do { + try data.write(to: localUrl) + return (newMessageId, localName) + } catch { + return nil + } + } else { + return nil + } + } + } else if asset.mediaType == .video { + return syncGalleryProcessVideo(asset) { avAsset in + // swiftlint:disable:next force_cast + let assetURL = avAsset as! AVURLAsset + let url = assetURL.url + let localName = "\(newMessageId)_\(fileId).mov" + let localUrl = FileProcessing.fileFolder.appendingPathComponent(localName) + do { + try FileManager.default.copyItem(at: url, to: localUrl) + return (newMessageId, localName) + } catch { + return nil + } + } + } else { + return nil + } + } + } } private extension FileProcessing { diff --git a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift index 0f771bb..af12982 100644 --- a/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/FileMiddleware.swift @@ -78,12 +78,18 @@ final class FileMiddleware { } .eraseToAnyPublisher() - case .fileAction(.copyFileForUploading(let messageId, let data, let thumbnail)): - print("=====") - print("copyFileForUploading") - print(data.count) - print(thumbnail?.count) - print("=====") + case .fileAction(.copyGalleryItemsForUploading(let items)): + return Future { promise in + let ids = FileProcessing.shared.copyGalleryItemsForUploading(items: items) + promise(.success(.fileAction(.galleryItemsCopiedForUploading(newMessageIds: ids.map { $0.0 }, localNames: ids.map { $0.1 })))) + } + .eraseToAnyPublisher() + + case .fileAction(.galleryItemsCopiedForUploading(let newMessageIds, let localNames)): + print("!!!!!!") + print(newMessageIds) + print(localNames) + print("!!!!!!") return Empty().eraseToAnyPublisher() default: diff --git a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift index e071dbf..070e087 100644 --- a/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift +++ b/ConversationsClassic/AppCore/Middlewares/SharingMiddleware.swift @@ -59,59 +59,9 @@ final class SharingMiddleware { // MARK: - Sharing case .sharingAction(.shareMedia(let ids)): - return Future { promise in - let assets = PHAsset.fetchAssets(withLocalIdentifiers: ids, options: nil) - assets.enumerateObjects { asset, _, _ in - if asset.mediaType == .image { - PHImageManager.default().requestImage( - for: asset, - targetSize: PHImageManagerMaximumSize, - contentMode: .aspectFill, - options: nil - ) { image, _ in - if let data = image?.jpegData(compressionQuality: 1.0) { - DispatchQueue.main.async { - let newMessageId = UUID().uuidString - store.dispatch(.fileAction(.copyFileForUploading( - messageId: newMessageId, - fileData: data, - thumbnailData: store.state.sharingState.galleryItems.first(where: { $0.id == asset.localIdentifier })?.thumbnail - ))) - } - } - } - } else if asset.mediaType == .video { - let options = PHVideoRequestOptions() - options.version = .original - options.deliveryMode = .highQualityFormat - PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, _, _ in - guard let urlAsset = avAsset as? AVURLAsset else { return } - let exporter = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetHighestQuality) - exporter?.outputFileType = .mp4 - let outputURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".mp4") - exporter?.outputURL = outputURL - exporter?.exportAsynchronously { - switch exporter?.status { - case .completed: - if let data = try? Data(contentsOf: outputURL) { - DispatchQueue.main.async { - let newMessageId = UUID().uuidString - store.dispatch(.fileAction(.copyFileForUploading( - messageId: newMessageId, - fileData: data, - thumbnailData: nil - ))) - } - } - - default: - break - } - } - } - } - } - promise(.success(.empty)) + return Future { promise in + let items = state.sharingState.galleryItems.filter { ids.contains($0.id) } + promise(.success(.fileAction(.copyGalleryItemsForUploading(items: items)))) } .eraseToAnyPublisher() diff --git a/ConversationsClassic/View/Screens/Sharing/SharingMediaPickerView.swift b/ConversationsClassic/View/Screens/Sharing/SharingMediaPickerView.swift index 28f9ab7..17cbed0 100644 --- a/ConversationsClassic/View/Screens/Sharing/SharingMediaPickerView.swift +++ b/ConversationsClassic/View/Screens/Sharing/SharingMediaPickerView.swift @@ -200,6 +200,7 @@ private struct GridViewItem: View { .fill(Color.Material.Background.light) .overlay { ProgressView() + .foregroundColor(.Material.Elements.active) } .frame(width: Const.galleryGridSize, height: Const.galleryGridSize) }