NIMSDK-AOS  10.9.90
FileAttachment.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.msg.attachment;
2 
3 import android.net.Uri;
4 import android.text.TextUtils;
5 import com.netease.nimlib.NimNosSceneKeyConstant;
6 import com.netease.nimlib.log.NimLog;
8 import com.netease.nimlib.util.JSONHelper;
9 import com.netease.nimlib.util.MD5;
10 import com.netease.nimlib.util.StringUtil;
11 import com.netease.nimlib.util.storage.NimStorageType;
12 import com.netease.nimlib.util.storage.NimStorageUtil;
13 import java.io.File;
14 import org.json.JSONObject;
15 
16 /**
17  * 带有文件的附件类型的基类
18  * 描述文件的相关信息
19  */
20 public class FileAttachment implements MsgAttachment {
21 
22  private static final String TAG = "FileAttachment";
23  public static final String KEY_DOT = ".";
24 
25  /**
26  * 文件路径
27  */
28  protected String path;
29 
30  /**
31  * 文件大小
32  */
33  protected long size;
34 
35  /**
36  * 文件内容的MD5
37  */
38  protected String md5;
39 
40  /**
41  * 文件下载地址
42  */
43  protected String url;
44 
45  /**
46  * 文件显示名
47  */
48  protected String displayName;
49 
50  /**
51  * 文件后缀名
52  */
53  protected String extension;
54 
55  /**
56  * 过期时间(仅对文件类型有效)
57  */
58  private long expire;
59 
60  /**
61  * 上传文件时用的对token对应的场景,默认{@link NimNosSceneKeyConstant#NIM_DEFAULT_IM}
62  */
63  protected String nosTokenSceneKey = NimNosSceneKeyConstant.NIM_DEFAULT_IM;
64 
65  /**
66  * 如果服务器存在相同的附件文件,是否强制重新上传 , 默认false
67  */
68  protected boolean forceUpload = false;
69  /**
70  * 是否是uri
71  */
72  private boolean isUri = false;
73 
74  public FileAttachment() {
75 
76  }
77 
78  public FileAttachment(String attach) {
79  fromJson(attach);
80  }
81 
82  /**
83  * 获取文件本地路径,若文件不存在,返回null
84  *
85  * @return 文件路径
86  */
87  public String getPath() {
88  try {
89  String path = getPathForSave();
90  if(TextUtils.isEmpty(path)){
91  return null;
92  }
93  return new File(path).exists() ? path : null;
94  }
95  catch (Throwable e) {
96  NimLog.e(TAG, "getPath exception", e);
97  return null;
98  }
99  }
100 
101  /**
102  * 获取用于保存该文件的位置
103  *
104  * @return 文件路径
105  */
106  public String getPathForSave() {
107  if (!isUri && !TextUtils.isEmpty(path)) {
108  return path;
109  } else {
110  return NimStorageUtil.getWritePath(getFileName(), storageType());
111  }
112  }
113 
114  /**
115  * 获取缩略图文件的本地路径,若文件不存在,返回null
116  *
117  * @return 文件路径
118  */
119  public String getThumbPath() {
120  String path = getThumbPathForSave();
121  return new File(path).exists() ? path : null;
122  }
123 
124  /**
125  * 获取用于保存缩略图文件的位置
126  *
127  * @return 文件路径
128  */
129  public String getThumbPathForSave() {
130  String fileName = getFileName();
131  if (!TextUtils.isEmpty(fileName)) {
132  int dotIndex = fileName.lastIndexOf(".");
133  // 如果找到了 '.' 而且最后一个字符不是 '.',则需要进行裁剪
134  if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
135  fileName = fileName.substring(0, dotIndex);
136  }
137  }
138  return NimStorageUtil.getWritePath(fileName, NimStorageType.TYPE_THUMB_IMAGE);
139  }
140 
141  /**
142  * 设置文件路径
143  *
144  * @param path 文件路径
145  */
146  public void setPath(String path) {
147  this.path = path;
148  this.isUri = UriUtils.isFileOrContentUri(path);
149  }
150 
151  /**
152  * 设置文件uri,
153  * 仅支持ContentResolver.SCHEME_FILE类型和ContentResolver.SCHEME_CONTENT类型的uri
154  * 仅支持发送消息时使用,接收的消息附件不支持uri
155  * @param uri
156  * @return false表示不支持的uri
157  */
158  public boolean setUri(Uri uri) {
159  if(uri == null)
160  {
161  return false;
162  }
163 
164  if(!UriUtils.isFileOrContentUri(uri))
165  {
166  return false;
167  }
168  isUri = true;
169  this.path = uri.toString();
170  return true;
171  }
172 
173  public Uri getUri() {
174  if(!isUri)
175  {
176  return null;
177  }
178  return UriUtils.string2Uri(path);
179  }
180 
181 
182 
183  /**
184  * 获取文件大小,单位为byte
185  *
186  * @return 文件大小
187  */
188  public long getSize() {
189  return size;
190  }
191 
192  /**
193  * 设置文件大小,单位为byte
194  *
195  * @param size 文件大小
196  */
197  public void setSize(long size) {
198  this.size = size;
199  }
200 
201  /**
202  * 获取文件内容MD5
203  *
204  * @return 内容MD5
205  */
206  public String getMd5() {
207  return md5;
208  }
209 
210  /**
211  * 设置文件内容MD5
212  *
213  * @param md5 内容MD5
214  */
215  public void setMd5(String md5) {
216  this.md5 = md5;
217  }
218 
219  /**
220  * 获取文件在服务器上的下载url。若文件还未上传,返回null
221  *
222  * @return 源站url
223  */
224  public String getUrl() {
225  return url;
226  }
227 
228  /**
229  * 设置文件在服务器上的下载url
230  *
231  * @param url
232  */
233  public void setUrl(String url) {
234  this.url = url;
235  }
236 
237  /**
238  * 获取文件后缀名
239  *
240  * @return 后缀名
241  */
242  public String getExtension() {
243  return extension;
244  }
245 
246  /**
247  * 设置文件后缀名
248  *
249  * @param extension 后缀名
250  */
251  public void setExtension(String extension) {
252  this.extension = extension;
253  }
254 
255  /**
256  * 获取文件名。
257  *
258  * @return
259  */
260  public String getFileName() {
261  if(isUri)
262  {
263  if (TextUtils.isEmpty(md5)) {
264  if (TextUtils.isEmpty(url)) {
265  return MD5.getStringMD5(path);
266  }
267  return MD5.getStringMD5(url);
268  } else {
269  return md5;
270  }
271  }
272  else if (!TextUtils.isEmpty(path)) {
273  return StringUtil.nameOfPath(path);
274  } else {
275  if (TextUtils.isEmpty(md5)) {
276  return MD5.getStringMD5(url);
277  } else {
278  return md5;
279  }
280  }
281  }
282 
283  /**
284  * 获取文件的显示名。可以和文件名不同,仅用于界面展示
285  *
286  * @return 文件显示名
287  */
288  public String getDisplayName() {
289  return displayName;
290  }
291 
292  /**
293  * 设置文件显示名
294  *
295  * @param displayName 文件显示名
296  */
297  public void setDisplayName(String displayName) {
298  this.displayName = displayName;
299  }
300 
301 
302  /**
303  * 获取文件上传时的nos scene
304  *
305  * @return nos scene
306  */
307  public String getNosTokenSceneKey() {
308  return nosTokenSceneKey;
309  }
310 
311  /**
312  * 设置文件上传时的nos scene
313  *
314  * @param nosTokenSceneKey
315  */
316  public void setNosTokenSceneKey(String nosTokenSceneKey) {
317  if (TextUtils.isEmpty(nosTokenSceneKey)) {
318  return;
319  }
320  this.nosTokenSceneKey = nosTokenSceneKey;
321  }
322 
323  public long getExpire() {
324  return expire;
325  }
326 
327  /**
328  * 文件是否强制重新上传
329  *
330  * @return force upload
331  */
332  public boolean isForceUpload() {
333  return forceUpload;
334  }
335 
336  /**
337  * 设置文件是否强制重新上传,默认false
338  *
339  * @param forceUpload 是否强制重新上传
340  */
341  public void setForceUpload(boolean forceUpload) {
342  this.forceUpload = forceUpload;
343  }
344 
345  protected NimStorageType storageType() {
346  return NimStorageType.TYPE_FILE;
347  }
348 
349  protected void save(JSONObject json) {
350 
351  }
352 
353  protected void load(JSONObject json) {
354 
355  }
356 
357  public static final String KEY_PATH = "path";
358  public static final String KEY_NAME = "name";
359  public static final String KEY_SIZE = "size";
360  public static final String KEY_MD5 = "md5";
361  public static final String KEY_URL = "url";
362  public static final String KEY_EXT = "ext";
363  public static final String KEY_SCENE = "sen";
364  public static final String KEY_FORCE_UPLOAD = "force_upload";
365  public static final String KEY_EXPIRE = "expire";
366 
367  public static final String KEY_V2_INTERNAL_UPLOAD_STATE = "KEY_V2_INTERNAL_UPLOAD_STATE";
368 
369  @Override
370  public String toJson(boolean send) {
371  JSONObject object = new JSONObject();
372  try {
373  if (!send && !TextUtils.isEmpty(path)) {
374  object.put(KEY_PATH, path);
375  }
376 
377  if (!TextUtils.isEmpty(md5)) {
378  object.put(KEY_MD5, md5);
379  }
380 
381  if (!TextUtils.isEmpty(displayName)) {
382  object.put(KEY_NAME, displayName);
383  }
384 
385  object.put(KEY_URL, url);
386  object.put(KEY_SIZE, size);
387 
388 
389  if (!TextUtils.isEmpty(extension)) {
390  object.put(KEY_EXT, extension);
391  }
392  if (!TextUtils.isEmpty(nosTokenSceneKey)) {
393  object.put(KEY_SCENE, nosTokenSceneKey);
394  }
395 
396  if (expire > 0) {
397  object.put(KEY_EXPIRE, expire);
398  }
399 
400  object.put(KEY_FORCE_UPLOAD, forceUpload);
401  save(object);
402 
403  } catch (Exception e) {
404  e.printStackTrace();
405  }
406 
407  return object.toString();
408  }
409 
410  private void fromJson(String attach) {
411  JSONObject json = JSONHelper.parse(attach);
412  if(json == null)
413  {
414  return;
415  }
416  path = JSONHelper.getString(json, KEY_PATH);
417  Uri uri = UriUtils.string2Uri(path);
419  {
420  isUri = true;
421  }
422  md5 = JSONHelper.getString(json, KEY_MD5);
423  url = JSONHelper.getString(json, KEY_URL);
424  displayName = JSONHelper.getString(json, KEY_NAME);
425  size = JSONHelper.getLong(json, KEY_SIZE);
426  extension = JSONHelper.getString(json, KEY_EXT);
427  if(extension != null && extension.startsWith(KEY_DOT))
428  {
429  extension = extension.substring(1);
430  }
431  setNosTokenSceneKey(JSONHelper.getString(json, KEY_SCENE));
432  forceUpload = JSONHelper.getBoolean(json, KEY_FORCE_UPLOAD);
433  expire = JSONHelper.getLong(json, KEY_EXPIRE);
434  load(json);
435  }
436 }
String getUrl()
获取文件在服务器上的下载url。若文件还未上传,返回null
String nosTokenSceneKey
上传文件时用的对token对应的场景,默认NimNosSceneKeyConstant#NIM_DEFAULT_IM
void setExtension(String extension)
设置文件后缀名
boolean setUri(Uri uri)
设置文件uri, 仅支持ContentResolver.SCHEME_FILE类型和ContentResolver.SCHEME_CONTENT类型的uri 仅支持发...
static boolean isFileOrContentUri(String uriString)
判断URI是否为File或者Content类型URI File类型URI表示私有文件 Content类型URI表示共享文件,如图片,音频...
Definition: UriUtils.java:115
static Uri string2Uri(String uriString)
Definition: UriUtils.java:20
带有文件的附件类型的基类 描述文件的相关信息
String getPath()
获取文件本地路径,若文件不存在,返回null
void setForceUpload(boolean forceUpload)
设置文件是否强制重新上传,默认false
String getThumbPath()
获取缩略图文件的本地路径,若文件不存在,返回null
String getDisplayName()
获取文件的显示名。可以和文件名不同,仅用于界面展示
boolean forceUpload
如果服务器存在相同的附件文件,是否强制重新上传 , 默认false
boolean isForceUpload()
文件是否强制重新上传
void setMd5(String md5)
设置文件内容MD5
void setPath(String path)
设置文件路径
void setNosTokenSceneKey(String nosTokenSceneKey)
设置文件上传时的nos scene
String getThumbPathForSave()
获取用于保存缩略图文件的位置
void setDisplayName(String displayName)
设置文件显示名
void setUrl(String url)
设置文件在服务器上的下载url
long getSize()
获取文件大小,单位为byte
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。
String getPathForSave()
获取用于保存该文件的位置
String getNosTokenSceneKey()
获取文件上传时的nos scene
void setSize(long size)
设置文件大小,单位为byte