Skip to content

Commit c0b1c70

Browse files
committed
Download limit functionality added.
1 parent 7ca2a0b commit c0b1c70

18 files changed

+913
-27
lines changed

app/src/androidTest/java/com/nmc/android/ui/FileSharingIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class FileSharingIT : AbstractIT() {
340340
)
341341
)
342342

343-
val permissionList = permissionList(isFolder, ocShare.shareType)
343+
val permissionList = permissionList(isFolder, ocShare.shareType!!)
344344

345345
for (i in permissionList.indices) {
346346
// Scroll to the item at position i

app/src/androidTest/java/com/owncloud/android/ui/fragment/FileDetailSharingFragmentIT.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
269269
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(isDisplayed()))
270270
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(isDisplayed()))
271271
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(not(isDisplayed())))
272+
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))
272273

273274
// read-only
274275
onView(ViewMatchers.withId(R.id.share_process_permission_read_only)).check(matches(isChecked()))
@@ -397,6 +398,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
397398
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(isDisplayed()))
398399
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(isDisplayed()))
399400
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(not(isDisplayed())))
401+
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(isDisplayed()))
400402

401403
// read-only
402404
publicShare.permissions = 17 // from server
@@ -514,6 +516,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
514516
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(not(isDisplayed())))
515517
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(not(isDisplayed())))
516518
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(isDisplayed()))
519+
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))
517520

518521
// read-only
519522
userShare.permissions = 17 // from server
@@ -637,6 +640,7 @@ class FileDetailSharingFragmentIT : AbstractIT() {
637640
onView(ViewMatchers.withId(R.id.share_process_set_password_switch)).check(matches(not(isDisplayed())))
638641
onView(ViewMatchers.withId(R.id.share_process_change_name_switch)).check(matches(not(isDisplayed())))
639642
onView(ViewMatchers.withId(R.id.share_process_allow_resharing_checkbox)).check(matches(isDisplayed()))
643+
onView(ViewMatchers.withId(R.id.share_process_download_limit_switch)).check(matches(not(isDisplayed())))
640644

641645
// read-only
642646
userShare.permissions = 17 // from server

app/src/main/java/com/owncloud/android/operations/CreateShareWithShareeOperation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ protected RemoteOperationResult run(OwnCloudClient client) {
149149
updateShareInfoOperation.setHideFileDownload(hideFileDownload);
150150
updateShareInfoOperation.setLabel(label);
151151

152+
//update the permission using update info api
153+
//because for external share the selected permission is not getting updated
154+
//instead default Read/View Only permission is getting updated
155+
updateShareInfoOperation.setPermissions(permissions);
156+
152157
//execute and save the result in database
153158
RemoteOperationResult updateShareInfoResult = updateShareInfoOperation.execute(client);
154159
if (updateShareInfoResult.isSuccess() && updateShareInfoResult.getData().size() > 0) {

app/src/main/java/com/owncloud/android/operations/UpdateShareInfoOperation.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@
2727
import com.owncloud.android.lib.common.OwnCloudClient;
2828
import com.owncloud.android.lib.common.operations.RemoteOperation;
2929
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
30+
import com.owncloud.android.lib.common.utils.Log_OC;
3031
import com.owncloud.android.lib.resources.shares.GetShareRemoteOperation;
3132
import com.owncloud.android.lib.resources.shares.OCShare;
33+
import com.owncloud.android.lib.resources.shares.ShareType;
3234
import com.owncloud.android.lib.resources.shares.UpdateShareRemoteOperation;
3335
import com.owncloud.android.operations.common.SyncOperation;
36+
import com.owncloud.android.operations.share_download_limit.DeleteShareDownloadLimitRemoteOperation;
37+
import com.owncloud.android.operations.share_download_limit.UpdateShareDownloadLimitRemoteOperation;
3438

3539

3640
/**
3741
* Updates an existing private share for a given file.
3842
*/
3943
public class UpdateShareInfoOperation extends SyncOperation {
4044

45+
private static final String TAG = UpdateShareInfoOperation.class.getSimpleName();
4146
private OCShare share;
4247
private long shareId;
4348
private long expirationDateInMillis;
@@ -46,6 +51,8 @@ public class UpdateShareInfoOperation extends SyncOperation {
4651
private int permissions = -1;
4752
private String password;
4853
private String label;
54+
//download limit for link share
55+
private long downloadLimit;
4956

5057
/**
5158
* Constructor
@@ -116,6 +123,9 @@ protected RemoteOperationResult run(OwnCloudClient client) {
116123
if (result.isSuccess() && shareId > 0) {
117124
OCShare ocShare = (OCShare) result.getData().get(0);
118125
ocShare.setPasswordProtected(!TextUtils.isEmpty(password));
126+
127+
executeShareDownloadLimitOperation(client, ocShare);
128+
119129
getStorageManager().saveShare(ocShare);
120130
}
121131

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

137+
/**
138+
* method will be used to update or delete the download limit for the particular share
139+
*
140+
* @param client
141+
* @param ocShare share object
142+
*/
143+
private void executeShareDownloadLimitOperation(OwnCloudClient client, OCShare ocShare) {
144+
//if share type is of Link Share then only we have to update the download limit if configured by user
145+
if (ocShare.getShareType() == ShareType.PUBLIC_LINK && !ocShare.isFolder()) {
146+
147+
//if download limit it greater than 0 then update the limit
148+
//else delete the download limit
149+
if (downloadLimit > 0) {
150+
//api will update the download limit for the particular share
151+
UpdateShareDownloadLimitRemoteOperation updateShareDownloadLimitRemoteOperation =
152+
new UpdateShareDownloadLimitRemoteOperation(ocShare.getToken(), downloadLimit);
153+
154+
RemoteOperationResult downloadLimitOp =
155+
updateShareDownloadLimitRemoteOperation.execute(client);
156+
if (downloadLimitOp.isSuccess()) {
157+
Log_OC.d(TAG, "Download limit updated for the share.");
158+
Log_OC.d(TAG, "Download limit " + downloadLimit);
159+
}
160+
} else {
161+
//api will delete the download limit for the particular share
162+
DeleteShareDownloadLimitRemoteOperation limitRemoteOperation =
163+
new DeleteShareDownloadLimitRemoteOperation(ocShare.getToken());
164+
165+
RemoteOperationResult deleteDownloadLimitOp =
166+
limitRemoteOperation.execute(client);
167+
if (deleteDownloadLimitOp.isSuccess()) {
168+
Log_OC.d(TAG, "Download limit delete for the share.");
169+
}
170+
}
171+
172+
}
173+
}
174+
127175
public void setExpirationDateInMillis(long expirationDateInMillis) {
128176
this.expirationDateInMillis = expirationDateInMillis;
129177
}
@@ -147,5 +195,9 @@ public void setPassword(String password) {
147195
public void setLabel(String label) {
148196
this.label = label;
149197
}
198+
199+
public void setDownloadLimit(long downloadLimit) {
200+
this.downloadLimit = downloadLimit;
201+
}
150202
}
151203

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* ownCloud Android client application
3+
*
4+
* @author TSI-mc Copyright (C) 2021 TSI-mc
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7+
* License version 2, as published by the Free Software Foundation.
8+
* <p>
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11+
* details.
12+
* <p>
13+
* You should have received a copy of the GNU General Public License along with this program. If not, see
14+
* <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package com.owncloud.android.operations.share_download_limit;
18+
19+
import com.owncloud.android.lib.common.OwnCloudClient;
20+
import com.owncloud.android.lib.common.operations.RemoteOperation;
21+
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
22+
import com.owncloud.android.lib.common.utils.Log_OC;
23+
24+
import org.apache.commons.httpclient.HttpStatus;
25+
import org.apache.commons.httpclient.methods.DeleteMethod;
26+
27+
/**
28+
* class to delete the download limit for the link share
29+
* this has to be executed when user has toggled off the download limit
30+
* <p>
31+
* API : //DELETE to /ocs/v2.php/apps/files_downloadlimit/{share_token}/limit
32+
*/
33+
public class DeleteShareDownloadLimitRemoteOperation extends RemoteOperation {
34+
35+
private static final String TAG = DeleteShareDownloadLimitRemoteOperation.class.getSimpleName();
36+
37+
private final String shareToken;
38+
39+
public DeleteShareDownloadLimitRemoteOperation(String shareToken) {
40+
this.shareToken = shareToken;
41+
}
42+
43+
@Override
44+
protected RemoteOperationResult run(OwnCloudClient client) {
45+
RemoteOperationResult result;
46+
int status;
47+
48+
DeleteMethod deleteMethod = null;
49+
50+
try {
51+
// Post Method
52+
deleteMethod = new DeleteMethod(client.getBaseUri() + ShareDownloadLimitUtils.INSTANCE.getDownloadLimitApiPath(shareToken));
53+
54+
deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
55+
56+
status = client.executeMethod(deleteMethod);
57+
58+
if (isSuccess(status)) {
59+
String response = deleteMethod.getResponseBodyAsString();
60+
61+
Log_OC.d(TAG, "Delete Download Limit response: " + response);
62+
63+
DownloadLimitXMLParser parser = new DownloadLimitXMLParser();
64+
result = parser.parse(true, response);
65+
66+
if (result.isSuccess()) {
67+
return result;
68+
}
69+
70+
} else {
71+
result = new RemoteOperationResult<>(false, deleteMethod);
72+
}
73+
74+
} catch (Exception e) {
75+
result = new RemoteOperationResult<>(e);
76+
Log_OC.e(TAG, "Exception while deleting share download limit", e);
77+
78+
} finally {
79+
if (deleteMethod != null) {
80+
deleteMethod.releaseConnection();
81+
}
82+
}
83+
return result;
84+
}
85+
86+
private boolean isSuccess(int status) {
87+
return status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST;
88+
}
89+
90+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.owncloud.android.operations.share_download_limit;
2+
3+
/**
4+
* response from the Get download limit api
5+
* <?xml version="1.0"?>
6+
* <ocs>
7+
* <meta>
8+
* <status>ok</status>
9+
* <statuscode>200</statuscode>
10+
* <message>OK</message>
11+
* </meta>
12+
* <data>
13+
* <limit>5</limit>
14+
* <count>0</count>
15+
* </data>
16+
* </ocs>
17+
*/
18+
public class DownloadLimitResponse {
19+
private long limit;
20+
private long count;
21+
22+
public long getLimit() {
23+
return limit;
24+
}
25+
26+
public void setLimit(long limit) {
27+
this.limit = limit;
28+
}
29+
30+
public long getCount() {
31+
return count;
32+
}
33+
34+
public void setCount(long count) {
35+
this.count = count;
36+
}
37+
}

0 commit comments

Comments
 (0)