use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation in project android by nextcloud.
the class FileDataStorageManager method saveFileWithParent.
/**
* traverses a files parent tree to be able to store a file with its parents. Throws a
* RemoteOperationFailedException in case the parent can't be retrieved.
*
* @param ocFile the file
* @param context the app context
* @return the parent file
*/
public OCFile saveFileWithParent(OCFile ocFile, Context context) {
if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) {
String remotePath = ocFile.getRemotePath();
String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName()));
OCFile parentFile = getFileByPath(parentPath);
OCFile returnFile;
if (parentFile == null) {
// remote request
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath);
// TODO Deprecated
RemoteOperationResult result = operation.execute(getUser().toPlatformAccount(), context);
if (result.isSuccess()) {
OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
returnFile = saveFileWithParent(remoteFolder, context);
} else {
Exception exception = result.getException();
String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / " + result.getLogMessage();
if (exception != null) {
throw new RemoteOperationFailedException(message, exception);
} else {
throw new RemoteOperationFailedException(message);
}
}
} else {
returnFile = saveFileWithParent(parentFile, context);
}
ocFile.setParentId(returnFile.getFileId());
saveFile(ocFile);
}
return ocFile;
}
use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation in project android by nextcloud.
the class UploadFileOperation method saveUploadedFile.
/**
* Saves a OC File after a successful upload.
* <p>
* A PROPFIND is necessary to keep the props in the local database
* synchronized with the server, specially the modification time and Etag
* (where available)
*/
private void saveUploadedFile(OwnCloudClient client) {
OCFile file = mFile;
if (file.fileExists()) {
file = getStorageManager().getFileById(file.getFileId());
}
if (file == null) {
// this can happen e.g. when the file gets deleted during upload
return;
}
long syncDate = System.currentTimeMillis();
file.setLastSyncDateForData(syncDate);
// new PROPFIND to keep data consistent with server
// in theory, should return the same we already have
// TODO from the appropriate OC server version, get data from last PUT response headers, instead
// TODO of a new PROPFIND; the latter may fail, specially for chunked uploads
String path;
if (encryptedAncestor) {
path = file.getParentRemotePath() + mFile.getEncryptedFileName();
} else {
path = getRemotePath();
}
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(path);
RemoteOperationResult result = operation.execute(client);
if (result.isSuccess()) {
updateOCFile(file, (RemoteFile) result.getData().get(0));
file.setLastSyncDateForProperties(syncDate);
} else {
Log_OC.e(TAG, "Error reading properties of file after successful upload; this is gonna hurt...");
}
if (mWasRenamed) {
OCFile oldFile = getStorageManager().getFileByPath(mOldFile.getRemotePath());
if (oldFile != null) {
oldFile.setStoragePath(null);
getStorageManager().saveFile(oldFile);
getStorageManager().saveConflict(oldFile, null);
}
// else: it was just an automatic renaming due to a name
// coincidence; nothing else is needed, the storagePath is right
// in the instance returned by mCurrentUpload.getFile()
}
file.setUpdateThumbnailNeeded(true);
getStorageManager().saveFile(file);
getStorageManager().saveConflict(file, null);
if (MimeTypeUtil.isMedia(file.getMimeType())) {
FileDataStorageManager.triggerMediaScan(file.getStoragePath(), file);
}
// generate new Thumbnail
final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(getStorageManager(), user);
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
}
use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation in project android by nextcloud.
the class SynchronizeFileOperation method run.
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
mTransferWasRequested = false;
if (mLocalFile == null) {
// Get local file from the DB
mLocalFile = getStorageManager().getFileByPath(mRemotePath);
}
if (!mLocalFile.isDown()) {
// / easy decision
requestForDownload(mLocalFile);
result = new RemoteOperationResult(ResultCode.OK);
} else {
// / local copy in the device -> need to think a bit more before do anything
if (mServerFile == null) {
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(mRemotePath);
result = operation.execute(client);
if (result.isSuccess()) {
mServerFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
mServerFile.setLastSyncDateForProperties(System.currentTimeMillis());
} else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
return result;
}
}
if (mServerFile != null) {
// / check changes in server and local file
boolean serverChanged;
if (TextUtils.isEmpty(mLocalFile.getEtag())) {
// file uploaded (null) or downloaded ("") before upgrade to version 1.8.0; check the old condition
serverChanged = mServerFile.getModificationTimestamp() != mLocalFile.getModificationTimestampAtLastSyncForData();
} else {
serverChanged = !mServerFile.getEtag().equals(mLocalFile.getEtag());
}
boolean localChanged = mLocalFile.getLocalModificationTimestamp() > mLocalFile.getLastSyncDateForData();
// if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
if (localChanged && serverChanged) {
result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT);
getStorageManager().saveConflict(mLocalFile, mServerFile.getEtag());
} else if (localChanged) {
if (mSyncFileContents && mAllowUploads) {
requestForUpload(mLocalFile);
// the local update of file properties will be done by the FileUploader
// service when the upload finishes
} else {
// NOTHING TO DO HERE: updating the properties of the file in the server
// without uploading the contents would be stupid;
// So, an instance of SynchronizeFileOperation created with
// syncFileContents == false is completely useless when we suspect
// that an upload is necessary (for instance, in FileObserverService).
Log_OC.d(TAG, "Nothing to do here");
}
result = new RemoteOperationResult(ResultCode.OK);
} else if (serverChanged) {
mLocalFile.setRemoteId(mServerFile.getRemoteId());
if (mSyncFileContents) {
// local, not server; we won't to keep
requestForDownload(mLocalFile);
// the value of favorite!
// the update of local data will be done later by the FileUploader
// service when the upload finishes
} else {
// TODO CHECK: is this really useful in some point in the code?
mServerFile.setFavorite(mLocalFile.isFavorite());
mServerFile.setLastSyncDateForData(mLocalFile.getLastSyncDateForData());
mServerFile.setStoragePath(mLocalFile.getStoragePath());
mServerFile.setParentId(mLocalFile.getParentId());
mServerFile.setEtag(mLocalFile.getEtag());
getStorageManager().saveFile(mServerFile);
}
result = new RemoteOperationResult(ResultCode.OK);
} else {
// nothing changed, nothing to do
result = new RemoteOperationResult(ResultCode.OK);
}
// safe blanket: sync'ing a not in-conflict file will clean wrong conflict markers in ancestors
if (result.getCode() != ResultCode.SYNC_CONFLICT) {
getStorageManager().saveConflict(mLocalFile, null);
}
} else {
// remote file does not exist, deleting local copy
boolean deleteResult = getStorageManager().removeFile(mLocalFile, true, true);
if (deleteResult) {
result = new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
} else {
Log_OC.e(TAG, "Removal of local copy failed (remote file does not exist any longer).");
}
}
}
Log_OC.i(TAG, "Synchronizing " + mUser.getAccountName() + ", file " + mLocalFile.getRemotePath() + ": " + result.getLogMessage());
return result;
}
use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation in project android by nextcloud.
the class EndToEndRandomIT method uploadFile.
private void uploadFile(int i) throws IOException {
String fileName = RandomString.make(5) + ".txt";
File file;
if (new Random().nextBoolean()) {
file = createFile(fileName, new Random().nextInt(50000));
} else {
file = createFile(fileName, 500000 + new Random().nextInt(50000));
}
String remotePath = currentFolder.getRemotePath() + fileName;
Log_OC.d(this, "[" + i + "/" + actionCount + "] " + "Upload file to: " + currentFolder.getDecryptedRemotePath() + fileName);
OCUpload ocUpload = new OCUpload(file.getAbsolutePath(), remotePath, account.name);
uploadOCUpload(ocUpload);
shortSleep();
OCFile parentFolder = getStorageManager().getFileByEncryptedRemotePath(new File(ocUpload.getRemotePath()).getParent() + "/");
String uploadedFileName = new File(ocUpload.getRemotePath()).getName();
String decryptedPath = parentFolder.getDecryptedRemotePath() + uploadedFileName;
OCFile uploadedFile = getStorageManager().getFileByDecryptedRemotePath(decryptedPath);
verifyStoragePath(uploadedFile);
// verify storage path
refreshFolder(currentFolder.getRemotePath());
uploadedFile = getStorageManager().getFileByDecryptedRemotePath(decryptedPath);
verifyStoragePath(uploadedFile);
// verify that encrypted file is on server
assertTrue(new ReadFileRemoteOperation(currentFolder.getRemotePath() + uploadedFile.getEncryptedFileName()).execute(client).isSuccess());
// verify that unencrypted file is not on server
assertFalse(new ReadFileRemoteOperation(currentFolder.getDecryptedRemotePath() + fileName).execute(client).isSuccess());
}
use of com.owncloud.android.lib.resources.files.ReadFileRemoteOperation in project android by nextcloud.
the class ConflictsResolveActivity method onStart.
@Override
protected void onStart() {
super.onStart();
if (getAccount() == null) {
finish();
return;
}
if (newFile == null) {
Log_OC.e(TAG, "No file received");
finish();
return;
}
if (existingFile == null) {
// fetch info of existing file from server
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(newFile.getRemotePath());
new Thread(() -> {
try {
RemoteOperationResult result = operation.execute(getAccount(), this);
if (result.isSuccess()) {
existingFile = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
existingFile.setLastSyncDateForProperties(System.currentTimeMillis());
startDialog();
} else {
Log_OC.e(TAG, "ReadFileRemoteOp returned failure with code: " + result.getHttpCode());
showErrorAndFinish();
}
} catch (Exception e) {
Log_OC.e(TAG, "Error when trying to fetch remote file", e);
showErrorAndFinish();
}
}).start();
} else {
startDialog();
}
}
Aggregations