+ * Checks the existence of the folder where the current file will be uploaded both in the remote server
+ * and in the local database.
+ *
+ * If the upload is set to enforce the creation of the folder, the method tries to create it both remote
+ * and locally.
+ *
+ * @param pathToGrant Full remote path whose existence will be granted.
+ * @return An {@link OCFile} instance corresponding to the folder where the file will be uploaded.
+ */
+ private RemoteOperationResult grantFolderExistence(String pathToGrant) {
+ RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, this, false);
+ RemoteOperationResult result = operation.execute(mUploadClient);
+ if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mCurrentUpload.isRemoteFolderToBeCreated()) {
+ operation = new CreateFolderOperation( pathToGrant,
+ true,
+ mStorageManager );
+ result = operation.execute(mUploadClient);
+ }
+ if (result.isSuccess()) {
+ OCFile parentDir = mStorageManager.getFileByPath(pathToGrant);
+ if (parentDir == null) {
+ parentDir = createLocalFolder(pathToGrant);
+ }
+ if (parentDir != null) {
+ result = new RemoteOperationResult(ResultCode.OK);
+ } else {
+ result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
+ }
+ }
+ return result;
+ }
+
+
+ private OCFile createLocalFolder(String remotePath) {
+ String parentPath = new File(remotePath).getParent();
+ parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
+ OCFile parent = mStorageManager.getFileByPath(parentPath);
+ if (parent == null) {
+ parent = createLocalFolder(parentPath);
+ }
+ if (parent != null) {
+ OCFile createdFolder = new OCFile(remotePath);
+ createdFolder.setMimetype("DIR");
+ createdFolder.setParentId(parent.getFileId());
+ mStorageManager.saveFile(createdFolder);
+ return createdFolder;
+ }
+ return null;
+ }
+
+
+ /**