OCFile.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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.text.TextUtils;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.lib.common.network.WebdavEntry;
  31. import com.owncloud.android.lib.common.network.WebdavUtils;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.lib.resources.files.model.ServerFileInterface;
  34. import com.owncloud.android.lib.resources.shares.ShareeUser;
  35. import com.owncloud.android.utils.MimeType;
  36. import java.io.File;
  37. import java.util.List;
  38. import androidx.annotation.NonNull;
  39. import androidx.core.content.FileProvider;
  40. import lombok.Getter;
  41. import lombok.Setter;
  42. import third_parties.daveKoeller.AlphanumComparator;
  43. public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterface {
  44. private final static String PERMISSION_SHARED_WITH_ME = "S";
  45. private final static String PERMISSION_CAN_RESHARE = "R";
  46. private final static String PERMISSION_CAN_WRITE = "CK";
  47. public static final String PATH_SEPARATOR = "/";
  48. public static final String ROOT_PATH = PATH_SEPARATOR;
  49. private static final String TAG = OCFile.class.getSimpleName();
  50. @Getter @Setter private long fileId; // android internal ID of the file
  51. @Getter @Setter private long parentId;
  52. @Getter @Setter private long fileLength;
  53. @Getter @Setter private long creationTimestamp; // UNIX timestamp of the time the file was created
  54. @Getter @Setter private long modificationTimestamp; // UNIX timestamp of the file modification time
  55. /** UNIX timestamp of the modification time, corresponding to the value returned by the server
  56. * in the last synchronization of THE CONTENTS of this file.
  57. */
  58. @Getter @Setter private long modificationTimestampAtLastSyncForData;
  59. @Setter private String remotePath;
  60. private String localPath;
  61. @Getter @Setter private String mimeType;
  62. @Getter private boolean needsUpdatingWhileSaving;
  63. @Getter @Setter private long lastSyncDateForProperties;
  64. @Getter @Setter private long lastSyncDateForData;
  65. @Getter @Setter private boolean previewAvailable;
  66. @Getter private String etag;
  67. @Getter private String etagOnServer;
  68. @Getter @Setter private boolean sharedViaLink;
  69. @Getter @Setter private String publicLink;
  70. @Getter @Setter private String permissions;
  71. @Getter @Setter private String remoteId; // The fileid namespaced by the instance fileId, globally unique
  72. @Getter @Setter private boolean updateThumbnailNeeded;
  73. @Getter @Setter private boolean downloading;
  74. @Getter @Setter private String etagInConflict; // Only saves file etag in the server, when there is a conflict
  75. @Getter @Setter private boolean sharedWithSharee;
  76. @Getter @Setter private boolean favorite;
  77. @Getter @Setter private boolean encrypted;
  78. @Getter @Setter private WebdavEntry.MountType mountType;
  79. @Getter @Setter private int unreadCommentsCount;
  80. @Getter @Setter private String ownerId;
  81. @Getter @Setter private String ownerDisplayName;
  82. @Getter @Setter String note;
  83. @Getter @Setter private List<ShareeUser> sharees;
  84. /**
  85. * URI to the local path of the file contents, if stored in the device; cached after first call
  86. * to {@link #getStorageUri()}
  87. */
  88. private Uri localUri;
  89. /**
  90. * Exportable URI to the local path of the file contents, if stored in the device.
  91. * <p>
  92. * Cached after first call, until changed.
  93. */
  94. private Uri exposedFileUri;
  95. @Getter @Setter private String encryptedFileName;
  96. /**
  97. * Create new {@link OCFile} with given path.
  98. * <p>
  99. * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
  100. *
  101. * @param path The remote path of the file.
  102. */
  103. public OCFile(String path) {
  104. resetData();
  105. needsUpdatingWhileSaving = false;
  106. if (TextUtils.isEmpty(path) || !path.startsWith(PATH_SEPARATOR)) {
  107. throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
  108. }
  109. remotePath = path;
  110. }
  111. /**
  112. * Reconstruct from parcel
  113. *
  114. * @param source The source parcel
  115. */
  116. private OCFile(Parcel source) {
  117. fileId = source.readLong();
  118. parentId = source.readLong();
  119. fileLength = source.readLong();
  120. creationTimestamp = source.readLong();
  121. modificationTimestamp = source.readLong();
  122. modificationTimestampAtLastSyncForData = source.readLong();
  123. remotePath = source.readString();
  124. localPath = source.readString();
  125. mimeType = source.readString();
  126. needsUpdatingWhileSaving = source.readInt() == 0;
  127. lastSyncDateForProperties = source.readLong();
  128. lastSyncDateForData = source.readLong();
  129. etag = source.readString();
  130. etagOnServer = source.readString();
  131. sharedViaLink = source.readInt() == 1;
  132. publicLink = source.readString();
  133. permissions = source.readString();
  134. remoteId = source.readString();
  135. updateThumbnailNeeded = source.readInt() == 1;
  136. downloading = source.readInt() == 1;
  137. etagInConflict = source.readString();
  138. sharedWithSharee = source.readInt() == 1;
  139. favorite = source.readInt() == 1;
  140. encrypted = source.readInt() == 1;
  141. encryptedFileName = source.readString();
  142. ownerId = source.readString();
  143. ownerDisplayName = source.readString();
  144. mountType = (WebdavEntry.MountType) source.readSerializable();
  145. }
  146. @Override
  147. public void writeToParcel(Parcel dest, int flags) {
  148. dest.writeLong(fileId);
  149. dest.writeLong(parentId);
  150. dest.writeLong(fileLength);
  151. dest.writeLong(creationTimestamp);
  152. dest.writeLong(modificationTimestamp);
  153. dest.writeLong(modificationTimestampAtLastSyncForData);
  154. dest.writeString(remotePath);
  155. dest.writeString(localPath);
  156. dest.writeString(mimeType);
  157. dest.writeInt(needsUpdatingWhileSaving ? 1 : 0);
  158. dest.writeLong(lastSyncDateForProperties);
  159. dest.writeLong(lastSyncDateForData);
  160. dest.writeString(etag);
  161. dest.writeString(etagOnServer);
  162. dest.writeInt(sharedViaLink ? 1 : 0);
  163. dest.writeString(publicLink);
  164. dest.writeString(permissions);
  165. dest.writeString(remoteId);
  166. dest.writeInt(updateThumbnailNeeded ? 1 : 0);
  167. dest.writeInt(downloading ? 1 : 0);
  168. dest.writeString(etagInConflict);
  169. dest.writeInt(sharedWithSharee ? 1 : 0);
  170. dest.writeInt(favorite ? 1 : 0);
  171. dest.writeInt(encrypted ? 1 : 0);
  172. dest.writeString(encryptedFileName);
  173. dest.writeString(ownerId);
  174. dest.writeString(ownerDisplayName);
  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(PATH_SEPARATOR)) {
  189. return parentPath + getEncryptedFileName();
  190. } else {
  191. return parentPath + PATH_SEPARATOR + getEncryptedFileName();
  192. }
  193. } else {
  194. if (isFolder()) {
  195. if (remotePath.endsWith(PATH_SEPARATOR)) {
  196. return remotePath;
  197. } else {
  198. return remotePath + PATH_SEPARATOR;
  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 (!TextUtils.isEmpty(localPath)) {
  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 (TextUtils.isEmpty(localPath)) {
  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 (TextUtils.isEmpty(localPath)) {
  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 (TextUtils.isEmpty(localPath)) {
  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 (!TextUtils.isEmpty(name) && !name.contains(PATH_SEPARATOR) && !ROOT_PATH.equals(remotePath)) {
  334. String parent = new File(this.getRemotePath()).getParent();
  335. parent = parent.endsWith(PATH_SEPARATOR) ? parent : parent + PATH_SEPARATOR;
  336. remotePath = parent + name;
  337. if (isFolder()) {
  338. remotePath += PATH_SEPARATOR;
  339. }
  340. Log_OC.d(TAG, "OCFile name changed to " + remotePath);
  341. }
  342. }
  343. /**
  344. * Used internally. Reset all file properties
  345. */
  346. private void resetData() {
  347. fileId = -1;
  348. remotePath = null;
  349. parentId = 0;
  350. localPath = null;
  351. mimeType = null;
  352. fileLength = 0;
  353. creationTimestamp = 0;
  354. modificationTimestamp = 0;
  355. modificationTimestampAtLastSyncForData = 0;
  356. lastSyncDateForProperties = 0;
  357. lastSyncDateForData = 0;
  358. needsUpdatingWhileSaving = false;
  359. etag = null;
  360. etagOnServer = null;
  361. sharedViaLink = false;
  362. publicLink = null;
  363. permissions = null;
  364. remoteId = null;
  365. updateThumbnailNeeded = false;
  366. downloading = false;
  367. etagInConflict = null;
  368. sharedWithSharee = false;
  369. favorite = false;
  370. encrypted = false;
  371. encryptedFileName = null;
  372. mountType = WebdavEntry.MountType.INTERNAL;
  373. }
  374. /**
  375. * get remote path of parent file
  376. *
  377. * @return remote path
  378. */
  379. public String getParentRemotePath() {
  380. String parentPath = new File(this.getRemotePath()).getParent();
  381. return parentPath.endsWith(PATH_SEPARATOR) ? parentPath : parentPath + PATH_SEPARATOR;
  382. }
  383. @Override
  384. public int describeContents() {
  385. return super.hashCode();
  386. }
  387. @Override
  388. public int compareTo(@NonNull OCFile another) {
  389. if (isFolder() && another.isFolder()) {
  390. return new AlphanumComparator().compare(this, another);
  391. } else if (isFolder()) {
  392. return -1;
  393. } else if (another.isFolder()) {
  394. return 1;
  395. }
  396. return new AlphanumComparator().compare(this, another);
  397. }
  398. @Override
  399. public boolean equals(Object o) {
  400. if (this == o) {
  401. return true;
  402. }
  403. if (o == null || getClass() != o.getClass()) {
  404. return false;
  405. }
  406. OCFile ocFile = (OCFile) o;
  407. return fileId == ocFile.fileId && parentId == ocFile.parentId;
  408. }
  409. @Override
  410. public int hashCode() {
  411. return 31 * (int) (fileId ^ (fileId >>> 32)) + (int) (parentId ^ (parentId >>> 32));
  412. }
  413. @NonNull
  414. @Override
  415. public String toString() {
  416. String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, " +
  417. "parentId=%s, etag=%s, favourite=%s]";
  418. return String.format(asString, fileId, getFileName(), mimeType, isDown(), localPath, remotePath, parentId,
  419. etag, favorite);
  420. }
  421. public void setEtag(String etag) {
  422. this.etag = etag != null ? etag : "";
  423. }
  424. public void setEtagOnServer(String etag) {
  425. this.etagOnServer = etag != null ? etag : "";
  426. }
  427. public long getLocalModificationTimestamp() {
  428. if (!TextUtils.isEmpty(localPath)) {
  429. File f = new File(localPath);
  430. return f.lastModified();
  431. }
  432. return 0;
  433. }
  434. /**
  435. * @return 'True' if the file is hidden
  436. */
  437. public boolean isHidden() {
  438. return !TextUtils.isEmpty(getFileName()) && getFileName().charAt(0) == '.';
  439. }
  440. /**
  441. * The unique fileId for the file within the instance
  442. *
  443. * @return file fileId, unique within the instance
  444. */
  445. public String getLocalId() {
  446. return getRemoteId().substring(0, 8).replaceAll("^0*", "");
  447. }
  448. public boolean isInConflict() {
  449. return !TextUtils.isEmpty(etagInConflict);
  450. }
  451. public boolean isSharedWithMe() {
  452. String permissions = getPermissions();
  453. return permissions != null && permissions.contains(PERMISSION_SHARED_WITH_ME);
  454. }
  455. public boolean canReshare() {
  456. String permissions = getPermissions();
  457. return permissions != null && permissions.contains(PERMISSION_CAN_RESHARE);
  458. }
  459. public boolean canWrite() {
  460. String permissions = getPermissions();
  461. return permissions != null && permissions.contains(PERMISSION_CAN_WRITE);
  462. }
  463. public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
  464. @Override
  465. public OCFile createFromParcel(Parcel source) {
  466. return new OCFile(source);
  467. }
  468. @Override
  469. public OCFile[] newArray(int size) {
  470. return new OCFile[size];
  471. }
  472. };
  473. }