Skip to content

Commit

Permalink
Download limit functionality added.
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Jun 26, 2023
1 parent 36fba6d commit 4951f39
Show file tree
Hide file tree
Showing 16 changed files with 907 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))

// read-only
onView(ViewMatchers.withId(R.id.share_process_permission_read_only)).check(matches(isChecked()))
Expand Down Expand Up @@ -397,6 +398,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(isDisplayed()))

// read-only
publicShare.permissions = 17 // from server
Expand Down Expand Up @@ -514,6 +516,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))

// read-only
userShare.permissions = 17 // from server
Expand Down Expand Up @@ -637,6 +640,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(not(isDisplayed())))
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(isDisplayed()))
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))

// read-only
userShare.permissions = 17 // from server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.shares.GetShareRemoteOperation;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation;
import com.owncloud.android.operations.common.SyncOperation;
import com.owncloud.android.operations.share_download_limit.DeleteShareDownloadLimitRemoteOperation;
import com.owncloud.android.operations.share_download_limit.UpdateShareDownloadLimitRemoteOperation;


/**
* Updates an existing private share for a given file.
*/
public class UpdateShareInfoOperation extends SyncOperation {

private static final String TAG = UpdateShareInfoOperation.class.getSimpleName();
private OCShare share;
private long shareId;
private long expirationDateInMillis;
Expand All @@ -46,6 +51,8 @@ public class UpdateShareInfoOperation extends SyncOperation {
private int permissions = -1;
private String password;
private String label;
//download limit for link share
private long downloadLimit;

/**
* Constructor
Expand Down Expand Up @@ -116,6 +123,9 @@ protected RemoteOperationResult run(OwnCloudClient client) {
if (result.isSuccess() && shareId > 0) {
OCShare ocShare = (OCShare) result.getData().get(0);
ocShare.setPasswordProtected(!TextUtils.isEmpty(password));

executeShareDownloadLimitOperation(client, ocShare);

getStorageManager().saveShare(ocShare);
}

Expand All @@ -124,6 +134,44 @@ protected RemoteOperationResult run(OwnCloudClient client) {
return result;
}

/**
* method will be used to update or delete the download limit for the particular share
*
* @param client
* @param ocShare share object
*/
private void executeShareDownloadLimitOperation(OwnCloudClient client, OCShare ocShare) {
//if share type is of Link Share then only we have to update the download limit if configured by user
if (ocShare.getShareType() == ShareType.PUBLIC_LINK && !ocShare.isFolder()) {

//if download limit it greater than 0 then update the limit
//else delete the download limit
if (downloadLimit > 0) {
//api will update the download limit for the particular share
UpdateShareDownloadLimitRemoteOperation updateShareDownloadLimitRemoteOperation =
new UpdateShareDownloadLimitRemoteOperation(ocShare.getToken(), downloadLimit);

RemoteOperationResult downloadLimitOp =
updateShareDownloadLimitRemoteOperation.execute(client);
if (downloadLimitOp.isSuccess()) {
Log_OC.d(TAG, "Download limit updated for the share.");
Log_OC.d(TAG, "Download limit " + downloadLimit);
}
} else {
//api will delete the download limit for the particular share
DeleteShareDownloadLimitRemoteOperation limitRemoteOperation =
new DeleteShareDownloadLimitRemoteOperation(ocShare.getToken());

RemoteOperationResult deleteDownloadLimitOp =
limitRemoteOperation.execute(client);
if (deleteDownloadLimitOp.isSuccess()) {
Log_OC.d(TAG, "Download limit delete for the share.");
}
}

}
}

public void setExpirationDateInMillis(long expirationDateInMillis) {
this.expirationDateInMillis = expirationDateInMillis;
}
Expand All @@ -147,5 +195,9 @@ public void setPassword(String password) {
public void setLabel(String label) {
this.label = label;
}

public void setDownloadLimit(long downloadLimit) {
this.downloadLimit = downloadLimit;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* ownCloud Android client application
*
* @author TSI-mc Copyright (C) 2021 TSI-mc
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* <p>
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/

package com.owncloud.android.operations.share_download_limit;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;

/**
* class to delete the download limit for the link share
* this has to be executed when user has toggled off the download limit
* <p>
* API : //DELETE to /ocs/v2.php/apps/files_downloadlimit/{share_token}/limit
*/
public class DeleteShareDownloadLimitRemoteOperation extends RemoteOperation {

private static final String TAG = DeleteShareDownloadLimitRemoteOperation.class.getSimpleName();

private final String shareToken;

public DeleteShareDownloadLimitRemoteOperation(String shareToken) {
this.shareToken = shareToken;
}

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result;
int status;

DeleteMethod deleteMethod = null;

try {
// Post Method
deleteMethod = new DeleteMethod(client.getBaseUri() + ShareDownloadLimitUtils.INSTANCE.getDownloadLimitApiPath(shareToken));

deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

status = client.executeMethod(deleteMethod);

if (isSuccess(status)) {
String response = deleteMethod.getResponseBodyAsString();

Log_OC.d(TAG, "Delete Download Limit response: " + response);

DownloadLimitXMLParser parser = new DownloadLimitXMLParser();
result = parser.parse(true, response);

if (result.isSuccess()) {
return result;
}

} else {
result = new RemoteOperationResult<>(false, deleteMethod);
}

} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Exception while deleting share download limit", e);

} finally {
if (deleteMethod != null) {
deleteMethod.releaseConnection();
}
}
return result;
}

private boolean isSuccess(int status) {
return status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.owncloud.android.operations.share_download_limit;

/**
* response from the Get download limit api
* <?xml version="1.0"?>
* <ocs>
* <meta>
* <status>ok</status>
* <statuscode>200</statuscode>
* <message>OK</message>
* </meta>
* <data>
* <limit>5</limit>
* <count>0</count>
* </data>
* </ocs>
*/
public class DownloadLimitResponse {
private long limit;
private long count;

public long getLimit() {
return limit;
}

public void setLimit(long limit) {
this.limit = limit;
}

public long getCount() {
return count;
}

public void setCount(long count) {
this.count = count;
}
}
Loading

0 comments on commit 4951f39

Please sign in to comment.