UploadFileOperation.java 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * @author Chris Narkiewicz
  6. * Copyright (C) 2016 ownCloud GmbH.
  7. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  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. package com.owncloud.android.operations;
  22. import android.annotation.SuppressLint;
  23. import android.content.Context;
  24. import android.net.Uri;
  25. import android.text.TextUtils;
  26. import com.nextcloud.client.account.User;
  27. import com.nextcloud.client.device.BatteryStatus;
  28. import com.nextcloud.client.device.PowerManagementService;
  29. import com.nextcloud.client.jobs.upload.FileUploadWorker;
  30. import com.nextcloud.client.network.Connectivity;
  31. import com.nextcloud.client.network.ConnectivityService;
  32. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  33. import com.owncloud.android.datamodel.ArbitraryDataProviderImpl;
  34. import com.owncloud.android.datamodel.FileDataStorageManager;
  35. import com.owncloud.android.datamodel.OCFile;
  36. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  37. import com.owncloud.android.datamodel.UploadsStorageManager;
  38. import com.owncloud.android.datamodel.e2e.v1.decrypted.Data;
  39. import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFile;
  40. import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedFolderMetadataFileV1;
  41. import com.owncloud.android.datamodel.e2e.v1.decrypted.DecryptedMetadata;
  42. import com.owncloud.android.datamodel.e2e.v1.encrypted.EncryptedFile;
  43. import com.owncloud.android.datamodel.e2e.v1.encrypted.EncryptedFolderMetadataFileV1;
  44. import com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFolderMetadataFile;
  45. import com.owncloud.android.db.OCUpload;
  46. import com.owncloud.android.files.services.NameCollisionPolicy;
  47. import com.owncloud.android.lib.common.OwnCloudClient;
  48. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  49. import com.owncloud.android.lib.common.network.ProgressiveDataTransfer;
  50. import com.owncloud.android.lib.common.operations.OperationCancelledException;
  51. import com.owncloud.android.lib.common.operations.RemoteOperation;
  52. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  53. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  54. import com.owncloud.android.lib.common.utils.Log_OC;
  55. import com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation;
  56. import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
  57. import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
  58. import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
  59. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  60. import com.owncloud.android.lib.resources.status.E2EVersion;
  61. import com.owncloud.android.operations.common.SyncOperation;
  62. import com.owncloud.android.utils.EncryptionUtils;
  63. import com.owncloud.android.utils.EncryptionUtilsV2;
  64. import com.owncloud.android.utils.FileStorageUtils;
  65. import com.owncloud.android.utils.FileUtil;
  66. import com.owncloud.android.utils.MimeType;
  67. import com.owncloud.android.utils.MimeTypeUtil;
  68. import com.owncloud.android.utils.UriUtils;
  69. import com.owncloud.android.utils.theme.CapabilityUtils;
  70. import org.apache.commons.httpclient.HttpStatus;
  71. import org.apache.commons.httpclient.methods.RequestEntity;
  72. import org.lukhnos.nnio.file.Files;
  73. import org.lukhnos.nnio.file.Paths;
  74. import java.io.File;
  75. import java.io.FileInputStream;
  76. import java.io.FileNotFoundException;
  77. import java.io.FileOutputStream;
  78. import java.io.IOException;
  79. import java.io.InputStream;
  80. import java.io.OutputStream;
  81. import java.io.RandomAccessFile;
  82. import java.nio.channels.FileChannel;
  83. import java.nio.channels.FileLock;
  84. import java.nio.channels.OverlappingFileLockException;
  85. import java.util.ArrayList;
  86. import java.util.HashMap;
  87. import java.util.HashSet;
  88. import java.util.List;
  89. import java.util.Set;
  90. import java.util.concurrent.atomic.AtomicBoolean;
  91. import androidx.annotation.CheckResult;
  92. import androidx.annotation.Nullable;
  93. /**
  94. * Operation performing the update in the ownCloud server of a file that was modified locally.
  95. */
  96. public class UploadFileOperation extends SyncOperation {
  97. private static final String TAG = UploadFileOperation.class.getSimpleName();
  98. public static final int CREATED_BY_USER = 0;
  99. public static final int CREATED_AS_INSTANT_PICTURE = 1;
  100. public static final int CREATED_AS_INSTANT_VIDEO = 2;
  101. /**
  102. * OCFile which is to be uploaded.
  103. */
  104. private OCFile mFile;
  105. /**
  106. * Original OCFile which is to be uploaded in case file had to be renamed (if nameCollisionPolicy==RENAME and remote
  107. * file already exists).
  108. */
  109. private OCFile mOldFile;
  110. private String mRemotePath;
  111. private String mFolderUnlockToken;
  112. private boolean mRemoteFolderToBeCreated;
  113. private NameCollisionPolicy mNameCollisionPolicy;
  114. private int mLocalBehaviour;
  115. private int mCreatedBy;
  116. private boolean mOnWifiOnly;
  117. private boolean mWhileChargingOnly;
  118. private boolean mIgnoringPowerSaveMode;
  119. private final boolean mDisableRetries;
  120. private boolean mWasRenamed;
  121. private long mOCUploadId;
  122. /**
  123. * Local path to file which is to be uploaded (before any possible renaming or moving).
  124. */
  125. private String mOriginalStoragePath;
  126. private final Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
  127. private OnRenameListener mRenameUploadListener;
  128. private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
  129. private final AtomicBoolean mUploadStarted = new AtomicBoolean(false);
  130. private Context mContext;
  131. private UploadFileRemoteOperation mUploadOperation;
  132. private RequestEntity mEntity;
  133. private final User user;
  134. private final OCUpload mUpload;
  135. private final UploadsStorageManager uploadsStorageManager;
  136. private final ConnectivityService connectivityService;
  137. private final PowerManagementService powerManagementService;
  138. private boolean encryptedAncestor;
  139. public static OCFile obtainNewOCFileToUpload(String remotePath, String localPath, String mimeType) {
  140. OCFile newFile = new OCFile(remotePath);
  141. newFile.setStoragePath(localPath);
  142. newFile.setLastSyncDateForProperties(0);
  143. newFile.setLastSyncDateForData(0);
  144. // size
  145. if (!TextUtils.isEmpty(localPath)) {
  146. File localFile = new File(localPath);
  147. newFile.setFileLength(localFile.length());
  148. newFile.setLastSyncDateForData(localFile.lastModified());
  149. } // don't worry about not assigning size, the problems with localPath
  150. // are checked when the UploadFileOperation instance is created
  151. // MIME type
  152. if (TextUtils.isEmpty(mimeType)) {
  153. newFile.setMimeType(MimeTypeUtil.getBestMimeTypeByFilename(localPath));
  154. } else {
  155. newFile.setMimeType(mimeType);
  156. }
  157. return newFile;
  158. }
  159. public UploadFileOperation(UploadsStorageManager uploadsStorageManager,
  160. ConnectivityService connectivityService,
  161. PowerManagementService powerManagementService,
  162. User user,
  163. OCFile file,
  164. OCUpload upload,
  165. NameCollisionPolicy nameCollisionPolicy,
  166. int localBehaviour,
  167. Context context,
  168. boolean onWifiOnly,
  169. boolean whileChargingOnly,
  170. FileDataStorageManager storageManager) {
  171. this(uploadsStorageManager,
  172. connectivityService,
  173. powerManagementService,
  174. user,
  175. file,
  176. upload,
  177. nameCollisionPolicy,
  178. localBehaviour,
  179. context,
  180. onWifiOnly,
  181. whileChargingOnly,
  182. true,
  183. storageManager);
  184. }
  185. public UploadFileOperation(UploadsStorageManager uploadsStorageManager,
  186. ConnectivityService connectivityService,
  187. PowerManagementService powerManagementService,
  188. User user,
  189. OCFile file,
  190. OCUpload upload,
  191. NameCollisionPolicy nameCollisionPolicy,
  192. int localBehaviour,
  193. Context context,
  194. boolean onWifiOnly,
  195. boolean whileChargingOnly,
  196. boolean disableRetries,
  197. FileDataStorageManager storageManager) {
  198. super(storageManager);
  199. if (upload == null) {
  200. throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation");
  201. }
  202. if (TextUtils.isEmpty(upload.getLocalPath())) {
  203. throw new IllegalArgumentException(
  204. "Illegal file in UploadFileOperation; storage path invalid: "
  205. + upload.getLocalPath());
  206. }
  207. this.uploadsStorageManager = uploadsStorageManager;
  208. this.connectivityService = connectivityService;
  209. this.powerManagementService = powerManagementService;
  210. this.user = user;
  211. mUpload = upload;
  212. if (file == null) {
  213. mFile = obtainNewOCFileToUpload(
  214. upload.getRemotePath(),
  215. upload.getLocalPath(),
  216. upload.getMimeType()
  217. );
  218. } else {
  219. mFile = file;
  220. }
  221. mOnWifiOnly = onWifiOnly;
  222. mWhileChargingOnly = whileChargingOnly;
  223. mRemotePath = upload.getRemotePath();
  224. mNameCollisionPolicy = nameCollisionPolicy;
  225. mLocalBehaviour = localBehaviour;
  226. mOriginalStoragePath = mFile.getStoragePath();
  227. mContext = context;
  228. mOCUploadId = upload.getUploadId();
  229. mCreatedBy = upload.getCreatedBy();
  230. mRemoteFolderToBeCreated = upload.isCreateRemoteFolder();
  231. // Ignore power save mode only if user explicitly created this upload
  232. mIgnoringPowerSaveMode = mCreatedBy == CREATED_BY_USER;
  233. mFolderUnlockToken = upload.getFolderUnlockToken();
  234. mDisableRetries = disableRetries;
  235. }
  236. public boolean isWifiRequired() {
  237. return mOnWifiOnly;
  238. }
  239. public boolean isChargingRequired() {
  240. return mWhileChargingOnly;
  241. }
  242. public boolean isIgnoringPowerSaveMode() {
  243. return mIgnoringPowerSaveMode;
  244. }
  245. public User getUser() {
  246. return user;
  247. }
  248. public String getFileName() {
  249. return (mFile != null) ? mFile.getFileName() : null;
  250. }
  251. public OCFile getFile() {
  252. return mFile;
  253. }
  254. /**
  255. * If remote file was renamed, return original OCFile which was uploaded. Is null is file was not renamed.
  256. */
  257. @Nullable
  258. public OCFile getOldFile() {
  259. return mOldFile;
  260. }
  261. public String getOriginalStoragePath() {
  262. return mOriginalStoragePath;
  263. }
  264. public String getStoragePath() {
  265. return mFile.getStoragePath();
  266. }
  267. public String getRemotePath() {
  268. return mFile.getRemotePath();
  269. }
  270. public String getDecryptedRemotePath() {
  271. return mFile.getDecryptedRemotePath();
  272. }
  273. public String getMimeType() {
  274. return mFile.getMimeType();
  275. }
  276. public int getLocalBehaviour() {
  277. return mLocalBehaviour;
  278. }
  279. public UploadFileOperation setRemoteFolderToBeCreated() {
  280. mRemoteFolderToBeCreated = true;
  281. return this;
  282. }
  283. public boolean wasRenamed() {
  284. return mWasRenamed;
  285. }
  286. public void setCreatedBy(int createdBy) {
  287. mCreatedBy = createdBy;
  288. if (createdBy < CREATED_BY_USER || CREATED_AS_INSTANT_VIDEO < createdBy) {
  289. mCreatedBy = CREATED_BY_USER;
  290. }
  291. }
  292. public int getCreatedBy() {
  293. return mCreatedBy;
  294. }
  295. public boolean isInstantPicture() {
  296. return mCreatedBy == CREATED_AS_INSTANT_PICTURE;
  297. }
  298. public boolean isInstantVideo() {
  299. return mCreatedBy == CREATED_AS_INSTANT_VIDEO;
  300. }
  301. public void setOCUploadId(long id) {
  302. mOCUploadId = id;
  303. }
  304. public long getOCUploadId() {
  305. return mOCUploadId;
  306. }
  307. public Set<OnDatatransferProgressListener> getDataTransferListeners() {
  308. return mDataTransferListeners;
  309. }
  310. public void addDataTransferProgressListener(OnDatatransferProgressListener listener) {
  311. synchronized (mDataTransferListeners) {
  312. mDataTransferListeners.add(listener);
  313. }
  314. if (mEntity != null) {
  315. ((ProgressiveDataTransfer) mEntity).addDataTransferProgressListener(listener);
  316. }
  317. if (mUploadOperation != null) {
  318. mUploadOperation.addDataTransferProgressListener(listener);
  319. }
  320. }
  321. public void removeDataTransferProgressListener(OnDatatransferProgressListener listener) {
  322. synchronized (mDataTransferListeners) {
  323. mDataTransferListeners.remove(listener);
  324. }
  325. if (mEntity != null) {
  326. ((ProgressiveDataTransfer) mEntity).removeDataTransferProgressListener(listener);
  327. }
  328. if (mUploadOperation != null) {
  329. mUploadOperation.removeDataTransferProgressListener(listener);
  330. }
  331. }
  332. public UploadFileOperation addRenameUploadListener(OnRenameListener listener) {
  333. mRenameUploadListener = listener;
  334. return this;
  335. }
  336. public Context getContext() {
  337. return mContext;
  338. }
  339. @Override
  340. @SuppressWarnings("PMD.AvoidDuplicateLiterals")
  341. protected RemoteOperationResult run(OwnCloudClient client) {
  342. mCancellationRequested.set(false);
  343. mUploadStarted.set(true);
  344. updateSize(0);
  345. String remoteParentPath = new File(getRemotePath()).getParent();
  346. remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
  347. remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
  348. OCFile parent = getStorageManager().getFileByPath(remoteParentPath);
  349. // in case of a fresh upload with subfolder, where parent does not exist yet
  350. if (parent == null && (mFolderUnlockToken == null || mFolderUnlockToken.isEmpty())) {
  351. // try to create folder
  352. RemoteOperationResult result = grantFolderExistence(remoteParentPath, client);
  353. if (!result.isSuccess()) {
  354. return result;
  355. }
  356. parent = getStorageManager().getFileByPath(remoteParentPath);
  357. if (parent == null) {
  358. return new RemoteOperationResult(false, "Parent folder not found", HttpStatus.SC_NOT_FOUND);
  359. }
  360. }
  361. // parent file is not null anymore:
  362. // - it was created on fresh upload or
  363. // - resume of encrypted upload, then parent file exists already as unlock is only for direct parent
  364. mFile.setParentId(parent.getFileId());
  365. // check if any parent is encrypted
  366. encryptedAncestor = FileStorageUtils.checkEncryptionStatus(parent, getStorageManager());
  367. mFile.setEncrypted(encryptedAncestor);
  368. if (encryptedAncestor) {
  369. Log_OC.d(TAG, "encrypted upload");
  370. return encryptedUpload(client, parent);
  371. } else {
  372. Log_OC.d(TAG, "normal upload");
  373. return normalUpload(client);
  374. }
  375. }
  376. @SuppressLint("AndroidLintUseSparseArrays") // gson cannot handle sparse arrays easily, therefore use hashmap
  377. private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile parentFile) {
  378. RemoteOperationResult result = null;
  379. File temporalFile = null;
  380. File originalFile = new File(mOriginalStoragePath);
  381. File expectedFile = null;
  382. FileLock fileLock = null;
  383. long size;
  384. boolean metadataExists = false;
  385. String token = null;
  386. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(getContext());
  387. String publicKey = arbitraryDataProvider.getValue(user.getAccountName(), EncryptionUtils.PUBLIC_KEY);
  388. try {
  389. // check conditions
  390. result = checkConditions(originalFile);
  391. if (result != null) {
  392. return result;
  393. }
  394. /***** E2E *****/
  395. // Only on V2+: whenever we change something, increase counter
  396. long counter = -1;
  397. if (CapabilityUtils.getCapability(mContext).getEndToEndEncryptionApiVersion().compareTo(E2EVersion.V2_0) >= 0) {
  398. counter = parentFile.getE2eCounter() + 1;
  399. }
  400. // we might have an old token from interrupted upload
  401. if (mFolderUnlockToken != null && !mFolderUnlockToken.isEmpty()) {
  402. token = mFolderUnlockToken;
  403. } else {
  404. token = EncryptionUtils.lockFolder(parentFile, client, counter);
  405. // immediately store it
  406. mUpload.setFolderUnlockToken(token);
  407. uploadsStorageManager.updateUpload(mUpload);
  408. }
  409. // Update metadata
  410. EncryptionUtilsV2 encryptionUtilsV2 = new EncryptionUtilsV2();
  411. // kotlin.Pair<Boolean, DecryptedFolderMetadataFile> metadataPair =
  412. // encryptionUtilsV2.retrieveMetadata(parentFile,
  413. // client,
  414. // user,
  415. // mContext);
  416. Object object = EncryptionUtils.downloadFolderMetadata(parentFile, client, mContext, user);
  417. if (CapabilityUtils.getCapability(mContext).getEndToEndEncryptionApiVersion().compareTo(E2EVersion.V2_0) >= 0) {
  418. if (object == null) {
  419. // TODO return error
  420. return new RemoteOperationResult(new IllegalStateException("Metadata does not exist"));
  421. } else {
  422. metadataExists = true;
  423. }
  424. } else {
  425. // v1 is allowed to be null, thus create it
  426. DecryptedFolderMetadataFileV1 metadata = new DecryptedFolderMetadataFileV1();
  427. metadata.setMetadata(new DecryptedMetadata());
  428. metadata.getMetadata().setVersion(1.2);
  429. metadata.getMetadata().setMetadataKeys(new HashMap<>());
  430. String metadataKey = EncryptionUtils.encodeBytesToBase64String(EncryptionUtils.generateKey());
  431. String encryptedMetadataKey = EncryptionUtils.encryptStringAsymmetric(metadataKey, publicKey);
  432. metadata.getMetadata().setMetadataKey(encryptedMetadataKey);
  433. object = metadata;
  434. metadataExists = false;
  435. }
  436. // todo fail if no metadata
  437. // metadataExists = metadataPair.getFirst();
  438. // DecryptedFolderMetadataFile metadata = metadataPair.getSecond();
  439. // TODO E2E: check counter: must be less than our counter, check rest: signature, etc
  440. /**** E2E *****/
  441. // check name collision
  442. List<String> fileNames = new ArrayList<>();
  443. if (object instanceof DecryptedFolderMetadataFileV1 metadata) {
  444. for (DecryptedFile file : metadata.getFiles().values()) {
  445. fileNames.add(file.getEncrypted().getFilename());
  446. }
  447. } else {
  448. for (com.owncloud.android.datamodel.e2e.v2.decrypted.DecryptedFile file :
  449. ((DecryptedFolderMetadataFile) object).getMetadata().getFiles().values()) {
  450. fileNames.add(file.getFilename());
  451. }
  452. }
  453. RemoteOperationResult collisionResult = checkNameCollision(client, fileNames, parentFile.isEncrypted());
  454. if (collisionResult != null) {
  455. result = collisionResult;
  456. return collisionResult;
  457. }
  458. mFile.setDecryptedRemotePath(parentFile.getDecryptedRemotePath() + originalFile.getName());
  459. String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
  460. expectedFile = new File(expectedPath);
  461. result = copyFile(originalFile, expectedPath);
  462. if (!result.isSuccess()) {
  463. return result;
  464. }
  465. // Get the last modification date of the file from the file system
  466. long lastModifiedTimestamp = originalFile.lastModified() / 1000;
  467. Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
  468. /***** E2E *****/
  469. // Key, always generate new one
  470. byte[] key = EncryptionUtils.generateKey();
  471. // IV, always generate new one
  472. byte[] iv = EncryptionUtils.randomBytes(EncryptionUtils.ivLength);
  473. EncryptedFile encryptedFile = EncryptionUtils.encryptFile(mFile, key, iv);
  474. // new random file name, check if it exists in metadata
  475. String encryptedFileName = EncryptionUtils.generateUid();
  476. if (object instanceof DecryptedFolderMetadataFileV1 metadata) {
  477. while (metadata.getFiles().get(encryptedFileName) != null) {
  478. encryptedFileName = EncryptionUtils.generateUid();
  479. }
  480. } else {
  481. while (((DecryptedFolderMetadataFile) object).getMetadata().getFiles().get(encryptedFileName) != null) {
  482. encryptedFileName = EncryptionUtils.generateUid();
  483. }
  484. }
  485. File encryptedTempFile = File.createTempFile("encFile", encryptedFileName);
  486. FileOutputStream fileOutputStream = new FileOutputStream(encryptedTempFile);
  487. fileOutputStream.write(encryptedFile.getEncryptedBytes());
  488. fileOutputStream.close();
  489. /***** E2E *****/
  490. FileChannel channel = null;
  491. try {
  492. channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
  493. fileLock = channel.tryLock();
  494. } catch (FileNotFoundException e) {
  495. // this basically means that the file is on SD card
  496. // try to copy file to temporary dir if it doesn't exist
  497. String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
  498. mFile.getRemotePath();
  499. mFile.setStoragePath(temporalPath);
  500. temporalFile = new File(temporalPath);
  501. Files.deleteIfExists(Paths.get(temporalPath));
  502. result = copy(originalFile, temporalFile);
  503. if (result.isSuccess()) {
  504. if (temporalFile.length() == originalFile.length()) {
  505. channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
  506. fileLock = channel.tryLock();
  507. } else {
  508. result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
  509. }
  510. }
  511. }
  512. try {
  513. size = channel.size();
  514. } catch (IOException e1) {
  515. size = new File(mFile.getStoragePath()).length();
  516. }
  517. updateSize(size);
  518. /// perform the upload
  519. if (size > ChunkedFileUploadRemoteOperation.CHUNK_SIZE_MOBILE) {
  520. boolean onWifiConnection = connectivityService.getConnectivity().isWifi();
  521. mUploadOperation = new ChunkedFileUploadRemoteOperation(encryptedTempFile.getAbsolutePath(),
  522. mFile.getParentRemotePath() + encryptedFileName,
  523. mFile.getMimeType(),
  524. mFile.getEtagInConflict(),
  525. lastModifiedTimestamp,
  526. onWifiConnection,
  527. token,
  528. creationTimestamp,
  529. mDisableRetries
  530. );
  531. } else {
  532. mUploadOperation = new UploadFileRemoteOperation(encryptedTempFile.getAbsolutePath(),
  533. mFile.getParentRemotePath() + encryptedFileName,
  534. mFile.getMimeType(),
  535. mFile.getEtagInConflict(),
  536. lastModifiedTimestamp,
  537. creationTimestamp,
  538. token,
  539. mDisableRetries
  540. );
  541. }
  542. for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners) {
  543. mUploadOperation.addDataTransferProgressListener(mDataTransferListener);
  544. }
  545. if (mCancellationRequested.get()) {
  546. throw new OperationCancelledException();
  547. }
  548. result = mUploadOperation.execute(client);
  549. /// move local temporal file or original file to its corresponding
  550. // location in the Nextcloud local folder
  551. if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
  552. result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
  553. }
  554. if (result.isSuccess()) {
  555. mFile.setDecryptedRemotePath(parentFile.getDecryptedRemotePath() + originalFile.getName());
  556. mFile.setRemotePath(parentFile.getRemotePath() + encryptedFileName);
  557. if (object instanceof DecryptedFolderMetadataFileV1 metadata) {
  558. // update metadata
  559. DecryptedFile decryptedFile = new DecryptedFile();
  560. Data data = new Data();
  561. data.setFilename(mFile.getDecryptedFileName());
  562. data.setMimetype(mFile.getMimeType());
  563. data.setKey(EncryptionUtils.encodeBytesToBase64String(key));
  564. decryptedFile.setEncrypted(data);
  565. decryptedFile.setInitializationVector(EncryptionUtils.encodeBytesToBase64String(iv));
  566. decryptedFile.setAuthenticationTag(encryptedFile.getAuthenticationTag());
  567. metadata.getFiles().put(encryptedFileName, decryptedFile);
  568. EncryptedFolderMetadataFileV1 encryptedFolderMetadata =
  569. EncryptionUtils.encryptFolderMetadata(metadata,
  570. publicKey,
  571. parentFile.getLocalId(),
  572. user,
  573. arbitraryDataProvider
  574. );
  575. String serializedFolderMetadata;
  576. // check if we need metadataKeys
  577. if (metadata.getMetadata().getMetadataKey() != null) {
  578. serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata, true);
  579. } else {
  580. serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
  581. }
  582. // upload metadata
  583. EncryptionUtils.uploadMetadata(parentFile,
  584. serializedFolderMetadata,
  585. token,
  586. client,
  587. metadataExists,
  588. E2EVersion.V1_2,
  589. "",
  590. arbitraryDataProvider,
  591. user);
  592. // unlock
  593. result = EncryptionUtils.unlockFolderV1(parentFile, client, token);
  594. if (result.isSuccess()) {
  595. token = null;
  596. }
  597. } else {
  598. DecryptedFolderMetadataFile metadata = (DecryptedFolderMetadataFile) object;
  599. encryptionUtilsV2.addFileToMetadata(
  600. encryptedFileName,
  601. mFile,
  602. iv,
  603. encryptedFile.getAuthenticationTag(),
  604. key,
  605. metadata,
  606. getStorageManager());
  607. // upload metadata
  608. encryptionUtilsV2.serializeAndUploadMetadata(parentFile,
  609. metadata,
  610. token,
  611. client,
  612. metadataExists,
  613. mContext,
  614. user,
  615. getStorageManager());
  616. // unlock
  617. result = EncryptionUtils.unlockFolder(parentFile, client, token);
  618. if (result.isSuccess()) {
  619. token = null;
  620. }
  621. }
  622. }
  623. } catch (FileNotFoundException e) {
  624. Log_OC.d(TAG, mFile.getStoragePath() + " not exists anymore");
  625. result = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
  626. } catch (OverlappingFileLockException e) {
  627. Log_OC.d(TAG, "Overlapping file lock exception");
  628. result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
  629. } catch (Exception e) {
  630. result = new RemoteOperationResult(e);
  631. } finally {
  632. mUploadStarted.set(false);
  633. if (fileLock != null) {
  634. try {
  635. fileLock.release();
  636. } catch (IOException e) {
  637. Log_OC.e(TAG, "Failed to unlock file with path " + mFile.getStoragePath());
  638. }
  639. }
  640. if (temporalFile != null && !originalFile.equals(temporalFile)) {
  641. temporalFile.delete();
  642. }
  643. if (result == null) {
  644. result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
  645. }
  646. logResult(result, mFile.getStoragePath(), mFile.getRemotePath());
  647. }
  648. if (result.isSuccess()) {
  649. handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
  650. } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
  651. getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
  652. }
  653. // unlock must be done always
  654. if (token != null) {
  655. RemoteOperationResult unlockFolderResult = EncryptionUtils.unlockFolder(parentFile,
  656. client,
  657. token);
  658. if (!unlockFolderResult.isSuccess()) {
  659. return unlockFolderResult;
  660. }
  661. }
  662. // delete temporal file
  663. if (temporalFile != null && temporalFile.exists() && !temporalFile.delete()) {
  664. Log_OC.e(TAG, "Could not delete temporal file " + temporalFile.getAbsolutePath());
  665. }
  666. return result;
  667. }
  668. private RemoteOperationResult checkConditions(File originalFile) {
  669. RemoteOperationResult remoteOperationResult = null;
  670. // check that connectivity conditions are met and delays the upload otherwise
  671. Connectivity connectivity = connectivityService.getConnectivity();
  672. if (mOnWifiOnly && (!connectivity.isWifi() || connectivity.isMetered())) {
  673. Log_OC.d(TAG, "Upload delayed until WiFi is available: " + getRemotePath());
  674. remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_FOR_WIFI);
  675. }
  676. // check if charging conditions are met and delays the upload otherwise
  677. final BatteryStatus battery = powerManagementService.getBattery();
  678. if (mWhileChargingOnly && !battery.isCharging()) {
  679. Log_OC.d(TAG, "Upload delayed until the device is charging: " + getRemotePath());
  680. remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_FOR_CHARGING);
  681. }
  682. // check that device is not in power save mode
  683. if (!mIgnoringPowerSaveMode && powerManagementService.isPowerSavingEnabled()) {
  684. Log_OC.d(TAG, "Upload delayed because device is in power save mode: " + getRemotePath());
  685. remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_IN_POWER_SAVE_MODE);
  686. }
  687. // check if the file continues existing before schedule the operation
  688. if (!originalFile.exists()) {
  689. Log_OC.d(TAG, mOriginalStoragePath + " not exists anymore");
  690. remoteOperationResult = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
  691. }
  692. // check that internet is not behind walled garden
  693. if (!connectivityService.getConnectivity().isConnected() || connectivityService.isInternetWalled()) {
  694. remoteOperationResult = new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION);
  695. }
  696. return remoteOperationResult;
  697. }
  698. private RemoteOperationResult normalUpload(OwnCloudClient client) {
  699. RemoteOperationResult result = null;
  700. File temporalFile = null;
  701. File originalFile = new File(mOriginalStoragePath);
  702. File expectedFile = null;
  703. FileLock fileLock = null;
  704. FileChannel channel = null;
  705. long size;
  706. try {
  707. // check conditions
  708. result = checkConditions(originalFile);
  709. if (result != null) {
  710. return result;
  711. }
  712. // check name collision
  713. RemoteOperationResult collisionResult = checkNameCollision(client, null, false);
  714. if (collisionResult != null) {
  715. result = collisionResult;
  716. return collisionResult;
  717. }
  718. String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
  719. expectedFile = new File(expectedPath);
  720. result = copyFile(originalFile, expectedPath);
  721. if (!result.isSuccess()) {
  722. return result;
  723. }
  724. // Get the last modification date of the file from the file system
  725. long lastModifiedTimestamp = originalFile.lastModified() / 1000;
  726. final Long creationTimestamp = FileUtil.getCreationTimestamp(originalFile);
  727. try {
  728. channel = new RandomAccessFile(mFile.getStoragePath(), "rw").getChannel();
  729. fileLock = channel.tryLock();
  730. } catch (FileNotFoundException e) {
  731. // this basically means that the file is on SD card
  732. // try to copy file to temporary dir if it doesn't exist
  733. String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
  734. mFile.getRemotePath();
  735. mFile.setStoragePath(temporalPath);
  736. temporalFile = new File(temporalPath);
  737. Files.deleteIfExists(Paths.get(temporalPath));
  738. result = copy(originalFile, temporalFile);
  739. if (result.isSuccess()) {
  740. if (temporalFile.length() == originalFile.length()) {
  741. channel = new RandomAccessFile(temporalFile.getAbsolutePath(), "rw").getChannel();
  742. fileLock = channel.tryLock();
  743. } else {
  744. result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
  745. }
  746. }
  747. }
  748. try {
  749. size = channel.size();
  750. } catch (Exception e1) {
  751. size = new File(mFile.getStoragePath()).length();
  752. }
  753. updateSize(size);
  754. // perform the upload
  755. if (size > ChunkedFileUploadRemoteOperation.CHUNK_SIZE_MOBILE) {
  756. boolean onWifiConnection = connectivityService.getConnectivity().isWifi();
  757. mUploadOperation = new ChunkedFileUploadRemoteOperation(mFile.getStoragePath(),
  758. mFile.getRemotePath(),
  759. mFile.getMimeType(),
  760. mFile.getEtagInConflict(),
  761. lastModifiedTimestamp,
  762. creationTimestamp,
  763. onWifiConnection,
  764. mDisableRetries);
  765. } else {
  766. mUploadOperation = new UploadFileRemoteOperation(mFile.getStoragePath(),
  767. mFile.getRemotePath(),
  768. mFile.getMimeType(),
  769. mFile.getEtagInConflict(),
  770. lastModifiedTimestamp,
  771. creationTimestamp,
  772. mDisableRetries);
  773. }
  774. for (OnDatatransferProgressListener mDataTransferListener : mDataTransferListeners) {
  775. mUploadOperation.addDataTransferProgressListener(mDataTransferListener);
  776. }
  777. if (mCancellationRequested.get()) {
  778. throw new OperationCancelledException();
  779. }
  780. if (result.isSuccess() && mUploadOperation != null) {
  781. result = mUploadOperation.execute(client);
  782. /// move local temporal file or original file to its corresponding
  783. // location in the Nextcloud local folder
  784. if (!result.isSuccess() && result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED) {
  785. result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
  786. }
  787. }
  788. } catch (FileNotFoundException e) {
  789. Log_OC.d(TAG, mOriginalStoragePath + " not exists anymore");
  790. result = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
  791. } catch (OverlappingFileLockException e) {
  792. Log_OC.d(TAG, "Overlapping file lock exception");
  793. result = new RemoteOperationResult(ResultCode.LOCK_FAILED);
  794. } catch (Exception e) {
  795. result = new RemoteOperationResult(e);
  796. } finally {
  797. mUploadStarted.set(false);
  798. if (fileLock != null) {
  799. try {
  800. fileLock.release();
  801. } catch (IOException e) {
  802. Log_OC.e(TAG, "Failed to unlock file with path " + mOriginalStoragePath);
  803. }
  804. }
  805. if (channel != null) {
  806. try {
  807. channel.close();
  808. } catch (IOException e) {
  809. Log_OC.w(TAG, "Failed to close file channel");
  810. }
  811. }
  812. if (temporalFile != null && !originalFile.equals(temporalFile)) {
  813. temporalFile.delete();
  814. }
  815. if (result == null) {
  816. result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
  817. }
  818. logResult(result, mOriginalStoragePath, mRemotePath);
  819. }
  820. if (result.isSuccess()) {
  821. handleSuccessfulUpload(temporalFile, expectedFile, originalFile, client);
  822. } else if (result.getCode() == ResultCode.SYNC_CONFLICT) {
  823. getStorageManager().saveConflict(mFile, mFile.getEtagInConflict());
  824. }
  825. // delete temporal file
  826. if (temporalFile != null && temporalFile.exists() && !temporalFile.delete()) {
  827. Log_OC.e(TAG, "Could not delete temporal file " + temporalFile.getAbsolutePath());
  828. }
  829. return result;
  830. }
  831. private void updateSize(long size) {
  832. OCUpload ocUpload = uploadsStorageManager.getUploadById(getOCUploadId());
  833. if (ocUpload != null) {
  834. ocUpload.setFileSize(size);
  835. uploadsStorageManager.updateUpload(ocUpload);
  836. }
  837. }
  838. private void logResult(RemoteOperationResult result, String sourcePath, String targetPath) {
  839. if (result.isSuccess()) {
  840. Log_OC.i(TAG, "Upload of " + sourcePath + " to " + targetPath + ": " + result.getLogMessage());
  841. } else {
  842. if (result.getException() != null) {
  843. if (result.isCancelled()) {
  844. Log_OC.w(TAG, "Upload of " + sourcePath + " to " + targetPath + ": "
  845. + result.getLogMessage());
  846. } else {
  847. Log_OC.e(TAG, "Upload of " + sourcePath + " to " + targetPath + ": "
  848. + result.getLogMessage(), result.getException());
  849. }
  850. } else {
  851. Log_OC.e(TAG, "Upload of " + sourcePath + " to " + targetPath + ": " + result.getLogMessage());
  852. }
  853. }
  854. }
  855. private RemoteOperationResult copyFile(File originalFile, String expectedPath) throws OperationCancelledException,
  856. IOException {
  857. if (mLocalBehaviour == FileUploadWorker.LOCAL_BEHAVIOUR_COPY && !mOriginalStoragePath.equals(expectedPath)) {
  858. String temporalPath = FileStorageUtils.getInternalTemporalPath(user.getAccountName(), mContext) +
  859. mFile.getRemotePath();
  860. mFile.setStoragePath(temporalPath);
  861. File temporalFile = new File(temporalPath);
  862. return copy(originalFile, temporalFile);
  863. }
  864. if (mCancellationRequested.get()) {
  865. throw new OperationCancelledException();
  866. }
  867. return new RemoteOperationResult(ResultCode.OK);
  868. }
  869. @CheckResult
  870. private RemoteOperationResult checkNameCollision(OwnCloudClient client,
  871. List<String> fileNames,
  872. boolean encrypted)
  873. throws OperationCancelledException {
  874. Log_OC.d(TAG, "Checking name collision in server");
  875. if (existsFile(client, mRemotePath, fileNames, encrypted)) {
  876. switch (mNameCollisionPolicy) {
  877. case CANCEL:
  878. Log_OC.d(TAG, "File exists; canceling");
  879. throw new OperationCancelledException();
  880. case RENAME:
  881. mRemotePath = getNewAvailableRemotePath(client, mRemotePath, fileNames, encrypted);
  882. mWasRenamed = true;
  883. createNewOCFile(mRemotePath);
  884. Log_OC.d(TAG, "File renamed as " + mRemotePath);
  885. if (mRenameUploadListener != null) {
  886. mRenameUploadListener.onRenameUpload();
  887. }
  888. break;
  889. case OVERWRITE:
  890. Log_OC.d(TAG, "Overwriting file");
  891. break;
  892. case ASK_USER:
  893. Log_OC.d(TAG, "Name collision; asking the user what to do");
  894. return new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
  895. }
  896. }
  897. if (mCancellationRequested.get()) {
  898. throw new OperationCancelledException();
  899. }
  900. return null;
  901. }
  902. private void handleSuccessfulUpload(File temporalFile,
  903. File expectedFile,
  904. File originalFile,
  905. OwnCloudClient client) {
  906. switch (mLocalBehaviour) {
  907. case FileUploadWorker.LOCAL_BEHAVIOUR_FORGET:
  908. default:
  909. mFile.setStoragePath("");
  910. saveUploadedFile(client);
  911. break;
  912. case FileUploadWorker.LOCAL_BEHAVIOUR_DELETE:
  913. originalFile.delete();
  914. mFile.setStoragePath("");
  915. getStorageManager().deleteFileInMediaScan(originalFile.getAbsolutePath());
  916. saveUploadedFile(client);
  917. break;
  918. case FileUploadWorker.LOCAL_BEHAVIOUR_COPY:
  919. if (temporalFile != null) {
  920. try {
  921. move(temporalFile, expectedFile);
  922. } catch (IOException e) {
  923. Log_OC.e(TAG, e.getMessage());
  924. }
  925. } else if (originalFile != null) {
  926. try {
  927. copy(originalFile, expectedFile);
  928. } catch (IOException e) {
  929. Log_OC.e(TAG, e.getMessage());
  930. }
  931. }
  932. mFile.setStoragePath(expectedFile.getAbsolutePath());
  933. saveUploadedFile(client);
  934. if (MimeTypeUtil.isMedia(mFile.getMimeType())) {
  935. FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath());
  936. }
  937. break;
  938. case FileUploadWorker.LOCAL_BEHAVIOUR_MOVE:
  939. String expectedPath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mFile);
  940. File newFile = new File(expectedPath);
  941. try {
  942. move(originalFile, newFile);
  943. } catch (IOException e) {
  944. Log_OC.e(TAG, "Error moving file", e);
  945. }
  946. getStorageManager().deleteFileInMediaScan(originalFile.getAbsolutePath());
  947. mFile.setStoragePath(newFile.getAbsolutePath());
  948. saveUploadedFile(client);
  949. if (MimeTypeUtil.isMedia(mFile.getMimeType())) {
  950. FileDataStorageManager.triggerMediaScan(newFile.getAbsolutePath());
  951. }
  952. break;
  953. }
  954. }
  955. /**
  956. * Checks the existence of the folder where the current file will be uploaded both in the remote server and in the
  957. * local database.
  958. * <p/>
  959. * If the upload is set to enforce the creation of the folder, the method tries to create it both remote and
  960. * locally.
  961. *
  962. * @param pathToGrant Full remote path whose existence will be granted.
  963. * @return An {@link OCFile} instance corresponding to the folder where the file will be uploaded.
  964. */
  965. private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
  966. RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, false);
  967. RemoteOperationResult result = operation.execute(client);
  968. if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
  969. SyncOperation syncOp = new CreateFolderOperation(pathToGrant, user, getContext(), getStorageManager());
  970. result = syncOp.execute(client);
  971. }
  972. if (result.isSuccess()) {
  973. OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
  974. if (parentDir == null) {
  975. parentDir = createLocalFolder(pathToGrant);
  976. }
  977. if (parentDir != null) {
  978. result = new RemoteOperationResult(ResultCode.OK);
  979. } else {
  980. result = new RemoteOperationResult(ResultCode.CANNOT_CREATE_FILE);
  981. }
  982. }
  983. return result;
  984. }
  985. private OCFile createLocalFolder(String remotePath) {
  986. String parentPath = new File(remotePath).getParent();
  987. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ?
  988. parentPath : parentPath + OCFile.PATH_SEPARATOR;
  989. OCFile parent = getStorageManager().getFileByPath(parentPath);
  990. if (parent == null) {
  991. parent = createLocalFolder(parentPath);
  992. }
  993. if (parent != null) {
  994. OCFile createdFolder = new OCFile(remotePath);
  995. createdFolder.setMimeType(MimeType.DIRECTORY);
  996. createdFolder.setParentId(parent.getFileId());
  997. getStorageManager().saveFile(createdFolder);
  998. return createdFolder;
  999. }
  1000. return null;
  1001. }
  1002. /**
  1003. * Create a new OCFile mFile with new remote path. This is required if nameCollisionPolicy==RENAME. New file is
  1004. * stored as mFile, original as mOldFile.
  1005. *
  1006. * @param newRemotePath new remote path
  1007. */
  1008. private void createNewOCFile(String newRemotePath) {
  1009. // a new OCFile instance must be created for a new remote path
  1010. OCFile newFile = new OCFile(newRemotePath);
  1011. newFile.setCreationTimestamp(mFile.getCreationTimestamp());
  1012. newFile.setFileLength(mFile.getFileLength());
  1013. newFile.setMimeType(mFile.getMimeType());
  1014. newFile.setModificationTimestamp(mFile.getModificationTimestamp());
  1015. newFile.setModificationTimestampAtLastSyncForData(
  1016. mFile.getModificationTimestampAtLastSyncForData()
  1017. );
  1018. newFile.setEtag(mFile.getEtag());
  1019. newFile.setLastSyncDateForProperties(mFile.getLastSyncDateForProperties());
  1020. newFile.setLastSyncDateForData(mFile.getLastSyncDateForData());
  1021. newFile.setStoragePath(mFile.getStoragePath());
  1022. newFile.setParentId(mFile.getParentId());
  1023. mOldFile = mFile;
  1024. mFile = newFile;
  1025. }
  1026. /**
  1027. * Returns a new and available (does not exists on the server) remotePath. This adds an incremental suffix.
  1028. *
  1029. * @param client OwnCloud client
  1030. * @param remotePath remote path of the file
  1031. * @param fileNames list of decrypted file names
  1032. * @return new remote path
  1033. */
  1034. private String getNewAvailableRemotePath(OwnCloudClient client,
  1035. String remotePath,
  1036. List<String> fileNames,
  1037. boolean encrypted) {
  1038. int extPos = remotePath.lastIndexOf('.');
  1039. String suffix;
  1040. String extension = "";
  1041. String remotePathWithoutExtension = "";
  1042. if (extPos >= 0) {
  1043. extension = remotePath.substring(extPos + 1);
  1044. remotePathWithoutExtension = remotePath.substring(0, extPos);
  1045. }
  1046. int count = 2;
  1047. boolean exists;
  1048. String newPath;
  1049. do {
  1050. suffix = " (" + count + ")";
  1051. newPath = extPos >= 0 ? remotePathWithoutExtension + suffix + "." + extension : remotePath + suffix;
  1052. exists = existsFile(client, newPath, fileNames, encrypted);
  1053. count++;
  1054. } while (exists);
  1055. return newPath;
  1056. }
  1057. private boolean existsFile(OwnCloudClient client,
  1058. String remotePath,
  1059. List<String> fileNames,
  1060. boolean encrypted) {
  1061. if (encrypted) {
  1062. String fileName = new File(remotePath).getName();
  1063. for (String name : fileNames) {
  1064. if (name.equalsIgnoreCase(fileName)) {
  1065. return true;
  1066. }
  1067. }
  1068. return false;
  1069. } else {
  1070. ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, false);
  1071. RemoteOperationResult result = existsOperation.execute(client);
  1072. return result.isSuccess();
  1073. }
  1074. }
  1075. /**
  1076. * Allows to cancel the actual upload operation. If actual upload operating is in progress it is cancelled, if
  1077. * upload preparation is being performed upload will not take place.
  1078. */
  1079. public void cancel(ResultCode cancellationReason) {
  1080. if (mUploadOperation == null) {
  1081. if (mUploadStarted.get()) {
  1082. Log_OC.d(TAG, "Cancelling upload during upload preparations.");
  1083. mCancellationRequested.set(true);
  1084. } else {
  1085. mCancellationRequested.set(true);
  1086. Log_OC.e(TAG, "No upload in progress. This should not happen.");
  1087. }
  1088. } else {
  1089. Log_OC.d(TAG, "Cancelling upload during actual upload operation.");
  1090. mUploadOperation.cancel(cancellationReason);
  1091. }
  1092. }
  1093. /**
  1094. * As soon as this method return true, upload can be cancel via cancel().
  1095. */
  1096. public boolean isUploadInProgress() {
  1097. return mUploadStarted.get();
  1098. }
  1099. /**
  1100. * TODO rewrite with homogeneous fail handling, remove dependency on {@link RemoteOperationResult},
  1101. * TODO use Exceptions instead
  1102. *
  1103. * @param sourceFile Source file to copy.
  1104. * @param targetFile Target location to copy the file.
  1105. * @return {@link RemoteOperationResult}
  1106. * @throws IOException exception if file cannot be accessed
  1107. */
  1108. private RemoteOperationResult copy(File sourceFile, File targetFile) throws IOException {
  1109. Log_OC.d(TAG, "Copying local file");
  1110. if (FileStorageUtils.getUsableSpace() < sourceFile.length()) {
  1111. return new RemoteOperationResult(ResultCode.LOCAL_STORAGE_FULL); // error when the file should be copied
  1112. } else {
  1113. Log_OC.d(TAG, "Creating temporal folder");
  1114. File temporalParent = targetFile.getParentFile();
  1115. if (!temporalParent.mkdirs() && !temporalParent.isDirectory()) {
  1116. return new RemoteOperationResult(ResultCode.CANNOT_CREATE_FILE);
  1117. }
  1118. Log_OC.d(TAG, "Creating temporal file");
  1119. if (!targetFile.createNewFile() && !targetFile.isFile()) {
  1120. return new RemoteOperationResult(ResultCode.CANNOT_CREATE_FILE);
  1121. }
  1122. Log_OC.d(TAG, "Copying file contents");
  1123. InputStream in = null;
  1124. OutputStream out = null;
  1125. try {
  1126. if (!mOriginalStoragePath.equals(targetFile.getAbsolutePath())) {
  1127. // In case document provider schema as 'content://'
  1128. if (mOriginalStoragePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {
  1129. Uri uri = Uri.parse(mOriginalStoragePath);
  1130. in = mContext.getContentResolver().openInputStream(uri);
  1131. } else {
  1132. in = new FileInputStream(sourceFile);
  1133. }
  1134. out = new FileOutputStream(targetFile);
  1135. int nRead;
  1136. byte[] buf = new byte[4096];
  1137. while (!mCancellationRequested.get() &&
  1138. (nRead = in.read(buf)) > -1) {
  1139. out.write(buf, 0, nRead);
  1140. }
  1141. out.flush();
  1142. } // else: weird but possible situation, nothing to copy
  1143. if (mCancellationRequested.get()) {
  1144. return new RemoteOperationResult(new OperationCancelledException());
  1145. }
  1146. } catch (Exception e) {
  1147. return new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED);
  1148. } finally {
  1149. try {
  1150. if (in != null) {
  1151. in.close();
  1152. }
  1153. } catch (Exception e) {
  1154. Log_OC.d(TAG, "Weird exception while closing input stream for " +
  1155. mOriginalStoragePath + " (ignoring)", e);
  1156. }
  1157. try {
  1158. if (out != null) {
  1159. out.close();
  1160. }
  1161. } catch (Exception e) {
  1162. Log_OC.d(TAG, "Weird exception while closing output stream for " +
  1163. targetFile.getAbsolutePath() + " (ignoring)", e);
  1164. }
  1165. }
  1166. }
  1167. return new RemoteOperationResult(ResultCode.OK);
  1168. }
  1169. /**
  1170. * TODO rewrite with homogeneous fail handling, remove dependency on {@link RemoteOperationResult},
  1171. * TODO use Exceptions instead
  1172. * TODO refactor both this and 'copy' in a single method
  1173. *
  1174. * @param sourceFile Source file to move.
  1175. * @param targetFile Target location to move the file.
  1176. * @throws IOException exception if file cannot be read/wrote
  1177. */
  1178. private void move(File sourceFile, File targetFile) throws IOException {
  1179. if (!targetFile.equals(sourceFile)) {
  1180. File expectedFolder = targetFile.getParentFile();
  1181. expectedFolder.mkdirs();
  1182. if (expectedFolder.isDirectory()) {
  1183. if (!sourceFile.renameTo(targetFile)) {
  1184. // try to copy and then delete
  1185. targetFile.createNewFile();
  1186. FileChannel inChannel = new FileInputStream(sourceFile).getChannel();
  1187. FileChannel outChannel = new FileOutputStream(targetFile).getChannel();
  1188. try {
  1189. inChannel.transferTo(0, inChannel.size(), outChannel);
  1190. sourceFile.delete();
  1191. } catch (Exception e) {
  1192. mFile.setStoragePath(""); // forget the local file
  1193. // by now, treat this as a success; the file was uploaded
  1194. // the best option could be show a warning message
  1195. } finally {
  1196. if (inChannel != null) {
  1197. inChannel.close();
  1198. }
  1199. if (outChannel != null) {
  1200. outChannel.close();
  1201. }
  1202. }
  1203. }
  1204. } else {
  1205. mFile.setStoragePath("");
  1206. }
  1207. }
  1208. }
  1209. /**
  1210. * Saves a OC File after a successful upload.
  1211. * <p>
  1212. * A PROPFIND is necessary to keep the props in the local database synchronized with the server, specially the
  1213. * modification time and Etag (where available)
  1214. */
  1215. private void saveUploadedFile(OwnCloudClient client) {
  1216. OCFile file = mFile;
  1217. if (file.fileExists()) {
  1218. file = getStorageManager().getFileById(file.getFileId());
  1219. }
  1220. if (file == null) {
  1221. // this can happen e.g. when the file gets deleted during upload
  1222. return;
  1223. }
  1224. long syncDate = System.currentTimeMillis();
  1225. file.setLastSyncDateForData(syncDate);
  1226. // new PROPFIND to keep data consistent with server
  1227. // in theory, should return the same we already have
  1228. // TODO from the appropriate OC server version, get data from last PUT response headers, instead
  1229. // TODO of a new PROPFIND; the latter may fail, specially for chunked uploads
  1230. String path;
  1231. if (encryptedAncestor) {
  1232. path = file.getParentRemotePath() + mFile.getEncryptedFileName();
  1233. } else {
  1234. path = getRemotePath();
  1235. }
  1236. ReadFileRemoteOperation operation = new ReadFileRemoteOperation(path);
  1237. RemoteOperationResult result = operation.execute(client);
  1238. if (result.isSuccess()) {
  1239. updateOCFile(file, (RemoteFile) result.getData().get(0));
  1240. file.setLastSyncDateForProperties(syncDate);
  1241. } else {
  1242. Log_OC.e(TAG, "Error reading properties of file after successful upload; this is gonna hurt...");
  1243. }
  1244. if (mWasRenamed) {
  1245. OCFile oldFile = getStorageManager().getFileByPath(mOldFile.getRemotePath());
  1246. if (oldFile != null) {
  1247. oldFile.setStoragePath(null);
  1248. getStorageManager().saveFile(oldFile);
  1249. getStorageManager().saveConflict(oldFile, null);
  1250. }
  1251. // else: it was just an automatic renaming due to a name
  1252. // coincidence; nothing else is needed, the storagePath is right
  1253. // in the instance returned by mCurrentUpload.getFile()
  1254. }
  1255. file.setUpdateThumbnailNeeded(true);
  1256. getStorageManager().saveFile(file);
  1257. getStorageManager().saveConflict(file, null);
  1258. if (MimeTypeUtil.isMedia(file.getMimeType())) {
  1259. FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
  1260. }
  1261. // generate new Thumbnail
  1262. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  1263. new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), user);
  1264. task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
  1265. }
  1266. private void updateOCFile(OCFile file, RemoteFile remoteFile) {
  1267. file.setCreationTimestamp(remoteFile.getCreationTimestamp());
  1268. file.setFileLength(remoteFile.getLength());
  1269. file.setMimeType(remoteFile.getMimeType());
  1270. file.setModificationTimestamp(remoteFile.getModifiedTimestamp());
  1271. file.setModificationTimestampAtLastSyncForData(remoteFile.getModifiedTimestamp());
  1272. file.setEtag(remoteFile.getEtag());
  1273. file.setRemoteId(remoteFile.getRemoteId());
  1274. file.setPermissions(remoteFile.getPermissions());
  1275. }
  1276. public interface OnRenameListener {
  1277. void onRenameUpload();
  1278. }
  1279. }