This commit is contained in:
fmodf 2024-07-14 18:53:33 +02:00
parent 73c7aa5563
commit cb1f159a7a
5 changed files with 60 additions and 60 deletions

View file

@ -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])

View file

@ -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 {

View file

@ -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:

View file

@ -59,59 +59,9 @@ final class SharingMiddleware {
// MARK: - Sharing
case .sharingAction(.shareMedia(let ids)):
return Future<AppAction, Never> { 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()

View file

@ -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)
}