Search in sources :

Example 1 with ResultCode

use of com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode in project android by owncloud.

the class CopyAndUploadContentUrisTask method doInBackground.

/**
 * @param params    Params to execute the task; see
 *                  {@link #makeParamsToExecute(Account, Uri[], String[], int, ContentResolver)}
 *                  for further details.
 */
@Override
protected ResultCode doInBackground(Object[] params) {
    ResultCode result = ResultCode.UNKNOWN_ERROR;
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    String fullTempPath = null;
    Uri currentUri = null;
    try {
        Account account = (Account) params[0];
        Uri[] uris = (Uri[]) params[1];
        String[] remotePaths = (String[]) params[2];
        int behaviour = (int) params[3];
        ContentResolver leakedContentResolver = (ContentResolver) params[4];
        String currentRemotePath;
        for (int i = 0; i < uris.length; i++) {
            currentUri = uris[i];
            currentRemotePath = remotePaths[i];
            fullTempPath = FileStorageUtils.getTemporalPath(account.name) + currentRemotePath;
            inputStream = leakedContentResolver.openInputStream(currentUri);
            File cacheFile = new File(fullTempPath);
            File tempDir = cacheFile.getParentFile();
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            cacheFile.createNewFile();
            outputStream = new FileOutputStream(fullTempPath);
            byte[] buffer = new byte[4096];
            int count;
            while ((count = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }
            requestUpload(account, fullTempPath, currentRemotePath, behaviour, leakedContentResolver.getType(currentUri));
            fullTempPath = null;
        }
        result = ResultCode.OK;
    } catch (ArrayIndexOutOfBoundsException e) {
        Timber.e(e, "Wrong number of arguments received");
    } catch (ClassCastException e) {
        Timber.e(e, "Wrong parameter received");
    } catch (FileNotFoundException e) {
        Timber.e(e, "Could not find source file %s", currentUri);
        result = ResultCode.LOCAL_FILE_NOT_FOUND;
    } catch (SecurityException e) {
        Timber.e(e, "Not enough permissions to read source file %s", currentUri);
        result = ResultCode.FORBIDDEN;
    } catch (Exception e) {
        Timber.e(e, "Exception while copying " + currentUri + " to temporary file");
        result = ResultCode.LOCAL_STORAGE_NOT_COPIED;
        // clean
        if (fullTempPath != null) {
            File f = new File(fullTempPath);
            if (f.exists()) {
                if (!f.delete()) {
                    Timber.e("Could not delete temporary file %s", fullTempPath);
                }
            }
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                Timber.w("Ignoring exception of inputStream closure");
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                Timber.w("Ignoring exception of outStream closure");
            }
        }
    }
    return result;
}
Also used : Account(android.accounts.Account) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ResultCode(com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode)

Example 2 with ResultCode

use of com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode in project android by nextcloud.

the class CopyAndUploadContentUrisTask method doInBackground.

/**
 * @param params    Params to execute the task; see
 *                  {@link #makeParamsToExecute(User, Uri[], String[], int, ContentResolver)}
 *                  for further details.
 */
@Override
protected ResultCode doInBackground(Object[] params) {
    ResultCode result = ResultCode.UNKNOWN_ERROR;
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    String fullTempPath = null;
    Uri currentUri = null;
    try {
        User user = (User) params[0];
        Uri[] uris = (Uri[]) params[1];
        String[] remotePaths = (String[]) params[2];
        int behaviour = (Integer) params[3];
        ContentResolver leakedContentResolver = (ContentResolver) params[4];
        String currentRemotePath;
        for (int i = 0; i < uris.length; i++) {
            currentUri = uris[i];
            currentRemotePath = remotePaths[i];
            long lastModified = 0;
            try (Cursor cursor = leakedContentResolver.query(currentUri, null, null, null, null)) {
                if (cursor != null && cursor.moveToFirst()) {
                    // this check prevents a crash when last modification time is not available on certain phones
                    int columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
                    if (columnIndex >= 0) {
                        lastModified = cursor.getLong(columnIndex);
                    }
                }
            }
            fullTempPath = FileStorageUtils.getTemporalPath(user.getAccountName()) + currentRemotePath;
            inputStream = leakedContentResolver.openInputStream(currentUri);
            File cacheFile = new File(fullTempPath);
            File tempDir = cacheFile.getParentFile();
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
            cacheFile.createNewFile();
            outputStream = new FileOutputStream(fullTempPath);
            byte[] buffer = new byte[4096];
            int count;
            while ((count = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }
            if (lastModified != 0) {
                try {
                    if (!cacheFile.setLastModified(lastModified)) {
                        Log_OC.w(TAG, "Could not change mtime of cacheFile");
                    }
                } catch (SecurityException e) {
                    Log_OC.e(TAG, "Not enough permissions to change mtime of cacheFile", e);
                } catch (IllegalArgumentException e) {
                    Log_OC.e(TAG, "Could not change mtime of cacheFile, mtime is negativ: " + lastModified, e);
                }
            }
            requestUpload(user.toPlatformAccount(), fullTempPath, currentRemotePath, behaviour, leakedContentResolver.getType(currentUri));
            fullTempPath = null;
        }
        result = ResultCode.OK;
    } catch (ArrayIndexOutOfBoundsException e) {
        Log_OC.e(TAG, "Wrong number of arguments received ", e);
    } catch (ClassCastException e) {
        Log_OC.e(TAG, "Wrong parameter received ", e);
    } catch (FileNotFoundException e) {
        Log_OC.e(TAG, "Could not find source file " + currentUri, e);
        result = ResultCode.LOCAL_FILE_NOT_FOUND;
    } catch (SecurityException e) {
        Log_OC.e(TAG, "Not enough permissions to read source file " + currentUri, e);
        result = ResultCode.FORBIDDEN;
    } catch (Exception e) {
        Log_OC.e(TAG, "Exception while copying " + currentUri + " to temporary file", e);
        result = ResultCode.LOCAL_STORAGE_NOT_COPIED;
        // clean
        if (fullTempPath != null) {
            File f = new File(fullTempPath);
            if (f.exists() && !f.delete()) {
                Log_OC.e(TAG, "Could not delete temporary file " + fullTempPath);
            }
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                Log_OC.w(TAG, "Ignoring exception of inputStream closure");
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                Log_OC.w(TAG, "Ignoring exception of outStream closure");
            }
        }
    }
    return result;
}
Also used : User(com.nextcloud.client.account.User) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ResultCode(com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode)

Aggregations

ContentResolver (android.content.ContentResolver)2 Uri (android.net.Uri)2 ResultCode (com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 Account (android.accounts.Account)1 Cursor (android.database.Cursor)1 User (com.nextcloud.client.account.User)1