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