Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 761ad4c2 authored by Álvaro Brey's avatar Álvaro Brey
Browse files

feat(download): Use specific code and exception when download fails to create local file

parent b610a4a1
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import com.nextcloud.common.OkHttpMethodBase;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.files.CreateLocalFileException;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.lib.resources.notifications.models.PushResponse;

@@ -302,6 +303,8 @@ public class RemoteOperationResult<T extends Object> implements Serializable {
            }
        } else if (e instanceof FileNotFoundException) {
            mCode = ResultCode.LOCAL_FILE_NOT_FOUND;
        } else if (e instanceof CreateLocalFileException) {
            mCode = ResultCode.CANNOT_CREATE_FILE;
        } else {
            mCode = ResultCode.UNKNOWN_ERROR;
        }
+33 −0
Original line number Diff line number Diff line
/* Nextcloud Android Library is available under MIT license
 *
 *   @author Álvaro Brey
 *   Copyright (C) 2022 Álvaro Brey
 *   Copyright (C) 2022 Nextcloud GmbH
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package com.owncloud.android.lib.resources.files

class CreateLocalFileException(val path: String, cause: Throwable) : Exception(cause) {

    override val message: String = "File could not be created"
}
+17 −12
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ public class DownloadFileRemoteOperation extends RemoteOperation {
    }


    private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException {
    private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException {
        int status;
        boolean savedFile = false;
        getMethod = new GetMethod(client.getFilesDavUri(remotePath));
@@ -110,7 +110,12 @@ public class DownloadFileRemoteOperation extends RemoteOperation {
        try {
            status = client.executeMethod(getMethod);
            if (isSuccess(status)) {
                try {
                    targetFile.createNewFile();
                } catch (IOException | SecurityException ex) {
                    Log_OC.e(TAG, "Error creating file " + targetFile.getAbsolutePath(), ex);
                    throw new CreateLocalFileException(targetFile.getPath(), ex);
                }
                BufferedInputStream bis = new BufferedInputStream(getMethod.getResponseBodyAsStream());
                fos = new FileOutputStream(targetFile);
                long transferred = 0;