Search in sources :

Example 56 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class DocumentsStorageProvider method createFile.

private String createFile(Document targetFolder, String displayName, String mimeType) throws FileNotFoundException {
    User user = targetFolder.getUser();
    // create dummy file
    File tempDir = new File(FileStorageUtils.getTemporalPath(user.getAccountName()));
    if (!tempDir.exists() && !tempDir.mkdirs()) {
        throw new FileNotFoundException("Temp folder could not be created: " + tempDir.getAbsolutePath());
    }
    File emptyFile = new File(tempDir, displayName);
    if (emptyFile.exists() && !emptyFile.delete()) {
        throw new FileNotFoundException("Previous file could not be deleted");
    }
    try {
        if (!emptyFile.createNewFile()) {
            throw new FileNotFoundException("File could not be created");
        }
    } catch (IOException e) {
        throw getFileNotFoundExceptionWithCause("File could not be created", e);
    }
    String newFilePath = targetFolder.getRemotePath() + displayName;
    // FIXME we need to update the mimeType somewhere else as well
    // perform the upload, no need for chunked operation as we have a empty file
    OwnCloudClient client = targetFolder.getClient();
    RemoteOperationResult result = new UploadFileRemoteOperation(emptyFile.getAbsolutePath(), newFilePath, mimeType, "", String.valueOf(System.currentTimeMillis() / 1000), FileUtil.getCreationTimestamp(emptyFile), false).execute(client);
    if (!result.isSuccess()) {
        Log_OC.e(TAG, result.toString());
        throw new FileNotFoundException("Failed to upload document with path " + newFilePath);
    }
    Context context = getNonNullContext();
    RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), false, false, true, targetFolder.getStorageManager(), user, context).execute(client);
    if (!updateParent.isSuccess()) {
        Log_OC.e(TAG, updateParent.toString());
        throw new FileNotFoundException("Failed to create document with documentId " + targetFolder.getDocumentId());
    }
    Document newFile = new Document(targetFolder.getStorageManager(), newFilePath);
    context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
    return newFile.getDocumentId();
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) UploadFileRemoteOperation(com.owncloud.android.lib.resources.files.UploadFileRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 57 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class UserListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    UserListItem userListItem = values.get(position);
    if (userListItem != null) {
        // create account item
        if (UserListItem.TYPE_ACCOUNT == userListItem.getType()) {
            final User user = userListItem.getUser();
            AccountViewHolderItem item = (AccountViewHolderItem) holder;
            item.bind(user, userListItem.isEnabled(), this);
        } else // create add account action item
        if (UserListItem.TYPE_ACTION_ADD == userListItem.getType() && accountListAdapterListener != null) {
            ((AddAccountViewHolderItem) holder).bind(accountListAdapterListener);
        }
    }
}
Also used : User(com.nextcloud.client.account.User)

Example 58 with User

use of com.nextcloud.client.account.User 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)

Example 59 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class UploadListAdapter method openConflictActivity.

private void openConflictActivity(OCFile file, OCUpload upload) {
    file.setStoragePath(upload.getLocalPath());
    Context context = MainApp.getAppContext();
    Optional<User> user = accountManager.getUser(upload.getAccountName());
    if (user.isPresent()) {
        Intent intent = ConflictsResolveActivity.createIntent(file, user.get(), upload.getUploadId(), Intent.FLAG_ACTIVITY_NEW_TASK, context);
        context.startActivity(intent);
    }
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) Intent(android.content.Intent)

Example 60 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class DrawerActivity method accountClicked.

/**
 * sets the new/current account and restarts. In case the given account equals the actual/current account the call
 * will be ignored.
 *
 * @param hashCode HashCode of account to be set
 */
public void accountClicked(int hashCode) {
    final User currentUser = accountManager.getUser();
    if (currentUser.hashCode() != hashCode && accountManager.setCurrentOwnCloudAccount(hashCode)) {
        fetchExternalLinks(true);
        restart();
    }
}
Also used : User(com.nextcloud.client.account.User)

Aggregations

User (com.nextcloud.client.account.User)84 OCFile (com.owncloud.android.datamodel.OCFile)21 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)19 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)19 Intent (android.content.Intent)14 Account (android.accounts.Account)12 ArrayList (java.util.ArrayList)12 Context (android.content.Context)9 Fragment (androidx.fragment.app.Fragment)9 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)9 File (java.io.File)9 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)7 Uri (android.net.Uri)6 Bundle (android.os.Bundle)6 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)6 GalleryFragment (com.owncloud.android.ui.fragment.GalleryFragment)6 PreviewTextFileFragment (com.owncloud.android.ui.preview.PreviewTextFileFragment)6 PreviewTextFragment (com.owncloud.android.ui.preview.PreviewTextFragment)6 PreviewTextStringFragment (com.owncloud.android.ui.preview.PreviewTextStringFragment)6 View (android.view.View)5