NIMSDK-AOS  10.9.90
V2NIMMessageAttachmentUtil.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.v2.utils;
2 
3 import android.text.TextUtils;
4 
11 import com.netease.nimlib.util.MD5;
12 import com.netease.nimlib.util.StringUtil;
13 import com.netease.nimlib.util.storage.NimStorageType;
14 import com.netease.nimlib.util.storage.NimStorageUtil;
15 
16 import java.io.File;
17 
18 /**
19  * 消息附件工具类
20  */
22 
23  /**
24  * 私有化构造器
25  */
27  }
28  /**
29  * 获取文件路径
30  * @param fileAttachment 文件附件
31  * @return 文件路径,如果文件不存在,返回null
32  */
33  public static String getFileAttachmentPath(V2NIMMessageFileAttachment fileAttachment){
34  if(fileAttachment == null){
35  return null;
36  }
37  String path = getDownloadAttachmentDefaultStoragePath(fileAttachment);
38  if(TextUtils.isEmpty(path) || !(new File(path).exists())) {
39  path = getFileAttachmentStoragePath(fileAttachment);
40  }
41  if(TextUtils.isEmpty(path)){
42  return null;
43  }
44  return new File(path).exists() ? path : null;
45  }
46 
47  /**
48  * 获取{@link V2NIMStorageService#downloadAttachment}的默认存储路径
49  * @param fileAttachment 文件附件
50  * @return 文件存储路径,一般不为null,fileAttachment为null时返回null
51  */
53  if(fileAttachment == null){
54  return null;
55  }
56  String path = fileAttachment.getPath();
57  if (!TextUtils.isEmpty(path)) {
58  return path;
59  } else {
60  String ext = extWithoutPointPrefixes(fileAttachment, V2NIMDownloadAttachmentType.V2NIM_DOWNLOAD_ATTACHMENT_TYPE_SOURCE);
61  return getDefaultSavePath(fileAttachment, ext,V2NIMDownloadAttachmentType.V2NIM_DOWNLOAD_ATTACHMENT_TYPE_SOURCE);
62  }
63  }
64 
65  /**
66  * 获取文件存储路径,和V1逻辑保持一致
67  * @param fileAttachment 文件附件
68  * @return 文件存储路径,一般不为null,fileAttachment为null时返回null
69  */
70  public static String getFileAttachmentStoragePath(V2NIMMessageFileAttachment fileAttachment){
71  if(fileAttachment == null){
72  return null;
73  }
74  String path = fileAttachment.getPath();
75  if (!TextUtils.isEmpty(path)) {
76  return path;
77  } else {
78  String fileName = getFileName(fileAttachment);
79  if(TextUtils.isEmpty(fileName)){
80  return null;
81  }
82  return NimStorageUtil.getWritePath(fileName, storageType(fileAttachment));
83  }
84  }
85 
86  /**
87  * 获取文件缩略图路径,和V1逻辑保持一致
88  * @param fileAttachment 文件附件
89  * @return 文件缩略图路径,如果文件不存在,返回null
90  */
91  public static String getFileAttachmentThumbPath(V2NIMMessageFileAttachment fileAttachment){
92  if(fileAttachment == null){
93  return null;
94  }
95  String path = getDownloadAttachmentDefaultThumbStoragePath(fileAttachment);
96  if(TextUtils.isEmpty(path) || !(new File(path).exists())) {
97  path = getFileAttachmentThumbStoragePath(fileAttachment);
98  }
99  if(TextUtils.isEmpty(path)){
100  return null;
101  }
102  return new File(path).exists() ? path : null;
103  }
104 
105  /**
106  * 获取{@link V2NIMStorageService#downloadAttachment}的缩略图默认存储路径
107  * @param fileAttachment 文件附件
108  * @return 文件存储路径,一般不为null,fileAttachment为null时返回null
109  */
111  if(fileAttachment == null){
112  return null;
113  }
114  String ext = extWithoutPointPrefixes(fileAttachment, V2NIMDownloadAttachmentType.V2NIM_DOWNLOAD_ATTACHMENT_TYPE_THUMBNAIL);
115  return getDefaultSavePath(fileAttachment, ext,V2NIMDownloadAttachmentType.V2NIM_DOWNLOAD_ATTACHMENT_TYPE_THUMBNAIL);
116  }
117 
118  /**
119  * 获取文件缩略图存储路径,和V1逻辑保持一致
120  * @param fileAttachment 文件附件
121  * @return 文件缩略图存储路径,一般不为null,fileAttachment为null时返回null
122  */
123  public static String getFileAttachmentThumbStoragePath(V2NIMMessageFileAttachment fileAttachment){
124  if(fileAttachment == null){
125  return null;
126  }
127  String fileName = getFileName(fileAttachment);
128  if (!TextUtils.isEmpty(fileName)) {
129  int dotIndex = fileName.lastIndexOf(".");
130  // 如果找到了 '.' 而且最后一个字符不是 '.',则需要进行裁剪
131  if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
132  fileName = fileName.substring(0, dotIndex);
133  }
134  }else{
135  return null;
136  }
137  return NimStorageUtil.getWritePath(fileName, NimStorageType.TYPE_THUMB_IMAGE);
138  }
139 
140  private static String getFileName(V2NIMMessageFileAttachment fileAttachment){
141  if(fileAttachment == null){
142  return null;
143  }
144  String md5 = fileAttachment.getMd5();
145  if (TextUtils.isEmpty(md5)) {
146  md5 = MD5.getStringMD5(fileAttachment.getUrl());
147  }
148  return md5 != null ? md5 : "";
149  }
150 
151  private static NimStorageType storageType(V2NIMMessageFileAttachment fileAttachment){
152  if(fileAttachment instanceof V2NIMMessageAudioAttachment){
153  return NimStorageType.TYPE_AUDIO;
154  }else if(fileAttachment instanceof V2NIMMessageImageAttachment){
155  return NimStorageType.TYPE_IMAGE;
156  }else if(fileAttachment instanceof V2NIMMessageVideoAttachment){
157  return NimStorageType.TYPE_VIDEO;
158  }else{
159  return NimStorageType.TYPE_FILE;
160  }
161  }
162 
163  private static String extWithoutPointPrefixes(V2NIMMessageFileAttachment attachment,V2NIMDownloadAttachmentType downloadType)
164  {
165  String ext;
166  switch (downloadType) {
167  // 封面图用png
168  case V2NIM_DOWNLOAD_ATTACHMENT_TYPE_THUMBNAIL:
169  case V2NIM_DOWNLOAD_ATTACHMENT_TYPE_VIDEO_COVER:
170  ext = "png";
171  break;
172  // 原图使用自带后缀
173  case V2NIM_DOWNLOAD_ATTACHMENT_TYPE_SOURCE:
174  default:
175  ext = attachment == null ? "" : attachment.getExt();
176  break;
177  }
178 
179  //去掉点号前缀
180  if (StringUtil.isEmpty(ext))
181  {
182  return "";
183  }
184  if (ext.startsWith("."))
185  {
186  ext = ext.substring(1);
187  }
188  return ext;
189  }
190 
191  private static String getDefaultSavePath(V2NIMMessageFileAttachment fileAttachment, String ext,V2NIMDownloadAttachmentType downloadType) {
192  if (fileAttachment == null) {
193  return null;
194  }
195  String md5 = fileAttachment.getMd5();
196  if (StringUtil.isEmpty(md5)) {
197  return null;
198  }
199  String fileName = "" + (md5) + "." + (ext);
201  fileName = "thumb_" + (fileName);
202  }
203  return NimStorageUtil.getWritePath(fileName, storageType(fileAttachment));
204  }
205 }
static String getFileAttachmentPath(V2NIMMessageFileAttachment fileAttachment)
获取文件路径
static String getFileAttachmentStoragePath(V2NIMMessageFileAttachment fileAttachment)
获取文件存储路径,和V1逻辑保持一致
static String getDownloadAttachmentDefaultThumbStoragePath(V2NIMMessageFileAttachment fileAttachment)
获取V2NIMStorageService#downloadAttachment的缩略图默认存储路径
V2NIM_DOWNLOAD_ATTACHMENT_TYPE_SOURCE
原始资源,支持全部有附件的类型
static String getDownloadAttachmentDefaultStoragePath(V2NIMMessageFileAttachment fileAttachment)
获取V2NIMStorageService#downloadAttachment的默认存储路径
static String getFileAttachmentThumbPath(V2NIMMessageFileAttachment fileAttachment)
获取文件缩略图路径,和V1逻辑保持一致
static String getFileAttachmentThumbStoragePath(V2NIMMessageFileAttachment fileAttachment)
获取文件缩略图存储路径,和V1逻辑保持一致