From 7c08c38829ae5ecf78079125a16898f003cc0c86 Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Thu, 7 Oct 2021 15:10:08 +0200 Subject: [PATCH] get image from either download dir or cache dir --- Classes/ChatConversationView.m | 10 +++++----- Classes/LinphoneManager.h | 1 + Classes/LinphoneManager.m | 18 ++++++++++++++++++ Classes/LinphoneUI/UIChatBubblePhotoCell.m | 12 ++++++------ Classes/LinphoneUI/UIChatBubbleTextCell.m | 8 ++++---- Classes/LinphoneUI/UIChatContentView.m | 2 +- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index a365d68e6..fd4e7571d 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -901,7 +901,7 @@ static UICompositeViewDescription *compositeDescription = nil; } + (void)writeMediaToGallery:(NSString *)name fileType:(NSString *)fileType { - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + NSString *filePath = [LinphoneManager getValidFile:name]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:filePath]) { NSData* data = [NSData dataWithContentsOfFile:filePath]; @@ -1325,17 +1325,17 @@ void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog } + (NSData *)getCacheFileData: (NSString *)name { - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + NSString *filePath = [LinphoneManager getValidFile:name]; return [NSData dataWithContentsOfFile:filePath]; } + (NSURL *)getCacheFileUrl: (NSString *)name { - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + NSString *filePath = [LinphoneManager getValidFile:name]; return [NSURL fileURLWithPath:filePath]; } + (void)writeFileInCache:(NSData *)data name:(NSString *)name { - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + NSString *filePath =[LinphoneManager getValidFile:name]; if (name || [name isEqualToString:@""]) { LOGW(@"try to write file in %@", filePath); } @@ -1383,7 +1383,7 @@ void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog return [data writeToURL:fileURL atomically:TRUE]; } else { // get the url of localfile - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; + NSString *filePath = [LinphoneManager getValidFile:fileName]; NSURL *localURL = nil; if (fileName || [fileName isEqualToString:@""]) { LOGW(@"[writeFileInICloud] try to write file in %@", filePath); diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index 12ff03ce1..632675f92 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -135,6 +135,7 @@ typedef struct _LinphoneManagerSounds { + (NSString *)documentFile:(NSString *)file; + (NSString*)dataFile:(NSString*)file; + (NSString*)cacheDirectory; ++ (NSString *)getValidFile:(NSString *)name; // migration + (NSString *)oldPreferenceFile:(NSString *)file; + (NSString *)oldDataFile:(NSString *)file; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 50ba532fa..2c86c0b62 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -1885,6 +1885,24 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { return fullPath; } ++ (NSString *)getValidFile:(NSString *)name { + // At present, downlaod_dir is .../Library/Images, but during sometimes download_dir was .../Library/Caches + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { + return filePath; + } + + NSString *objcGroupdId = [NSString stringWithCString:kLinphoneMsgNotificationAppGroupId.UTF8String encoding:[NSString defaultCStringEncoding]]; + NSURL *basePath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:objcGroupdId]; + NSString * fullPath = [[basePath path] stringByAppendingString:@"/Library/Caches/"]; + fullPath = [fullPath stringByAppendingPathComponent:name]; + if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) { + [[NSFileManager defaultManager] copyItemAtPath:fullPath toPath:filePath error:nil]; + } + + return fullPath; +} + + (NSString *)oldPreferenceFile:(NSString *)file { // migration LinphoneFactory *factory = linphone_factory_get(); diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index d2f99565f..0250dc004 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -266,7 +266,7 @@ ms_free(cPath); [LinphoneManager setValueInMessageAppData:filePath forKey:@"encryptedfile" inMessage:self.message]; } else { - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; + filePath = [LinphoneManager getValidFile:fileName]; } } if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { @@ -435,7 +435,7 @@ if (!filePath) { NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:self.message]; NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:(localVideo?:localFile)]; + filePath = [LinphoneManager getValidFile:(localVideo?:localFile)]; } if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { @@ -492,7 +492,7 @@ return; } NSString *name = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; - if([[NSFileManager defaultManager] fileExistsAtPath:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]]) { + if([[NSFileManager defaultManager] fileExistsAtPath:[LinphoneManager getValidFile:name]]) { [view openFileWithURL:[ChatConversationView getCacheFileUrl:name]]; } else { [view openFileWithURL:[view getICloudFileUrl:name]]; @@ -540,16 +540,16 @@ NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:self.message]; NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; NSString *imageName = NULL; - if (localImage && [[NSFileManager defaultManager] fileExistsAtPath:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:localImage]]) { + if (localImage && [[NSFileManager defaultManager] fileExistsAtPath:[LinphoneManager getValidFile:localImage]]) { imageName = localImage; - } else if (localFile && [[NSFileManager defaultManager] fileExistsAtPath:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:localFile]]) { + } else if (localFile && [[NSFileManager defaultManager] fileExistsAtPath:[LinphoneManager getValidFile:localFile]]) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"] || [localFile hasSuffix:@"jpg"] || [localFile hasSuffix:@"png"]) { imageName = localFile; } } if (imageName) { - NSData *data = [NSData dataWithContentsOfFile:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:imageName]]; + NSData *data = [NSData dataWithContentsOfFile:[LinphoneManager getValidFile:imageName]]; UIImage *image = [[UIImage alloc] initWithData:data]; if (image) [view setImage:image]; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 2dd4c2688..31544aa04 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -327,7 +327,7 @@ NSString *name = [NSString stringWithUTF8String:linphone_content_get_name(content)]; NSString *filePath = [encrptedFilePaths valueForKey:name]; if (filePath == NULL) { - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + filePath = [LinphoneManager getValidFile:name]; } [newfileContext addObject:[NSData dataWithContentsOfFile:filePath] name:name type:[NSString stringWithUTF8String:linphone_content_get_type(content)]]; } @@ -441,7 +441,7 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 44; NSString *type = [NSString stringWithUTF8String:linphone_content_get_type(content)]; NSString *name = [NSString stringWithUTF8String:linphone_content_get_name(content)]; if (!filePath) { - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + filePath = [LinphoneManager getValidFile:name]; } UIImage *image = nil; @@ -502,7 +502,7 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 44; NSString *name = [NSString stringWithUTF8String:linphone_content_get_name(content)]; NSString *filePath=[encrptedFilePaths valueForKey:name]; if (filePath == NULL) { - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + filePath = [LinphoneManager getValidFile:name]; } image = [UIChatBubbleTextCell getImageFromContent:content filePath:filePath]; @@ -562,7 +562,7 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 44; CGSize originalImageSize = CGSizeMake(230, 50); if (!filePath) { - filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; + filePath = [LinphoneManager getValidFile:fileName]; } if (localFile) { UIImage *image = nil; diff --git a/Classes/LinphoneUI/UIChatContentView.m b/Classes/LinphoneUI/UIChatContentView.m index fd24709cf..a5e621b62 100644 --- a/Classes/LinphoneUI/UIChatContentView.m +++ b/Classes/LinphoneUI/UIChatContentView.m @@ -48,7 +48,7 @@ } else { if (_filePath == NULL) { NSString *name = [NSString stringWithUTF8String:linphone_content_get_name(content)]; - _filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; + _filePath = [LinphoneManager getValidFile:name]; } UIImage *image = [UIChatBubbleTextCell getImageFromContent:content filePath:_filePath]; [self setImage:image];