Commit cb948dc43e8edbae4aa4ad0de0754bd3a19c0bfd

Authored by daojian
1 parent 2c3f307e

0.1.19 版本 聊天图片 和 视频 本地地址迁移到 Documents 下

CNLiveBusinessTools.podspec
... ... @@ -8,7 +8,7 @@
8 8  
9 9 Pod::Spec.new do |s|
10 10 s.name = 'CNLiveBusinessTools'
11   - s.version = '0.1.18'
  11 + s.version = '0.1.19'
12 12 s.summary = '业务工具集合组件.'
13 13  
14 14 # This description is used to generate tags and improve search results.
... ...
CNLiveBusinessTools/Classes/CNLiveBusinessTools.h
... ... @@ -71,9 +71,7 @@ typedef NS_ENUM(NSInteger, CNFileType) {
71 71 @param userId 单聊为对方ID,群聊为群聊 ID,图片和视频存放位置
72 72 @return 地址
73 73 */
74   -+ (NSString *)newChatFilePathByUserId:(NSString *)userId;
75   -
76   -+ (nullable NSString *)chatFileWithCachePathByUserId:(NSString *_Nonnull)userId;
  74 ++ (NSString *)newChatFilePathByUserId:(NSString *)userId DEPRECATED_MSG_ATTRIBUTE("please use chatImageAndVideoUploadIMCachePath: instead");
77 75  
78 76 //用户ID
79 77 + (nullable NSString *)chatFilePathByUserId:(NSString *_Nonnull)userId;
... ... @@ -84,6 +82,45 @@ typedef NS_ENUM(NSInteger, CNFileType) {
84 82 */
85 83 + (NSString *)getChatFilesPathByUserId:(NSString *)userId;
86 84  
  85 +/// 获取聊天文件路径,通过消息 msgId 用于区分同一个会话文件名相同,实际文件不是同一个文件的问题
  86 +/// @param userId 会话id
  87 +/// @param msgId 消息id
  88 ++ (NSString *)getChatFilesPathByUserId:(NSString *)userId msgId:(NSString *)msgId;
  89 +
  90 +
  91 +/// 某一个 聊天图片 文件地址
  92 +/// @param conversationId 会话id
  93 +/// @param read 是否是读文件
  94 +/// @param isThumb 是否是缩略图
  95 +/// @param imageElem_path_lastPathComponent 自己发送时的图片上传地址名字 ,只在读文件的时候才需要用到
  96 ++ (NSString *)chatImagePathByConversationId:(NSString *)conversationId imageName:(NSString *)imageName isRead:(BOOL)read isThumb:(BOOL)isThumb imageElem_path_lastPathComponent:(NSString *)imageElem_path_lastPathComponent;
  97 +
  98 +/// 获取本地聊天图片 文件夹路径
  99 +/// @param conversationId 会话id
  100 ++ (NSString *)getChatImageFile:(NSString *)conversationId;
  101 +
  102 +
  103 +
  104 +/// 某一个聊天视频或视频封面图 文件地址
  105 +/// @param conversationId 会话id
  106 +/// @param fileName 文件名字
  107 +/// @param read 是否是读文件
  108 +/// @param isSnapshot 是否是视频封面图
  109 +/// @param videoElem_path_lastPathComponent 自己发送时的视频名字 ,只在读文件的时候才需要用到
  110 +/// @param snapshotPath_lastPathComponent 自己发送时的视频封面图名字,只在读文件的时候才需要用到
  111 ++ (NSString *)chatVideoPathByConversationId:(NSString *)conversationId fileName:(NSString *)fileName isRead:(BOOL)read isSnapshot:(BOOL)isSnapshot videoElem_path_lastPathComponent:(NSString *)videoElem_path_lastPathComponent snapshotPath_lastPathComponent:(NSString *)snapshotPath_lastPathComponent;
  112 +
  113 +/// 获取本地聊天视频 文件夹路径
  114 +/// @param conversationId 会话id
  115 ++ (NSString *)getChatVideoFile:(NSString *)conversationId;
  116 +
  117 +/// 发送图片或视频消息时上传给IM的文件路径,发送消息成功后或者失败后会把这个图片或视频文件拷贝到对应的 image 或者video 文件夹下
  118 +/// @param fileName 临时图片或视频名字
  119 ++ (NSString *)chatImageAndVideoUploadIMCachePath:(NSString *)fileName;
  120 +
  121 +/// 删除聊天 图片 和 视频 上传时的临时文件
  122 ++ (BOOL )deleteChatImageAndVideoUploadImCache;
  123 +
87 124 /**
88 125 0
89 126 @param userId 单聊为对方ID,群聊为群聊 ID ,语音文件保存路径
... ...
CNLiveBusinessTools/Classes/CNLiveBusinessTools.m
... ... @@ -534,7 +534,6 @@
534 534 NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempName];
535 535  
536 536 NSFileManager *fileManager = [NSFileManager defaultManager];
537   -
538 537 BOOL isDir = NO;
539 538  
540 539 // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
... ... @@ -580,7 +579,317 @@
580 579 }
581 580 return filePath;
582 581 }
  582 +/// 获取聊天文件路径
  583 +/// @param userId 会话id
  584 +/// @param msgId 消息id
  585 ++ (NSString *)getChatFilesPathByUserId:(NSString *)userId msgId:(NSString *)msgId
  586 +{
  587 + NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/file/%@", CNUserShareModel.uid, userId,msgId];
  588 +
  589 + NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
  590 +
  591 + NSFileManager *fileManager = [NSFileManager defaultManager];
  592 +
  593 + BOOL isDir = NO;
  594 +
  595 + // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
  596 + BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
  597 +
  598 + if ( !(isDir == YES && existed == YES) ) {
  599 +
  600 + // 在 Documents 目录下创建
  601 +
  602 + if ([fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {
  603 + NSLog(@"创建聊天文件路径成功");
  604 + }
  605 + else
  606 + {
  607 + NSLog(@"聊天文件路径已经创建过");
  608 + }
  609 + }
  610 + return filePath;
  611 +}
  612 +
  613 +/// 某一个 聊天图片 文件地址
  614 +/// @param conversationId 会话id
  615 +/// @param read 是否是读文件
  616 +/// @param isThumb 是否是缩略图
  617 +/// @param imageElem_path_lastPathComponent 自己发送时的图片上传地址名字 ,只在读文件的时候才需要用到
  618 ++ (NSString *)chatImagePathByConversationId:(NSString *)conversationId imageName:(NSString *)imageName isRead:(BOOL)read isThumb:(BOOL)isThumb imageElem_path_lastPathComponent:(NSString *)imageElem_path_lastPathComponent;
  619 +{
  620 + #pragma mark 备注:自己发送的图片 imageElem.path 才有值,但是删除APP再次下载imageElem.path无值,收到别人的图片self.path无值
  621 +
  622 + NSFileManager *fileMgr = [NSFileManager defaultManager];
  623 + if (read) {
  624 +
  625 + //读本地图片文件, 包含兼容老版本逻辑
  626 +
  627 + if (!isThumb) {
  628 + //原图地址
  629 + NSString *tempStr = @"";
  630 + //path1 为本地image文件下的图片本地地址
  631 + NSString *path1 = [[CNLiveBusinessTools getChatImageFile:conversationId] stringByAppendingPathComponent:imageName];
  632 + //path2 为自己发送时传给 IM 的本地临时 image_video_temp 文件下的图片地址
  633 + NSString *path2 = [CNLiveBusinessTools chatImageAndVideoUploadIMCachePath:imageElem_path_lastPathComponent];
  634 + //path3 为老版本存的图片地址
  635 + NSString *path3 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:imageName];
  636 + //path4 为老版本自己发送时传给 IM 的本地图片地址
  637 + NSString *path4 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:imageElem_path_lastPathComponent];
  638 +
  639 + if ([fileMgr fileExistsAtPath:path1])
  640 + {
  641 + //自己下载的图片 本地地址
  642 + tempStr = path1;
  643 + }
  644 + else if ([fileMgr fileExistsAtPath:path2] && ![NSString isEmpty:imageElem_path_lastPathComponent]) {
  645 + // 自己发送时传给 IM 的本地图片地址
  646 + tempStr = path2;
  647 + }
  648 + else if ([fileMgr fileExistsAtPath:path3])
  649 + {
  650 + //老版本存的图片地址
  651 + tempStr = path3;
  652 + }
  653 + else if ([fileMgr fileExistsAtPath:path4] && ![NSString isEmpty:imageElem_path_lastPathComponent])
  654 + {
  655 + //为老版本自己发送时传给 IM 的本地图片地址
  656 + tempStr = path4;
  657 + }
  658 + return tempStr;
  659 + }
  660 + else
  661 + {
  662 + //缩略图地址
  663 + #pragma mark 本地缩略图地址
  664 + NSString *thumbStr = @"";
  665 +
  666 + //thumPath1 为本地Image文件夹下的缩略图地址
  667 + NSString *thumPath1 = [[CNLiveBusinessTools getChatImageFile:conversationId] stringByAppendingPathComponent:imageName];
  668 +
  669 + //thumPath2 为自己发送消息时本地临时文件夹下image_video_temp的缩略图地址
  670 + NSString *thumPath2 = [CNLiveBusinessTools chatImageAndVideoUploadIMCachePath:[NSString stringWithFormat:@"%@_thumb",imageElem_path_lastPathComponent]];
  671 +
  672 + //thumbPath3 为老版本存的缩略图 地址
  673 + NSString *thumbPath3 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:imageName];
  674 + //thumbPath4 为老版本自己发送时传给 IM 的本地图片地址
  675 + NSString *thumPath4 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_thumb",imageElem_path_lastPathComponent]];
  676 +
  677 + if ([fileMgr fileExistsAtPath:thumPath1])
  678 + {
  679 + //为本地Image文件夹下的缩略图地址
  680 + thumbStr = thumPath1;
  681 + }
  682 + else if ([fileMgr fileExistsAtPath:thumPath2] && ![NSString isEmpty:imageElem_path_lastPathComponent]) {
  683 + // 为自己发送消息时本地临时文件夹下image_video_temp的缩略图地址
  684 + thumbStr = thumPath2;
  685 + }
  686 + else if ([fileMgr fileExistsAtPath:thumbPath3])
  687 + {
  688 + //老版本存的缩略图 地址
  689 + thumbStr = thumbPath3;
  690 + }
  691 + else if ([fileMgr fileExistsAtPath:thumPath4] && ![NSString isEmpty:imageElem_path_lastPathComponent])
  692 + {
  693 + //为老版本自己发送时传给 IM 的本地图片地址
  694 + thumbStr = thumPath4;
  695 + }
  696 + return thumbStr;
  697 + }
  698 +
  699 +
  700 + }
  701 + else
  702 + {
  703 + //存文件
  704 + NSString *path = [[CNLiveBusinessTools getChatImageFile:conversationId] stringByAppendingPathComponent:imageName];
  705 + return path;
  706 + }
  707 +
  708 +}
  709 +/// 获取本地聊天图片 文件夹路径
  710 +/// @param conversationId 会话id
  711 ++ (NSString *)getChatImageFile:(NSString *)conversationId
  712 +{
  713 + // NSHomeDirectory():应用程序目录, 在Documents文件夹下创建
  714 + NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/Image", CNUserShareModel.uid, conversationId];
  715 +
  716 + NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
  717 +
  718 + NSFileManager *fileManager = [NSFileManager defaultManager];
  719 +
  720 + BOOL isDir = NO;
  721 +
  722 + // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
  723 + BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
  724 +
  725 + if ( !(isDir == YES && existed == YES) ) {
  726 +
  727 + [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  728 + }
  729 +
  730 + return filePath;
  731 +}
  732 +
  733 +/// 某一个聊天视频或视频封面图 文件地址
  734 +/// @param conversationId 会话id
  735 +/// @param fileName 文件名字
  736 +/// @param read 是否是读文件
  737 +/// @param isSnapshot 是否是视频封面图
  738 +/// @param videoElem_path_lastPathComponent 自己发送时的视频名字 ,只在读文件的时候才需要用到
  739 +/// @param snapshotPath_lastPathComponent 自己发送时的视频封面图名字,只在读文件的时候才需要用到
  740 ++ (NSString *)chatVideoPathByConversationId:(NSString *)conversationId fileName:(NSString *)fileName isRead:(BOOL)read isSnapshot:(BOOL)isSnapshot videoElem_path_lastPathComponent:(NSString *)videoElem_path_lastPathComponent snapshotPath_lastPathComponent:(NSString *)snapshotPath_lastPathComponent
  741 +{
  742 + NSFileManager *fileMgr = [NSFileManager defaultManager];
  743 +
  744 + if (read) {
  745 + //读文件
  746 +
  747 + if (!isSnapshot) {
  748 + //视频地址
  749 +
  750 + NSString *videoPath = @"";
  751 +
  752 + //path1 为Video文件夹下视频本地地址
  753 + NSString *path1 = [[CNLiveBusinessTools getChatVideoFile:conversationId] stringByAppendingPathComponent:fileName];
  754 + //path2 为自己发送时的本地image_video_temp文件夹下的视频地址
  755 + NSString *path2 = [CNLiveBusinessTools chatImageAndVideoUploadIMCachePath:videoElem_path_lastPathComponent];
  756 + //path3 为老版本已经下载的视频本地地址
  757 + NSString *path3 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:fileName];
  758 + //path4 为老版本自己发送的视频本地地址
  759 + NSString *path4 = [[CNLiveBusinessTools newChatFilePathByUserId:conversationId] stringByAppendingPathComponent:videoElem_path_lastPathComponent];
  760 +
  761 +
  762 + if ([fileMgr fileExistsAtPath:path1 isDirectory:nil])
  763 + {
  764 + //自己下载的视频本地地址
  765 + videoPath = path1;
  766 + }
  767 + else if ([fileMgr fileExistsAtPath:path2 isDirectory:nil] && ![NSString isEmpty:videoElem_path_lastPathComponent]) {
  768 + //自己发送时的本地视频地址
  769 + videoPath = path2;
  770 + }
  771 + else if ([fileMgr fileExistsAtPath:path3 isDirectory:nil])
  772 + {
  773 + //老版本下载的视频本地地址
  774 + videoPath = path3;
  775 + }
  776 + else if ([fileMgr fileExistsAtPath:path4 isDirectory:nil] && ![NSString isEmpty:videoElem_path_lastPathComponent])
  777 + {
  778 + //为老版本自己发送的视频本地地址
  779 + videoPath = path4;
  780 + }
  781 +
  782 + return videoPath;
  783 + }
  784 + else
  785 + {
  786 + //视频封面图地址
  787 + NSString *snapshotPath = @"";
  788 +
  789 + //snapshotPath1 为Video文件下的视频封面图 本地地址
  790 + NSString *snapshotPath1 = [[CNLiveBusinessTools getChatVideoFile:conversationId] stringByAppendingPathComponent:fileName];
  791 + //snapshotPath2 为自己发送消息时image_video_temp文件夹下本地视频封面图地址
  792 + NSString *snapshotPath2 = [CNLiveBusinessTools chatImageAndVideoUploadIMCachePath:snapshotPath_lastPathComponent];
  793 + //snapshotPath3 为老版本存的视频图 地址
  794 + NSString *snapshotPath3 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:fileName];
  795 + //snapshotPath4 为老版本自己发送的视频封面图地址
  796 + NSString *snapshotPath4 = [[CNLiveBusinessTools chatFileWithCachePathByUserId:conversationId] stringByAppendingPathComponent:snapshotPath_lastPathComponent];
  797 +
  798 + if ([PathUtility isExistFile:snapshotPath1])
  799 + {
  800 + //自己下载的视频图 本地地址
  801 + snapshotPath = snapshotPath1;
  802 + }
  803 + if ([PathUtility isExistFile:snapshotPath2] && ![NSString isEmpty:snapshotPath_lastPathComponent]) {
  804 + // 自己发送消息时本地视频图地址
  805 + snapshotPath = snapshotPath2;
  806 + }
  807 + else if ([PathUtility isExistFile:snapshotPath3])
  808 + {
  809 + //老版本存的视频图 地址
  810 + snapshotPath = snapshotPath3;
  811 + }
  812 + else if ([PathUtility isExistFile:snapshotPath4] && ![NSString isEmpty:snapshotPath_lastPathComponent])
  813 + {
  814 + // 为老版本自己发送的视频封面图地址
  815 + snapshotPath = snapshotPath4;
  816 + }
  817 +
  818 + return snapshotPath;
  819 + }
  820 +
  821 + }
  822 + else
  823 + {
  824 + //存文件
  825 + NSString *filePath = [[CNLiveBusinessTools getChatVideoFile:conversationId] stringByAppendingPathComponent:fileName];
  826 + return filePath;
  827 + }
  828 +}
  829 +/// 获取本地聊天视频 文件夹路径
  830 +/// @param conversationId 会话id
  831 ++ (NSString *)getChatVideoFile:(NSString *)conversationId
  832 +{
  833 + // NSHomeDirectory():应用程序目录, 在Documents文件夹下创建
  834 + NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/Video", CNUserShareModel.uid, conversationId];
  835 +
  836 + NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
  837 +
  838 + NSFileManager *fileManager = [NSFileManager defaultManager];
  839 +
  840 + BOOL isDir = NO;
  841 +
  842 + // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
  843 + BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
  844 +
  845 + if ( !(isDir == YES && existed == YES) ) {
  846 +
  847 + [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  848 + }
  849 +
  850 + return filePath;
  851 +}
583 852  
  853 ++ (NSString *)chatImageAndVideoUploadIMCachePath:(NSString *)fileName;
  854 +{
  855 + NSString *temp = [NSString stringWithFormat:@"Documents/%@/image_video_temp", CNUserShareModel.uid];
  856 +
  857 + NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:temp];
  858 +
  859 + NSFileManager *fileManager = [NSFileManager defaultManager];
  860 +
  861 + BOOL isDir = NO;
  862 +
  863 + // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
  864 + BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
  865 +
  866 + if ( !(isDir == YES && existed == YES) ) {
  867 +
  868 + // 在 Documents 目录下创建一个 image_video_temp 目录
  869 + [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
  870 + }
  871 +
  872 + filePath = [filePath stringByAppendingPathComponent:fileName];
  873 + return filePath;
  874 +}
  875 ++ (BOOL )deleteChatImageAndVideoUploadImCache;
  876 +{
  877 + NSString *temp = [NSString stringWithFormat:@"Documents/%@/image_video_temp", CNUserShareModel.uid];
  878 +
  879 + NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:temp];
  880 +
  881 + NSFileManager *fileManager = [NSFileManager defaultManager];
  882 +
  883 + BOOL isDir = NO;
  884 +
  885 + // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
  886 + BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
  887 + if (existed) {
  888 + BOOL suceess = [fileManager removeItemAtPath:filePath error:nil];
  889 + return suceess;
  890 + }
  891 + return YES;
  892 +}
584 893 /**
585 894 0
586 895 @param userId 单聊为对方ID,群聊为群聊 ID ,语音文件保存路径
... ...