Search in sources :

Example 66 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class AccountUtils method setCurrentOwnCloudAccount.

public static boolean setCurrentOwnCloudAccount(final Context context, String accountName) {
    boolean result = false;
    if (accountName != null) {
        boolean found;
        for (final Account account : getAccounts(context)) {
            found = (account.name.equals(accountName));
            if (found) {
                SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
                appPrefs.putString("select_oc_account", accountName);
                // update credentials
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
                        GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
                        RemoteOperationResult updateResult = getCapabilities.execute(storageManager, context);
                        Log_OC.w(TAG, "Update Capabilities: " + updateResult.isSuccess());
                    }
                });
                t.start();
                appPrefs.apply();
                result = true;
                break;
            }
        }
    }
    return result;
}
Also used : Account(android.accounts.Account) SharedPreferences(android.content.SharedPreferences) GetCapabilitiesOperarion(com.owncloud.android.operations.GetCapabilitiesOperarion) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager)

Example 67 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class DocumentsStorageProvider method toDocument.

private Document toDocument(String documentId) throws FileNotFoundException {
    String[] separated = documentId.split(DOCUMENTID_SEPARATOR, DOCUMENTID_PARTS);
    if (separated.length != DOCUMENTID_PARTS) {
        throw new FileNotFoundException("Invalid documentID " + documentId + "!");
    }
    FileDataStorageManager storageManager = rootIdToStorageManager.get(separated[0]);
    if (storageManager == null) {
        throw new FileNotFoundException("No storage manager associated for " + documentId + "!");
    }
    return new Document(storageManager, Long.parseLong(separated[1]));
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager)

Example 68 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class SynchronizeFolderOperation method removeLocalFolder.

private void removeLocalFolder() {
    FileDataStorageManager storageManager = getStorageManager();
    if (storageManager.fileExists(mLocalFolder.getFileId())) {
        String currentSavePath = FileStorageUtils.getSavePath(user.getAccountName());
        storageManager.removeFolder(mLocalFolder, true, // TODO: debug, I think this is always false for folders
        mLocalFolder.isDown() && mLocalFolder.getStoragePath().startsWith(currentSavePath));
    }
}
Also used : FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager)

Example 69 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class SynchronizeFolderOperation method synchronizeData.

/**
 * Synchronizes the data retrieved from the server about the contents of the target folder
 * with the current data in the local database.
 *
 * @param folderAndFiles Remote folder and children files in Folder
 */
private void synchronizeData(List<Object> folderAndFiles) throws OperationCancelledException {
    // parse data from remote folder
    OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(0));
    remoteFolder.setParentId(mLocalFolder.getParentId());
    remoteFolder.setFileId(mLocalFolder.getFileId());
    Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
    mFilesForDirectDownload.clear();
    mFilesToSyncContents.clear();
    if (mCancellationRequested.get()) {
        throw new OperationCancelledException();
    }
    FileDataStorageManager storageManager = getStorageManager();
    // if local folder is encrypted, download fresh metadata
    boolean encryptedAncestor = FileStorageUtils.checkEncryptionStatus(remoteFolder, storageManager);
    mLocalFolder.setEncrypted(encryptedAncestor);
    // update permission
    mLocalFolder.setPermissions(remoteFolder.getPermissions());
    // update richWorkspace
    mLocalFolder.setRichWorkspace(remoteFolder.getRichWorkspace());
    DecryptedFolderMetadata metadata = RefreshFolderOperation.getDecryptedFolderMetadata(encryptedAncestor, mLocalFolder, getClient(), user.toPlatformAccount(), mContext);
    // get current data about local contents of the folder to synchronize
    Map<String, OCFile> localFilesMap = RefreshFolderOperation.prefillLocalFilesMap(metadata, storageManager.getFolderContent(mLocalFolder, false));
    // loop to synchronize every child
    List<OCFile> updatedFiles = new ArrayList<>(folderAndFiles.size() - 1);
    OCFile remoteFile;
    OCFile localFile;
    OCFile updatedFile;
    RemoteFile remote;
    for (int i = 1; i < folderAndFiles.size(); i++) {
        // / new OCFile instance with the data from the server
        remote = (RemoteFile) folderAndFiles.get(i);
        remoteFile = FileStorageUtils.fillOCFile(remote);
        // / new OCFile instance to merge fresh data from server with local state
        updatedFile = FileStorageUtils.fillOCFile(remote);
        updatedFile.setParentId(mLocalFolder.getFileId());
        // / retrieve local data for the read file
        localFile = localFilesMap.remove(remoteFile.getRemotePath());
        // TODO better implementation is needed
        if (localFile == null) {
            localFile = storageManager.getFileByPath(updatedFile.getRemotePath());
        }
        // / add to updatedFile data about LOCAL STATE (not existing in server)
        updateLocalStateData(remoteFile, localFile, updatedFile);
        // / check and fix, if needed, local storage path
        FileStorageUtils.searchForLocalFileInDefaultPath(updatedFile, user.getAccountName());
        // update file name for encrypted files
        if (metadata != null) {
            RefreshFolderOperation.updateFileNameForEncryptedFile(storageManager, metadata, updatedFile);
        }
        // we parse content, so either the folder itself or its direct parent (which we check) must be encrypted
        boolean encrypted = updatedFile.isEncrypted() || mLocalFolder.isEncrypted();
        updatedFile.setEncrypted(encrypted);
        // / classify file to sync/download contents later
        classifyFileForLaterSyncOrDownload(remoteFile, localFile);
        updatedFiles.add(updatedFile);
    }
    if (metadata != null) {
        RefreshFolderOperation.updateFileNameForEncryptedFile(storageManager, metadata, mLocalFolder);
    }
    // save updated contents in local database
    storageManager.saveFolder(remoteFolder, updatedFiles, localFilesMap.values());
    mLocalFolder.setLastSyncDateForData(System.currentTimeMillis());
    storageManager.saveFile(mLocalFolder);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ArrayList(java.util.ArrayList) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile)

Example 70 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.

the class DownloadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // / perform the download
    synchronized (cancellationRequested) {
        if (cancellationRequested.get()) {
            return new RemoteOperationResult(new OperationCancelledException());
        }
    }
    RemoteOperationResult result;
    File newFile;
    boolean moved;
    // / download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());
    String tmpFolder = getTmpFolder();
    downloadOperation = new DownloadFileRemoteOperation(file.getRemotePath(), tmpFolder);
    Iterator<OnDatatransferProgressListener> listener = dataTransferListeners.iterator();
    while (listener.hasNext()) {
        downloadOperation.addDatatransferProgressListener(listener.next());
    }
    result = downloadOperation.execute(client);
    if (result.isSuccess()) {
        modificationTimestamp = downloadOperation.getModificationTimestamp();
        etag = downloadOperation.getEtag();
        newFile = new File(getSavePath());
        if (!newFile.getParentFile().exists() && !newFile.getParentFile().mkdirs()) {
            Log_OC.e(TAG, "Unable to create parent folder " + newFile.getParentFile().getAbsolutePath());
        }
        // decrypt file
        if (file.isEncrypted()) {
            FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, context.getContentResolver());
            OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath());
            DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, context, user.toPlatformAccount());
            if (metadata == null) {
                return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
            }
            byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getEncrypted().getKey());
            byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getInitializationVector());
            byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles().get(file.getEncryptedFileName()).getAuthenticationTag());
            try {
                byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
                try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)) {
                    fileOutputStream.write(decryptedBytes);
                }
            } catch (Exception e) {
                return new RemoteOperationResult(e);
            }
        }
        moved = tmpFile.renameTo(newFile);
        newFile.setLastModified(file.getModificationTimestamp());
        if (!moved) {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
        }
    }
    Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
    return result;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) DownloadFileRemoteOperation(com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) OCFile(com.owncloud.android.datamodel.OCFile) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileOutputStream(java.io.FileOutputStream) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Aggregations

FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)91 OCFile (com.owncloud.android.datamodel.OCFile)43 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)21 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)16 Intent (android.content.Intent)12 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)12 Context (android.content.Context)9 File (java.io.File)9 ArrayList (java.util.ArrayList)9 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)8 Test (org.junit.Test)8 ScreenshotTest (com.owncloud.android.utils.ScreenshotTest)7 OCUpload (com.owncloud.android.db.OCUpload)6 AccountManager (android.accounts.AccountManager)5 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)5 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)5 DialogFragment (androidx.fragment.app.DialogFragment)4 IOException (java.io.IOException)4 AccountsException (android.accounts.AccountsException)3