mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Implement UploadRemoteFileOperation with dav4droid
This commit is contained in:
parent
2ef51dd5f6
commit
75ce8e186f
@ -1 +1 @@
|
|||||||
Subproject commit ffeb457c4df4691aad75a43eaad54a6d8788ee75
|
Subproject commit 6a5835a960c4a3d4361a70ef72914b9fa214363d
|
@ -24,32 +24,6 @@
|
|||||||
|
|
||||||
package com.owncloud.android.lib.sampleclient;
|
package com.owncloud.android.lib.sampleclient;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
|
||||||
import com.owncloud.android.lib.refactor.OCContext;
|
|
||||||
import com.owncloud.android.lib.refactor.account.OCAccount;
|
|
||||||
import com.owncloud.android.lib.refactor.authentication.credentials.OwnCloudCredentialsFactory;
|
|
||||||
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
|
||||||
import com.owncloud.android.lib.refactor.OwnCloudContext;
|
|
||||||
import com.owncloud.android.lib.refactor.operations.PropfindOperation;
|
|
||||||
import com.owncloud.android.lib.resources.files.RemoteFile;
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
|
||||||
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
|
|
||||||
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
|
|
||||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
|
||||||
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
|
||||||
import com.owncloud.android.lib.resources.files.FileUtils;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.res.AssetManager;
|
import android.content.res.AssetManager;
|
||||||
import android.graphics.drawable.BitmapDrawable;
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
@ -62,6 +36,31 @@ import android.widget.ListView;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
||||||
|
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||||
|
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.refactor.OCContext;
|
||||||
|
import com.owncloud.android.lib.refactor.account.OCAccount;
|
||||||
|
import com.owncloud.android.lib.refactor.authentication.credentials.OwnCloudCredentialsFactory;
|
||||||
|
import com.owncloud.android.lib.refactor.operations.PropfindOperation;
|
||||||
|
import com.owncloud.android.lib.refactor.operations.UploadRemoteFileOperation;
|
||||||
|
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
|
||||||
|
import com.owncloud.android.lib.resources.files.FileUtils;
|
||||||
|
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
|
||||||
|
import com.owncloud.android.lib.resources.files.RemoteFile;
|
||||||
|
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener {
|
||||||
|
|
||||||
private static String LOG_TAG = MainActivity.class.getCanonicalName();
|
private static String LOG_TAG = MainActivity.class.getCanonicalName();
|
||||||
@ -165,6 +164,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startUpload() {
|
private void startUpload() {
|
||||||
|
|
||||||
|
|
||||||
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path));
|
||||||
File fileToUpload = upFolder.listFiles()[0];
|
File fileToUpload = upFolder.listFiles()[0];
|
||||||
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
String remotePath = FileUtils.PATH_SEPARATOR + fileToUpload.getName();
|
||||||
@ -174,9 +175,19 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
Long timeStampLong = fileToUpload.lastModified()/1000;
|
Long timeStampLong = fileToUpload.lastModified()/1000;
|
||||||
String timeStamp = timeStampLong.toString();
|
String timeStamp = timeStampLong.toString();
|
||||||
|
|
||||||
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
final UploadRemoteFileOperation uploadRemoteFileOperation = new UploadRemoteFileOperation(
|
||||||
uploadOperation.addDatatransferProgressListener(this);
|
mOCContext,
|
||||||
uploadOperation.execute(mClient, this, mHandler);
|
fileToUpload.getAbsolutePath(),
|
||||||
|
remotePath,
|
||||||
|
mimeType,
|
||||||
|
timeStamp
|
||||||
|
);
|
||||||
|
|
||||||
|
new Thread(() -> uploadRemoteFileOperation.exec()).start();
|
||||||
|
|
||||||
|
// UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
|
||||||
|
// uploadOperation.addDatatransferProgressListener(this);
|
||||||
|
// uploadOperation.execute(mClient, this, mHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRemoteDeletion() {
|
private void startRemoteDeletion() {
|
||||||
@ -219,8 +230,8 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
} else if (operation instanceof ReadRemoteFolderOperation) {
|
} else if (operation instanceof ReadRemoteFolderOperation) {
|
||||||
onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
|
onSuccessfulRefresh((ReadRemoteFolderOperation)operation, result);
|
||||||
|
|
||||||
} else if (operation instanceof UploadRemoteFileOperation ) {
|
} else if (operation instanceof com.owncloud.android.lib.resources.files.UploadRemoteFileOperation) {
|
||||||
onSuccessfulUpload((UploadRemoteFileOperation)operation, result);
|
onSuccessfulUpload((com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)operation, result);
|
||||||
|
|
||||||
} else if (operation instanceof RemoveRemoteFileOperation ) {
|
} else if (operation instanceof RemoveRemoteFileOperation ) {
|
||||||
onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result);
|
onSuccessfulRemoteDeletion((RemoveRemoteFileOperation)operation, result);
|
||||||
@ -249,7 +260,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
|
|||||||
mFilesAdapter.notifyDataSetChanged();
|
mFilesAdapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onSuccessfulUpload(UploadRemoteFileOperation operation, RemoteOperationResult result) {
|
private void onSuccessfulUpload(com.owncloud.android.lib.resources.files.UploadRemoteFileOperation operation, RemoteOperationResult result) {
|
||||||
startRefresh();
|
startRefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,27 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2018 ownCloud GmbH.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
package com.owncloud.android.lib.refactor.operations;
|
package com.owncloud.android.lib.refactor.operations;
|
||||||
|
|
||||||
import com.owncloud.android.lib.refactor.OCContext;
|
import com.owncloud.android.lib.refactor.OCContext;
|
||||||
@ -13,7 +37,6 @@ public class PropfindOperation extends RemoteOperation {
|
|||||||
|
|
||||||
public PropfindOperation(OCContext context, String remotePath) {
|
public PropfindOperation(OCContext context, String remotePath) {
|
||||||
super(context);
|
super(context);
|
||||||
|
|
||||||
mRemotePath = remotePath;
|
mRemotePath = remotePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
package com.owncloud.android.lib.refactor.operations;
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.refactor.OCContext;
|
|
||||||
import com.owncloud.android.lib.refactor.RemoteOperation;
|
|
||||||
import com.owncloud.android.lib.refactor.RemoteOperationResult;
|
|
||||||
|
|
||||||
import at.bitfire.dav4android.DavOCResource;
|
|
||||||
import at.bitfire.dav4android.DavResource;
|
|
||||||
import at.bitfire.dav4android.PropertyUtils;
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
|
|
||||||
public class PutOperation extends RemoteOperation {
|
|
||||||
|
|
||||||
private String mRemotePath;
|
|
||||||
private String mMimeType;
|
|
||||||
private String mTimeStamp;
|
|
||||||
|
|
||||||
public PutOperation(OCContext context, String remotePath, String mimetype, String timestamp) {
|
|
||||||
super(context);
|
|
||||||
|
|
||||||
mRemotePath = remotePath;
|
|
||||||
mMimeType = mimetype;
|
|
||||||
mTimeStamp = timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RemoteOperationResult exec() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
MediaType mediaType = MediaType.parse(mMimeType);
|
|
||||||
RequestBody requestBody = RequestBody.create(mediaType, mRemotePath);
|
|
||||||
|
|
||||||
DavOCResource davOCResource = new DavOCResource(getClient(), getWebDavHttpUrl(mRemotePath), null);
|
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,81 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2018 ownCloud GmbH.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.owncloud.android.lib.refactor.operations;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.refactor.OCContext;
|
||||||
|
import com.owncloud.android.lib.refactor.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.refactor.RemoteOperationResult;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import at.bitfire.dav4android.DavOCResource;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
|
||||||
|
public class UploadRemoteFileOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
private String mLocalPath;
|
||||||
|
private String mRemotePath;
|
||||||
|
private String mMimeType;
|
||||||
|
private String mFileLastModifTimestamp;
|
||||||
|
|
||||||
|
public UploadRemoteFileOperation(OCContext context, String localPath, String remotePath, String mimetype,
|
||||||
|
String fileLastModifTimestamp) {
|
||||||
|
super(context);
|
||||||
|
|
||||||
|
mLocalPath = localPath;
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
mMimeType = mimetype;
|
||||||
|
mFileLastModifTimestamp = fileLastModifTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RemoteOperationResult exec() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
File fileToUpload = new File(mLocalPath);
|
||||||
|
MediaType mediaType = MediaType.parse(mMimeType);
|
||||||
|
RequestBody requestBody = RequestBody.create(mediaType, fileToUpload);
|
||||||
|
|
||||||
|
DavOCResource davOCResource = new DavOCResource(
|
||||||
|
getClient(),
|
||||||
|
getWebDavHttpUrl(mRemotePath),
|
||||||
|
null);
|
||||||
|
|
||||||
|
davOCResource.put(requestBody,
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
"multipart/form-data",
|
||||||
|
String.valueOf(fileToUpload.length()),
|
||||||
|
mFileLastModifTimestamp);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user