Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download limit for file link share #11563

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author TSI-mc
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
* Copyright (C) 2021 TSI-mc
* Copyright (C) 2023 TSI-mc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand Down 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 @@ -2,7 +2,7 @@
* Nextcloud Android client application
*
* @author TSI-mc
* Copyright (C) 2021 TSI-mc
* Copyright (C) 2023 TSI-mc
* Copyright (C) 2021 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -27,8 +27,12 @@
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.download_limit.DeleteShareDownloadLimitRemoteOperation;
import com.owncloud.android.lib.resources.download_limit.UpdateShareDownloadLimitRemoteOperation;
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;

Expand All @@ -38,6 +42,7 @@
*/
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
Expand Up @@ -7,7 +7,7 @@
* @author TSI-mc
* Copyright (C) 2015 ownCloud Inc.
* Copyright (C) 2018 Andy Scherzinger
* Copyright (C) 2021 TSI-mc
* Copyright (C) 2023 TSI-mc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -52,6 +52,7 @@
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.download_limit.GetShareDownloadLimitOperation;
import com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation;
import com.owncloud.android.lib.resources.files.model.FileVersion;
import com.owncloud.android.lib.resources.shares.OCShare;
Expand Down Expand Up @@ -109,6 +110,8 @@ public class OperationsService extends Service {
public static final String EXTRA_SHARE_ID = "SHARE_ID";
public static final String EXTRA_SHARE_NOTE = "SHARE_NOTE";
public static final String EXTRA_IN_BACKGROUND = "IN_BACKGROUND";
public static final String EXTRA_SHARE_TOKEN = "SHARE_TOKEN";
public static final String EXTRA_SHARE_DOWNLOAD_LIMIT = "SHARE_DOWNLOAD_LIMIT";

public static final String ACTION_CREATE_SHARE_VIA_LINK = "CREATE_SHARE_VIA_LINK";
public static final String ACTION_CREATE_SECURE_FILE_DROP = "CREATE_SECURE_FILE_DROP";
Expand All @@ -129,6 +132,7 @@ public class OperationsService extends Service {
public static final String ACTION_COPY_FILE = "COPY_FILE";
public static final String ACTION_CHECK_CURRENT_CREDENTIALS = "CHECK_CURRENT_CREDENTIALS";
public static final String ACTION_RESTORE_VERSION = "RESTORE_VERSION";
public static final String ACTION_GET_SHARE_DOWNLOAD_LIMIT = "GET_SHARE_DOWNLOAD_LIMIT";

private ServiceHandler mOperationsHandler;
private OperationsServiceBinder mOperationsBinder;
Expand Down Expand Up @@ -643,6 +647,12 @@ private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
updateShare.setLabel(operationIntent.getStringExtra(EXTRA_SHARE_PUBLIC_LABEL));
}

//download limit for link share type
if (operationIntent.hasExtra(EXTRA_SHARE_DOWNLOAD_LIMIT)) {
updateShare.setDownloadLimit(operationIntent.getLongExtra(EXTRA_SHARE_DOWNLOAD_LIMIT,
0L));
}

operation = updateShare;
}
break;
Expand Down Expand Up @@ -734,6 +744,13 @@ private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
fileVersion.getFileName());
break;

case ACTION_GET_SHARE_DOWNLOAD_LIMIT:
String shareToken = operationIntent.getStringExtra(EXTRA_SHARE_TOKEN);
if (!TextUtils.isEmpty(shareToken)) {
operation = new GetShareDownloadLimitOperation(shareToken);
}
break;

default:
// do nothing
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2016 ownCloud Inc.
* Copyright (C) 2019 Chris Narkiewicz <[email protected]>
* Copyright (C) 2021 TSI-mc
* Copyright (C) 2023 TSI-mc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -66,6 +66,8 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.download_limit.GetShareDownloadLimitOperation;
import com.owncloud.android.lib.resources.download_limit.model.DownloadLimitResponse;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.operations.CreateShareViaLinkOperation;
Expand All @@ -89,6 +91,7 @@
import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
import com.owncloud.android.ui.fragment.FileDetailFragment;
import com.owncloud.android.ui.fragment.FileDetailSharingFragment;
import com.owncloud.android.ui.fragment.FileDetailsSharingProcessFragment;
import com.owncloud.android.ui.fragment.OCFileListFragment;
import com.owncloud.android.ui.helpers.FileOperationsHelper;
import com.owncloud.android.ui.preview.PreviewImageActivity;
Expand Down Expand Up @@ -415,6 +418,8 @@ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationRe
onUpdateShareInformation(result, R.string.unsharing_failed);
} else if (operation instanceof UpdateNoteForShareOperation) {
onUpdateNoteForShareOperationFinish(result);
} else if (operation instanceof GetShareDownloadLimitOperation) {
onShareDownloadLimitFetched(result);
}
}

Expand Down Expand Up @@ -846,6 +851,24 @@ private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation ope
}
}

/**
* method will be called when download limit is fetched
*
* @param result
*/
private void onShareDownloadLimitFetched(RemoteOperationResult result) {
FileDetailSharingFragment sharingFragment = getShareFileFragment();

if (result.isSuccess() && sharingFragment != null) {
if (result.isSuccess() && result.getResultData() != null) {
if (result.getResultData() instanceof DownloadLimitResponse) {
onLinkShareDownloadLimitFetched(((DownloadLimitResponse) result.getResultData()).getLimit(),
((DownloadLimitResponse) result.getResultData()).getCount());
}
}
}
}

/**
* Shortcut to get access to the {@link FileDetailSharingFragment} instance, if any
*
Expand Down Expand Up @@ -923,6 +946,14 @@ public void editExistingShare(OCShare share, int screenTypePermission, boolean i
}
}

@Override
public void onLinkShareDownloadLimitFetched(long downloadLimit, long downloadCount) {
Fragment fileDetailsSharingProcessFragment = getSupportFragmentManager().findFragmentByTag(FileDetailsSharingProcessFragment.TAG);
if (fileDetailsSharingProcessFragment != null) {
((FileDetailsSharingProcessFragment) fileDetailsSharingProcessFragment).onLinkShareDownloadLimitFetched(downloadLimit, downloadCount);
}
}

/**
* callback triggered on closing/finishing the sharing process
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Copyright (C) 2018 Andy Scherzinger
* Copyright (C) 2020 Chris Narkiewicz <[email protected]>
* Copyright (C) 2020 TSI-mc
* Copyright (C) 2023 TSI-mc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Expand Down Expand Up @@ -551,5 +551,7 @@ void editExistingShare(OCShare share, int screenTypePermission, boolean isReshar
boolean isExpiryDateShown);

void onShareProcessClosed();

void onLinkShareDownloadLimitFetched(long downloadLimit, long downloadCount);
}
}
Loading