NIMSDK-AOS  9.16.0
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.SDKCache;
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 
20 public class FileAttachment implements MsgAttachment {
21 
25  protected String path;
26 
30  protected long size;
31 
35  protected String md5;
36 
40  protected String url;
41 
45  protected String displayName;
46 
50  protected String extension;
51 
55  private long expire;
56 
60  protected String nosTokenSceneKey = NimNosSceneKeyConstant.NIM_DEFAULT_IM;
61 
65  protected boolean forceUpload = false;
69  private boolean isUri = false;
70 
71  public FileAttachment() {
72 
73  }
74 
75  public FileAttachment(String attach) {
76  fromJson(attach);
77  }
78 
84  public String getPath() {
85  String path = getPathForSave();
86  return new File(path).exists() ? path : null;
87  }
88 
94  public String getPathForSave() {
95  if (!isUri && !TextUtils.isEmpty(path)) {
96  return path;
97  } else {
98  return NimStorageUtil.getWritePath(getFileName(), storageType());
99  }
100  }
101 
107  public String getThumbPath() {
108  String path = getThumbPathForSave();
109  return new File(path).exists() ? path : null;
110  }
111 
117  public String getThumbPathForSave() {
118  String fileName = getFileName();
119  if (!TextUtils.isEmpty(fileName)) {
120  int dotIndex = fileName.lastIndexOf(".");
121  // 如果找到了 '.' 而且最后一个字符不是 '.',则需要进行裁剪
122  if (dotIndex >= 0 && dotIndex < fileName.length() - 1) {
123  fileName = fileName.substring(0, dotIndex);
124  }
125  }
126  return NimStorageUtil.getWritePath(fileName, NimStorageType.TYPE_THUMB_IMAGE);
127  }
128 
134  public void setPath(String path) {
135  this.path = path;
136  this.isUri = UriUtils.isFileOrContentUri(path);
137  }
138 
146  public boolean setUri(Uri uri) {
147  if(uri == null)
148  {
149  return false;
150  }
151 
152  if(!UriUtils.isFileOrContentUri(uri))
153  {
154  return false;
155  }
156  isUri = true;
157  this.path = uri.toString();
158  return true;
159  }
160 
161  public Uri getUri() {
162  if(!isUri)
163  {
164  return null;
165  }
166  return UriUtils.string2Uri(path);
167  }
168 
169 
170 
176  public long getSize() {
177  return size;
178  }
179 
185  public void setSize(long size) {
186  this.size = size;
187  }
188 
194  public String getMd5() {
195  return md5;
196  }
197 
203  public void setMd5(String md5) {
204  this.md5 = md5;
205  }
206 
212  public String getUrl() {
213  return url;
214  }
215 
221  public void setUrl(String url) {
222  this.url = url;
223  }
224 
230  public String getExtension() {
231  return extension;
232  }
233 
239  public void setExtension(String extension) {
240  this.extension = extension;
241  }
242 
248  public String getFileName() {
249  if(isUri)
250  {
251  return UriUtils.getFileNameFromUri(SDKCache.getContext(),UriUtils.string2Uri(path));
252  }
253  else if (!TextUtils.isEmpty(path)) {
254  return StringUtil.nameOfPath(path);
255  } else {
256  if (TextUtils.isEmpty(md5)) {
257  return MD5.getStringMD5(url);
258  } else {
259  return md5;
260  }
261  }
262  }
263 
269  public String getDisplayName() {
270  return displayName;
271  }
272 
278  public void setDisplayName(String displayName) {
279  this.displayName = displayName;
280  }
281 
282 
288  public String getNosTokenSceneKey() {
289  return nosTokenSceneKey;
290  }
291 
297  public void setNosTokenSceneKey(String nosTokenSceneKey) {
298  if (TextUtils.isEmpty(nosTokenSceneKey)) {
299  return;
300  }
301  this.nosTokenSceneKey = nosTokenSceneKey;
302  }
303 
304  public long getExpire() {
305  return expire;
306  }
307 
313  public boolean isForceUpload() {
314  return forceUpload;
315  }
316 
322  public void setForceUpload(boolean forceUpload) {
323  this.forceUpload = forceUpload;
324  }
325 
326  protected NimStorageType storageType() {
327  return NimStorageType.TYPE_FILE;
328  }
329 
330  protected void save(JSONObject json) {
331 
332  }
333 
334  protected void load(JSONObject json) {
335 
336  }
337 
338  private static final String KEY_PATH = "path";
339  private static final String KEY_NAME = "name";
340  private static final String KEY_SIZE = "size";
341  private static final String KEY_MD5 = "md5";
342  private static final String KEY_URL = "url";
343  private static final String KEY_EXT = "ext";
344  private static final String KEY_SCENE = "sen";
345  private static final String KEY_FORCE_UPLOAD = "force_upload";
346  private static final String KEY_EXPIRE = "expire";
347 
348 
349  @Override
350  public String toJson(boolean send) {
351  JSONObject object = new JSONObject();
352  try {
353  if (!send && !TextUtils.isEmpty(path)) {
354  object.put(KEY_PATH, path);
355  }
356 
357  if (!TextUtils.isEmpty(md5)) {
358  object.put(KEY_MD5, md5);
359  }
360 
361  if (!TextUtils.isEmpty(displayName)) {
362  object.put(KEY_NAME, displayName);
363  }
364 
365  object.put(KEY_URL, url);
366  object.put(KEY_SIZE, size);
367 
368 
369  if (!TextUtils.isEmpty(extension)) {
370  object.put(KEY_EXT, extension);
371  }
372  if (!TextUtils.isEmpty(nosTokenSceneKey)) {
373  object.put(KEY_SCENE, nosTokenSceneKey);
374  }
375 
376  if (expire > 0) {
377  object.put(KEY_EXPIRE, expire);
378  }
379 
380  object.put(KEY_FORCE_UPLOAD, forceUpload);
381  save(object);
382 
383  } catch (Exception e) {
384  e.printStackTrace();
385  }
386 
387  return object.toString();
388  }
389 
390  private void fromJson(String attach) {
391  JSONObject json = JSONHelper.parse(attach);
392  if(json == null)
393  {
394  return;
395  }
396  path = JSONHelper.getString(json, KEY_PATH);
397  Uri uri = UriUtils.string2Uri(path);
399  {
400  isUri = true;
401  }
402  md5 = JSONHelper.getString(json, KEY_MD5);
403  url = JSONHelper.getString(json, KEY_URL);
404  displayName = JSONHelper.getString(json, KEY_NAME);
405  size = JSONHelper.getLong(json, KEY_SIZE);
406  extension = JSONHelper.getString(json, KEY_EXT);
407  setNosTokenSceneKey(JSONHelper.getString(json, KEY_SCENE));
408  forceUpload = JSONHelper.getBoolean(json, KEY_FORCE_UPLOAD);
409  expire = JSONHelper.getLong(json, KEY_EXPIRE);
410  load(json);
411  }
412 }
String getUrl()
获取文件在服务器上的下载url。若文件还未上传,返回null
String nosTokenSceneKey
上传文件时用的对token对应的场景,默认NimNosSceneKeyConstant#NIM_DEFAULT_IM
static String getFileNameFromUri(Context context, Uri uri)
Definition: UriUtils.java:270
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