Search in sources :

Example 6 with UploadFileOperation

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);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) SuppressLint(android.annotation.SuppressLint)

Example 7 with UploadFileOperation

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);
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 8 with UploadFileOperation

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));
        }
    }
}
Also used : User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) ThumbnailsCacheManager(com.owncloud.android.datamodel.ThumbnailsCacheManager) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 9 with UploadFileOperation

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);
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) Test(org.junit.Test)

Example 10 with UploadFileOperation

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());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.db.OCUpload) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) Test(org.junit.Test)

Aggregations

UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)13 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)9 OCUpload (com.owncloud.android.db.OCUpload)8 Test (org.junit.Test)6 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)5 ConnectivityService (com.nextcloud.client.network.ConnectivityService)4 OCFile (com.owncloud.android.datamodel.OCFile)4 File (java.io.File)4 BatteryStatus (com.nextcloud.client.device.BatteryStatus)3 PowerManagementService (com.nextcloud.client.device.PowerManagementService)3 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)3 UserAccountManager (com.nextcloud.client.account.UserAccountManager)2 Connectivity (com.nextcloud.client.network.Connectivity)2 UploadsStorageManager (com.owncloud.android.datamodel.UploadsStorageManager)2 ChunkedUploadFileOperation (com.owncloud.android.operations.ChunkedUploadFileOperation)2 Account (android.accounts.Account)1 SuppressLint (android.annotation.SuppressLint)1 Message (android.os.Message)1 Parcelable (android.os.Parcelable)1 User (com.nextcloud.client.account.User)1