From 0e3dbc41258c84c3f2c8771e1ab082de4508301f Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Mon, 3 Apr 2023 11:49:43 +0200 Subject: [PATCH] Fix VFS media (delete file after exportPlainFile) --- .../ChatConversationViewModel.swift | 13 +++ .../ChatConversationTableViewSwift.swift | 64 ++++++++++-- .../Views/ChatConversationViewSwift.swift | 16 ++- .../Chat/Views/MultilineMessageCell.swift | 97 +++++++++++++++---- 4 files changed, 160 insertions(+), 30 deletions(-) diff --git a/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift b/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift index 12eb514c0..338f53d1b 100644 --- a/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift +++ b/Classes/Swift/Chat/ViewModels/ChatConversationViewModel.swift @@ -555,4 +555,17 @@ class ChatConversationViewModel: ControlsViewModel { func stopSharedPlayer() { AudioPlayer.stopSharedPlayer() } + + func removeTmpFile(filePath: String?){ + if (filePath != nil) { + if (filePath != "") { + do { + print("[vfs] remove item at \(filePath)") + try FileManager.default.removeItem(atPath: filePath!) + }catch{ + print("[vfs] remove item error") + } + } + } + } } diff --git a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift index 5be0acc0e..4a89bbff6 100644 --- a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift @@ -470,8 +470,7 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour func previewControllerDidDismiss(_ controller: QLPreviewController) { if afterPreviewIndex > -1 { - collectionView.scrollToItem(at: IndexPath(row: afterPreviewIndex, section: 0), at: .centeredVertically, animated: false) - + //collectionView.scrollToItem(at: IndexPath(row: afterPreviewIndex, section: 0), at: .centeredVertically, animated: false) } } @@ -481,15 +480,39 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour if (state.rawValue == LinphoneChatMessageStateNotDelivered.rawValue) { print("Messsage not delivered") } else { - if ((VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) || ConfigManager.instance().lpConfigBoolForKey(key: "use_in_app_file_viewer_for_non_encrypted_files", section: "app")) && chatMessage.contents.first?.type == "image"){ + if (VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) || ConfigManager.instance().lpConfigBoolForKey(key: "use_in_app_file_viewer_for_non_encrypted_files", section: "app")){ let view: ImageView = VIEW(ImageView.compositeViewDescription()) - let image = UIImage(contentsOfFile: chatMessage.contents.first!.filePath) + + var image = UIImage() + if chatMessage.contents.first!.type == "image" { + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = chatMessage.contents.first!.exportPlainFile() + + image = UIImage(contentsOfFile: plainFile)! + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + + }else { + image = UIImage(contentsOfFile: chatMessage.contents.first!.filePath)! + } + } PhoneMainView.instance().changeCurrentView(view.compositeViewDescription()) view.image = image } else { let previewController = QLPreviewController() self.previewItems = [] - self.previewItems.append(self.getPreviewItem(filePath: (chatMessage.contents.first?.filePath)!)) + + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = chatMessage.contents.first?.exportPlainFile() + + self.previewItems.append(self.getPreviewItem(filePath: plainFile!)) + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + }else { + self.previewItems.append(self.getPreviewItem(filePath: (chatMessage.contents.first?.filePath)!)) + } afterPreviewIndex = index @@ -508,16 +531,41 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour if (state.rawValue == LinphoneChatMessageStateNotDelivered.rawValue) { print("Messsage not delivered") } else { - if ((VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) || ConfigManager.instance().lpConfigBoolForKey(key: "use_in_app_file_viewer_for_non_encrypted_files", section: "app")) && chatMessage!.contents[index].type == "image"){ + if (VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) || ConfigManager.instance().lpConfigBoolForKey(key: "use_in_app_file_viewer_for_non_encrypted_files", section: "app")){ let view: ImageView = VIEW(ImageView.compositeViewDescription()) - let image = UIImage(contentsOfFile: chatMessage!.contents[index].filePath) + + var image = UIImage() + if chatMessage!.contents[index].type == "image" { + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = chatMessage!.contents[index].exportPlainFile() + + image = UIImage(contentsOfFile: plainFile)! + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + + }else { + image = UIImage(contentsOfFile: chatMessage!.contents[index].filePath)! + } + } + PhoneMainView.instance().changeCurrentView(view.compositeViewDescription()) view.image = image } else { let previewController = QLPreviewController() self.previewItems = [] chatMessage?.contents.forEach({ content in - self.previewItems.append(self.getPreviewItem(filePath: (content.filePath))) + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + + self.previewItems.append(self.getPreviewItem(filePath: plainFile)) + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + + }else { + self.previewItems.append(self.getPreviewItem(filePath: (content.filePath))) + } }) afterPreviewIndex = indexMessage diff --git a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift index b2d307ca9..e3a809c37 100644 --- a/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationViewSwift.swift @@ -829,8 +829,20 @@ class ChatConversationViewSwift: BackActionsNavigationView, PHPickerViewControll if(content.isFile){ let indexPath = IndexPath(row: ChatConversationViewModel.sharedModel.replyCollectionView.count, section: 0) - ChatConversationViewModel.sharedModel.replyURLCollection.append(URL(string: content.filePath.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)!) - ChatConversationViewModel.sharedModel.replyCollectionView.append(ChatConversationViewModel.sharedModel.getImageFrom(content.getCobject, filePath: content.filePath, forReplyBubble: true)!) + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + + ChatConversationViewModel.sharedModel.replyURLCollection.append(URL(string: plainFile.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!) + ChatConversationViewModel.sharedModel.replyCollectionView.append(ChatConversationViewModel.sharedModel.getImageFrom(content.getCobject, filePath: plainFile, forReplyBubble: true)!) + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + + }else{ + ChatConversationViewModel.sharedModel.replyURLCollection.append(URL(string: content.filePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!) + ChatConversationViewModel.sharedModel.replyCollectionView.append(ChatConversationViewModel.sharedModel.getImageFrom(content.getCobject, filePath: content.filePath, forReplyBubble: true)!) + } + collectionViewReply.insertItems(at: [indexPath]) }else if(content.isText){ replyContentTextSpacing.isHidden = false diff --git a/Classes/Swift/Chat/Views/MultilineMessageCell.swift b/Classes/Swift/Chat/Views/MultilineMessageCell.swift index 20bfc9402..46cbbf032 100644 --- a/Classes/Swift/Chat/Views/MultilineMessageCell.swift +++ b/Classes/Swift/Chat/Views/MultilineMessageCell.swift @@ -770,6 +770,7 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI meetingView.isHidden = true event.chatMessage!.contents.forEach { content in + if content.isFileTransfer { let indexPath = IndexPath(row: imagesGridCollectionView.count, section: 0) @@ -810,8 +811,18 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI imageViewBubble.isHidden = true }else{ - if let imageMessage = UIImage(named: content.filePath){ - imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + if let imageMessage = UIImage(named: plainFile){ + imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + } + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + }else{ + if let imageMessage = UIImage(named: content.filePath){ + imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + } } if(content.isFile){ @@ -836,8 +847,18 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI imageVideoViewBubble.isHidden = true }else{ - if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: content.filePath){ - imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: plainFile){ + imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + } + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + }else{ + if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: content.filePath){ + imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + } } if(content.isFile){ @@ -976,7 +997,7 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI } func createThumbnailOfVideoFromFileURL(videoURL: String) -> UIImage? { - let asset = AVAsset(url: URL(string: "file://" + videoURL)!) + let asset = AVAsset(url: URL(string: "file://" + videoURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true do { @@ -1209,19 +1230,28 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI } func getImageFrom(_ content: Content?, forReplyBubble: Bool) -> UIImage? { - var filePath = content?.filePath + var filePath = "" + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + filePath = content!.exportPlainFile() + }else { + filePath = content!.filePath + } let type = content?.type let name = content?.name - if filePath == nil { + if filePath == "" { filePath = LinphoneManager.validFilePath(name) } var image: UIImage? = nil if type == "video" { - image = createThumbnailOfVideoFromFileURL(videoURL: filePath!) + image = createThumbnailOfVideoFromFileURL(videoURL: filePath) } else if type == "image" { - image = UIImage(named: content!.filePath) + image = UIImage(named: filePath) } + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: filePath) + filePath = "" + if let image { return image } else { @@ -1363,17 +1393,44 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI if (imagesGridCollectionView.count <= 1){ if content.type == "video" { - if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: content.filePath){ - imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) - if (imageVideoViewBubble.image != nil && imagesGridCollectionView.count <= 1){ - ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() - } - } - } - if let imageMessage = UIImage(named: content.filePath){ - imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) - if (imageViewBubble.image != nil && imagesGridCollectionView.count <= 1){ - ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: plainFile){ + imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if (imageVideoViewBubble.image != nil && imagesGridCollectionView.count <= 1){ + ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() + } + } + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + }else{ + if let imageMessage = createThumbnailOfVideoFromFileURL(videoURL: content.filePath){ + imageVideoViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if (imageVideoViewBubble.image != nil && imagesGridCollectionView.count <= 1){ + ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() + } + } + } + } else if content.type == "image" { + if VFSUtil.vfsEnabled(groupName: kLinphoneMsgNotificationAppGroupId) { + var plainFile = content.exportPlainFile() + if let imageMessage = UIImage(named: plainFile){ + imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if (imageViewBubble.image != nil && imagesGridCollectionView.count <= 1){ + ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() + } + } + + ChatConversationViewModel.sharedModel.removeTmpFile(filePath: plainFile) + plainFile = "" + }else{ + if let imageMessage = UIImage(named: content.filePath){ + imageViewBubble.image = resizeImage(image: imageMessage, targetSize: CGSizeMake(UIScreen.main.bounds.size.width*3/4, 300.0)) + if (imageViewBubble.image != nil && imagesGridCollectionView.count <= 1){ + ChatConversationTableViewModel.sharedModel.reloadCollectionViewCell() + } + } } } else { collectionViewImagesGrid.reloadItems(at: [IndexPath(row: indexTransferProgress, section: 0)])