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();
}
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);
}
}
}
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;
}
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);
}
}
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();
}
}
Aggregations