OCFile.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2012 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.datamodel;
  23. import android.content.ContentResolver;
  24. import android.content.Context;
  25. import android.net.Uri;
  26. import android.os.Parcel;
  27. import android.os.Parcelable;
  28. import android.support.annotation.NonNull;
  29. import android.support.v4.content.FileProvider;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.lib.common.network.WebdavEntry;
  32. import com.owncloud.android.lib.common.network.WebdavUtils;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.lib.resources.files.ServerFileInterface;
  35. import com.owncloud.android.utils.MimeType;
  36. import java.io.File;
  37. import lombok.Getter;
  38. import lombok.Setter;
  39. import third_parties.daveKoeller.AlphanumComparator;
  40. public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterface {
  41. private final static String PERMISSION_SHARED_WITH_ME = "S";
  42. private final static String PERMISSION_CAN_RESHARE = "R";
  43. private final static String PERMISSION_CAN_WRITE = "CK";
  44. public static final String PATH_SEPARATOR = "/";
  45. public static final String ROOT_PATH = PATH_SEPARATOR;
  46. private static final String TAG = OCFile.class.getSimpleName();
  47. @Getter
  48. @Setter
  49. private long fileId; // android internal ID of the file
  50. @Getter @Setter private long parentId;
  51. @Getter @Setter private long fileLength;
  52. @Getter
  53. @Setter
  54. private long creationTimestamp; // UNIX timestamp of the time the file was created
  55. @Getter
  56. @Setter
  57. private long modificationTimestamp; // UNIX timestamp of the file modification time
  58. /** UNIX timestamp of the modification time, corresponding to the value returned by the server
  59. * in the last synchronization of THE CONTENTS of this file.
  60. */
  61. @Getter @Setter private long modificationTimestampAtLastSyncForData;
  62. @Setter private String remotePath;
  63. private String localPath;
  64. @Getter @Setter private String mimeType;
  65. @Getter private boolean needsUpdatingWhileSaving;
  66. @Getter @Setter private long lastSyncDateForProperties;
  67. @Getter @Setter private long lastSyncDateForData;
  68. @Getter @Setter private boolean availableOffline;
  69. @Getter
  70. @Setter
  71. private boolean previewAvailable;
  72. @Getter private String etag;
  73. @Getter @Setter private boolean sharedViaLink;
  74. @Getter @Setter private String publicLink;
  75. @Getter @Setter private String permissions;
  76. @Getter
  77. @Setter
  78. private String remoteId; // The fileid namespaced by the instance fileId, globally unique
  79. @Getter @Setter private boolean updateThumbnailNeeded;
  80. @Getter @Setter private boolean downloading;
  81. @Getter
  82. @Setter
  83. private String etagInConflict; // Only saves file etag in the server, when there is a conflict
  84. @Getter @Setter private boolean sharedWithSharee;
  85. @Getter @Setter private boolean favorite;
  86. @Getter @Setter private boolean encrypted;
  87. @Getter @Setter private WebdavEntry.MountType mountType;
  88. /**
  89. * URI to the local path of the file contents, if stored in the device; cached after first call
  90. * to {@link #getStorageUri()}
  91. */
  92. private Uri localUri;
  93. /**
  94. * Exportable URI to the local path of the file contents, if stored in the device.
  95. * <p>
  96. * Cached after first call, until changed.
  97. */
  98. private Uri exposedFileUri;
  99. @Getter @Setter private String encryptedFileName;
  100. /**
  101. * Create new {@link OCFile} with given path.
  102. * <p>
  103. * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
  104. *
  105. * @param path The remote path of the file.
  106. */
  107. public OCFile(String path) {
  108. resetData();
  109. needsUpdatingWhileSaving = false;
  110. if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
  111. throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
  112. }
  113. remotePath = path;
  114. }
  115. /**
  116. * Reconstruct from parcel
  117. *
  118. * @param source The source parcel
  119. */
  120. private OCFile(Parcel source) {
  121. fileId = source.readLong();
  122. parentId = source.readLong();
  123. fileLength = source.readLong();
  124. creationTimestamp = source.readLong();
  125. modificationTimestamp = source.readLong();
  126. modificationTimestampAtLastSyncForData = source.readLong();
  127. remotePath = source.readString();
  128. localPath = source.readString();
  129. mimeType = source.readString();
  130. needsUpdatingWhileSaving = source.readInt() == 0;
  131. availableOffline = source.readInt() == 1;
  132. lastSyncDateForProperties = source.readLong();
  133. lastSyncDateForData = source.readLong();
  134. etag = source.readString();
  135. sharedViaLink = source.readInt() == 1;
  136. publicLink = source.readString();
  137. permissions = source.readString();
  138. remoteId = source.readString();
  139. updateThumbnailNeeded = source.readInt() == 1;
  140. downloading = source.readInt() == 1;
  141. etagInConflict = source.readString();
  142. sharedWithSharee = source.readInt() == 1;
  143. favorite = source.readInt() == 1;
  144. encrypted = source.readInt() == 1;
  145. encryptedFileName = source.readString();
  146. mountType = (WebdavEntry.MountType) source.readSerializable();
  147. }
  148. @Override
  149. public void writeToParcel(Parcel dest, int flags) {
  150. dest.writeLong(fileId);
  151. dest.writeLong(parentId);
  152. dest.writeLong(fileLength);
  153. dest.writeLong(creationTimestamp);
  154. dest.writeLong(modificationTimestamp);
  155. dest.writeLong(modificationTimestampAtLastSyncForData);
  156. dest.writeString(remotePath);
  157. dest.writeString(localPath);
  158. dest.writeString(mimeType);
  159. dest.writeInt(needsUpdatingWhileSaving ? 1 : 0);
  160. dest.writeInt(availableOffline ? 1 : 0);
  161. dest.writeLong(lastSyncDateForProperties);
  162. dest.writeLong(lastSyncDateForData);
  163. dest.writeString(etag);
  164. dest.writeInt(sharedViaLink ? 1 : 0);
  165. dest.writeString(publicLink);
  166. dest.writeString(permissions);
  167. dest.writeString(remoteId);
  168. dest.writeInt(updateThumbnailNeeded ? 1 : 0);
  169. dest.writeInt(downloading ? 1 : 0);
  170. dest.writeString(etagInConflict);
  171. dest.writeInt(sharedWithSharee ? 1 : 0);
  172. dest.writeInt(favorite ? 1 : 0);
  173. dest.writeInt(encrypted ? 1 : 0);
  174. dest.writeString(encryptedFileName);
  175. dest.writeSerializable(mountType);
  176. }
  177. public String getDecryptedRemotePath() {
  178. return remotePath;
  179. }
  180. /**
  181. * Returns the remote path of the file on ownCloud
  182. *
  183. * @return The remote path to the file
  184. */
  185. public String getRemotePath() {
  186. if (isEncrypted() && !isFolder()) {
  187. String parentPath = new File(remotePath).getParent();
  188. if (parentPath.endsWith("/")) {
  189. return parentPath + getEncryptedFileName();
  190. } else {
  191. return parentPath + "/" + getEncryptedFileName();
  192. }
  193. } else {
  194. if (isFolder()) {
  195. if (remotePath.endsWith("/")) {
  196. return remotePath;
  197. } else {
  198. return remotePath + "/";
  199. }
  200. } else {
  201. return remotePath;
  202. }
  203. }
  204. }
  205. /**
  206. * Can be used to check, whether or not this file exists in the database
  207. * already
  208. *
  209. * @return true, if the file exists in the database
  210. */
  211. public boolean fileExists() {
  212. return fileId != -1;
  213. }
  214. /**
  215. * Use this to find out if this file is a folder.
  216. *
  217. * @return true if it is a folder
  218. */
  219. public boolean isFolder() {
  220. return MimeType.DIRECTORY.equals(mimeType);
  221. }
  222. /**
  223. * Sets mimetype to folder and returns this file
  224. * Only for testing
  225. *
  226. * @return OCFile this file
  227. */
  228. public OCFile setFolder() {
  229. setMimeType(MimeType.DIRECTORY);
  230. return this;
  231. }
  232. /**
  233. * Use this to check if this file is available locally
  234. *
  235. * @return true if it is
  236. */
  237. public boolean isDown() {
  238. return !isFolder() && existsOnDevice();
  239. }
  240. /**
  241. * Use this to check if this file or folder is available locally
  242. *
  243. * @return true if it is
  244. */
  245. public boolean existsOnDevice() {
  246. if (localPath != null && localPath.length() > 0) {
  247. return new File(localPath).exists();
  248. }
  249. return false;
  250. }
  251. /**
  252. * The path, where the file is stored locally
  253. *
  254. * @return The local path to the file
  255. */
  256. public String getStoragePath() {
  257. return localPath;
  258. }
  259. /**
  260. * The URI to the file contents, if stored locally
  261. *
  262. * @return A URI to the local copy of the file, or NULL if not stored in the device
  263. */
  264. public Uri getStorageUri() {
  265. if (localPath == null || localPath.length() == 0) {
  266. return null;
  267. }
  268. if (localUri == null) {
  269. Uri.Builder builder = new Uri.Builder();
  270. builder.scheme(ContentResolver.SCHEME_FILE);
  271. builder.path(localPath);
  272. localUri = builder.build();
  273. }
  274. return localUri;
  275. }
  276. public Uri getLegacyExposedFileUri() {
  277. if (localPath == null || localPath.length() == 0) {
  278. return null;
  279. }
  280. if (exposedFileUri == null) {
  281. return Uri.parse(ContentResolver.SCHEME_FILE + "://" + WebdavUtils.encodePath(localPath));
  282. }
  283. return exposedFileUri;
  284. }
  285. /*
  286. Partly disabled because not all apps understand paths that we get via this method for now
  287. */
  288. public Uri getExposedFileUri(Context context) {
  289. if (localPath == null || localPath.length() == 0) {
  290. return null;
  291. }
  292. if (exposedFileUri == null) {
  293. try {
  294. exposedFileUri = FileProvider.getUriForFile(
  295. context,
  296. context.getString(R.string.file_provider_authority),
  297. new File(localPath));
  298. } catch (IllegalArgumentException ex) {
  299. // Could not share file using FileProvider URI scheme.
  300. // Fall back to legacy URI parsing.
  301. getLegacyExposedFileUri();
  302. }
  303. }
  304. return exposedFileUri;
  305. }
  306. /**
  307. * Can be used to set the path where the file is stored
  308. *
  309. * @param storage_path to set
  310. */
  311. public void setStoragePath(String storage_path) {
  312. localPath = storage_path;
  313. localUri = null;
  314. exposedFileUri = null;
  315. }
  316. /**
  317. * Returns the filename and "/" for the root directory
  318. *
  319. * @return The name of the file
  320. */
  321. public String getFileName() {
  322. File f = new File(remotePath);
  323. return f.getName().length() == 0 ? ROOT_PATH : f.getName();
  324. }
  325. /**
  326. * Sets the name of the file
  327. * <p/>
  328. * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root
  329. * directory
  330. */
  331. public void setFileName(String name) {
  332. Log_OC.d(TAG, "OCFile name changing from " + remotePath);
  333. if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) &&
  334. !ROOT_PATH.equals(remotePath)) {
  335. String parent = new File(this.getRemotePath()).getParent();
  336. parent = parent.endsWith(PATH_SEPARATOR) ? parent : parent + PATH_SEPARATOR;
  337. remotePath = parent + name;
  338. if (isFolder()) {
  339. remotePath += PATH_SEPARATOR;
  340. }
  341. Log_OC.d(TAG, "OCFile name changed to " + remotePath);
  342. }
  343. }
  344. /**
  345. * Used internally. Reset all file properties
  346. */
  347. private void resetData() {
  348. fileId = -1;
  349. remotePath = null;
  350. parentId = 0;
  351. localPath = null;
  352. mimeType = null;
  353. fileLength = 0;
  354. creationTimestamp = 0;
  355. modificationTimestamp = 0;
  356. modificationTimestampAtLastSyncForData = 0;
  357. lastSyncDateForProperties = 0;
  358. lastSyncDateForData = 0;
  359. availableOffline = false;
  360. needsUpdatingWhileSaving = false;
  361. etag = null;
  362. sharedViaLink = false;
  363. publicLink = null;
  364. permissions = null;
  365. remoteId = null;
  366. updateThumbnailNeeded = false;
  367. downloading = false;
  368. etagInConflict = null;
  369. sharedWithSharee = false;
  370. favorite = false;
  371. encrypted = false;
  372. encryptedFileName = null;
  373. mountType = WebdavEntry.MountType.INTERNAL;
  374. }
  375. /**
  376. * get remote path of parent file
  377. *
  378. * @return remote path
  379. */
  380. public String getParentRemotePath() {
  381. String parentPath = new File(this.getRemotePath()).getParent();
  382. return parentPath.endsWith("/") ? parentPath : parentPath + "/";
  383. }
  384. @Override
  385. public int describeContents() {
  386. return super.hashCode();
  387. }
  388. @Override
  389. public int compareTo(@NonNull OCFile another) {
  390. if (isFolder() && another.isFolder()) {
  391. return new AlphanumComparator().compare(this, another);
  392. } else if (isFolder()) {
  393. return -1;
  394. } else if (another.isFolder()) {
  395. return 1;
  396. }
  397. return new AlphanumComparator().compare(this, another);
  398. }
  399. @Override
  400. public boolean equals(Object o) {
  401. if (this == o) {
  402. return true;
  403. }
  404. if (o == null || getClass() != o.getClass()) {
  405. return false;
  406. }
  407. OCFile ocFile = (OCFile) o;
  408. return fileId == ocFile.fileId && parentId == ocFile.parentId;
  409. }
  410. @Override
  411. public int hashCode() {
  412. return 31 * (int) (fileId ^ (fileId >>> 32)) + (int) (parentId ^ (parentId >>> 32));
  413. }
  414. @NonNull
  415. @Override
  416. public String toString() {
  417. String asString = "[fileId=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
  418. "parentId=%s, availableOffline=%s etag=%s favourite=%s]";
  419. return String.format(asString, fileId, getFileName(), mimeType, isDown(),
  420. localPath, remotePath, parentId, availableOffline,
  421. etag, favorite);
  422. }
  423. public void setEtag(String etag) {
  424. this.etag = etag != null ? etag : "";
  425. }
  426. public long getLocalModificationTimestamp() {
  427. if (localPath != null && localPath.length() > 0) {
  428. File f = new File(localPath);
  429. return f.lastModified();
  430. }
  431. return 0;
  432. }
  433. /**
  434. * @return 'True' if the file is hidden
  435. */
  436. public boolean isHidden() {
  437. return getFileName().length() > 0 && getFileName().charAt(0) == '.';
  438. }
  439. /**
  440. * The unique fileId for the file within the instance
  441. *
  442. * @return file fileId, unique within the instance
  443. */
  444. public String getLocalId() {
  445. return getRemoteId().substring(0, 8).replaceAll("^0*", "");
  446. }
  447. public boolean isInConflict() {
  448. return etagInConflict != null && !"".equals(etagInConflict);
  449. }
  450. public boolean isSharedWithMe() {
  451. String permissions = getPermissions();
  452. return permissions != null && permissions.contains(PERMISSION_SHARED_WITH_ME);
  453. }
  454. public boolean canReshare() {
  455. String permissions = getPermissions();
  456. return permissions != null && permissions.contains(PERMISSION_CAN_RESHARE);
  457. }
  458. public boolean canWrite() {
  459. String permissions = getPermissions();
  460. return permissions != null && permissions.contains(PERMISSION_CAN_WRITE);
  461. }
  462. public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
  463. @Override
  464. public OCFile createFromParcel(Parcel source) {
  465. return new OCFile(source);
  466. }
  467. @Override
  468. public OCFile[] newArray(int size) {
  469. return new OCFile[size];
  470. }
  471. };
  472. }