use of com.owncloud.android.operations.UploadFileOperation in project android by nextcloud.
the class FileUploader method startNewUpload.
/**
* Start a new {@link UploadFileOperation}.
*/
@SuppressLint("SdCardPath")
private void startNewUpload(User user, List<String> requestedUploads, boolean onWifiOnly, boolean whileChargingOnly, NameCollisionPolicy nameCollisionPolicy, int localAction, boolean isCreateRemoteFolder, int createdBy, OCFile file, boolean disableRetries) {
if (file.getStoragePath().startsWith("/data/data/")) {
Log_OC.d(TAG, "Upload from sensitive path is not allowed");
return;
}
OCUpload ocUpload = new OCUpload(file, user);
ocUpload.setFileSize(file.getFileLength());
ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
ocUpload.setCreatedBy(createdBy);
ocUpload.setLocalAction(localAction);
ocUpload.setUseWifiOnly(onWifiOnly);
ocUpload.setWhileChargingOnly(whileChargingOnly);
ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, file, ocUpload, nameCollisionPolicy, localAction, this, onWifiOnly, whileChargingOnly, disableRetries, new FileDataStorageManager(user, getContentResolver()));
newUpload.setCreatedBy(createdBy);
if (isCreateRemoteFolder) {
newUpload.setRemoteFolderToBeCreated();
}
newUpload.addDataTransferProgressListener(this);
newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), file.getRemotePath(), newUpload);
if (putResult != null) {
requestedUploads.add(putResult.first);
// Save upload in database
long id = mUploadsStorageManager.storeUpload(ocUpload);
newUpload.setOCUploadId(id);
}
}
use of com.owncloud.android.operations.UploadFileOperation in project android by nextcloud.
the class FileUploader method retryUploads.
/**
* Retries a list of uploads.
*/
private void retryUploads(Intent intent, User user, List<String> requestedUploads) {
boolean onWifiOnly;
boolean whileChargingOnly;
OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
onWifiOnly = upload.isUseWifiOnly();
whileChargingOnly = upload.isWhileChargingOnly();
UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, null, upload, upload.getNameCollisionPolicy(), upload.getLocalAction(), this, onWifiOnly, whileChargingOnly, true, new FileDataStorageManager(user, getContentResolver()));
newUpload.addDataTransferProgressListener(this);
newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), upload.getRemotePath(), newUpload);
if (putResult != null) {
String uploadKey = putResult.first;
requestedUploads.add(uploadKey);
// Update upload in database
upload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
mUploadsStorageManager.updateUpload(upload);
}
}
use of com.owncloud.android.operations.UploadFileOperation in project android by nextcloud.
the class FileUploader method uploadFile.
/**
* Core upload method: sends the file(s) to upload
*
* @param uploadKey Key to access the upload to perform, contained in mPendingUploads
*/
public void uploadFile(String uploadKey) {
mCurrentUpload = mPendingUploads.get(uploadKey);
if (mCurrentUpload != null) {
// / Check account existence
if (!accountManager.exists(mCurrentUpload.getAccount())) {
Log_OC.w(TAG, "Account " + mCurrentUpload.getAccount().name + " does not exist anymore -> cancelling all its uploads");
cancelUploadsForAccount(mCurrentUpload.getAccount());
return;
}
// / OK, let's upload
mUploadsStorageManager.updateDatabaseUploadStart(mCurrentUpload);
notifyUploadStart(mCurrentUpload);
sendBroadcastUploadStarted(mCurrentUpload);
RemoteOperationResult uploadResult = null;
try {
// / prepare client object to send the request to the ownCloud server
if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
mCurrentAccount = mCurrentUpload.getAccount();
mStorageManager = new FileDataStorageManager(getCurrentUser().get(), getContentResolver());
}
// else, reuse storage manager from previous operation
// always get client from client manager, to get fresh credentials in case of update
OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
// // If parent folder is encrypted, upload file encrypted
// OCFile parent = mStorageManager.getFileByPath(mCurrentUpload.getFile().getParentRemotePath());
// if (parent.isEncrypted()) {
// UploadEncryptedFileOperation uploadEncryptedFileOperation =
// new UploadEncryptedFileOperation(parent, mCurrentUpload);
//
// uploadResult = uploadEncryptedFileOperation.execute(mUploadClient, mStorageManager);
// } else {
// / perform the regular upload
uploadResult = mCurrentUpload.execute(mUploadClient);
// }
} catch (Exception e) {
Log_OC.e(TAG, "Error uploading", e);
uploadResult = new RemoteOperationResult(e);
} finally {
Pair<UploadFileOperation, String> removeResult;
if (mCurrentUpload.wasRenamed()) {
removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getOldFile().getRemotePath());
// TODO: grant that name is also updated for mCurrentUpload.getOCUploadId
} else {
removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getDecryptedRemotePath());
}
mUploadsStorageManager.updateDatabaseUploadResult(uploadResult, mCurrentUpload);
// / notify result
notifyUploadResult(mCurrentUpload, uploadResult);
sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
}
// generate new Thumbnail
Optional<User> user = getCurrentUser();
if (user.isPresent()) {
final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(mStorageManager, user.get());
File file = new File(mCurrentUpload.getOriginalStoragePath());
String remoteId = mCurrentUpload.getFile().getRemoteId();
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId));
}
}
}
use of com.owncloud.android.operations.UploadFileOperation in project android by nextcloud.
the class UploadIT method testUploadOnWifiOnlyAndWifi.
@Test
public void testUploadOnWifiOnlyAndWifi() {
OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt", FOLDER + "wifi.txt", account.name);
ocUpload.setWhileChargingOnly(true);
UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, true, false, getStorageManager());
newUpload.setRemoteFolderToBeCreated();
newUpload.addRenameUploadListener(() -> {
// dummy
});
RemoteOperationResult result = newUpload.execute(client);
assertTrue(result.toString(), result.isSuccess());
// cleanup
new RemoveFileOperation(getStorageManager().getFileByDecryptedRemotePath(FOLDER), false, account, false, targetContext, getStorageManager()).execute(client);
}
use of com.owncloud.android.operations.UploadFileOperation in project android by nextcloud.
the class UploadIT method testCreationAndUploadTimestamp.
@Test
public void testCreationAndUploadTimestamp() throws IOException {
File file = getDummyFile("/empty.txt");
String remotePath = "/testFile.txt";
OCUpload ocUpload = new OCUpload(file.getAbsolutePath(), remotePath, account.name);
long creationTimestamp = Files.readAttributes(file.toPath(), BasicFileAttributes.class).creationTime().to(TimeUnit.SECONDS);
// wait a bit to simulate a later upload, so we can verify if creation date is set correct
shortSleep();
assertTrue(new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, false, false, getStorageManager()).setRemoteFolderToBeCreated().execute(client).isSuccess());
long uploadTimestamp = System.currentTimeMillis() / 1000;
// RefreshFolderOperation
assertTrue(new RefreshFolderOperation(getStorageManager().getFileByDecryptedRemotePath("/"), System.currentTimeMillis() / 1000, false, false, getStorageManager(), user, targetContext).execute(client).isSuccess());
List<OCFile> files = getStorageManager().getFolderContent(getStorageManager().getFileByDecryptedRemotePath("/"), false);
OCFile ocFile = files.get(0);
assertEquals(remotePath, ocFile.getRemotePath());
assertEquals(creationTimestamp, ocFile.getCreationTimestamp());
assertTrue(uploadTimestamp - 10 < ocFile.getUploadTimestamp() || uploadTimestamp + 10 > ocFile.getUploadTimestamp());
}
Aggregations