OCFile.java 16 KB

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