Fix VFS media (delete file after exportPlainFile)

This commit is contained in:
Benoit Martins 2023-04-03 11:49:43 +02:00 committed by QuentinArguillere
parent c8e7e4591a
commit 0e3dbc4125
4 changed files with 160 additions and 30 deletions

View file

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

View file

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

View file

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

View file

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