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 #1123

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
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ services:
- su www-data -c "git clone https://github.com/nextcloud/photos.git /var/www/html/apps/photos/"
- su www-data -c "cd /var/www/html/apps/photos; composer install"
- su www-data -c "php /var/www/html/occ app:enable -f photos"
- su www-data -c "git clone https://github.com/nextcloud/files_downloadlimit.git /var/www/html/apps/files_downloadlimit/"
- su www-data -c "php /var/www/html/occ app:enable -f files_downloadlimit"
- /usr/local/bin/run.sh

trigger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.resources.download_limit.GetShareDownloadLimitOperation;
import com.owncloud.android.lib.resources.download_limit.UpdateShareDownloadLimitRemoteOperation;
import com.owncloud.android.lib.resources.download_limit.model.DownloadLimitResponse;
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation;
import com.owncloud.android.lib.resources.shares.OCShare;
import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.lib.resources.status.NextcloudVersion;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.List;

/**
* Test create share
Expand Down Expand Up @@ -246,4 +253,36 @@ public void testCreateFederatedShareWithNonExistingFile() {
assertFalse("file doesn't exist", result.isSuccess());
assertEquals("file doesn't exist", ResultCode.FILE_NOT_FOUND, result.getCode());
}

@Test
public void testCreatePublicShareWithDownloadLimit() {
testOnlyOnServer(NextcloudVersion.nextcloud_25);

int downloadLimit = 5;
CreateShareRemoteOperation operation = new CreateShareRemoteOperation(
mFullPath2FileToShare,
ShareType.PUBLIC_LINK,
"",
false,
"",
1);
operation.setGetShareDetails(true);
RemoteOperationResult<List<OCShare>> result = operation.execute(client);
assertTrue(result.isSuccess());
String shareToken = result.getResultData().get(0).getToken();
assertNotNull(shareToken);

assertTrue(new UpdateShareDownloadLimitRemoteOperation(shareToken, downloadLimit)
.execute(client)
.isSuccess()
);

RemoteOperationResult<DownloadLimitResponse> limitOperation =
new GetShareDownloadLimitOperation(shareToken)
.execute(client);

assertTrue(limitOperation.isSuccess());
assertEquals(downloadLimit, limitOperation.getResultData().getLimit());
assertEquals(0, limitOperation.getResultData().getCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* Nextcloud Android Library is available under MIT license
*
* @author TSI-mc
* Copyright (C) 2023 TSI-mc
* Copyright (C) 2023 Nextcloud 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.resources.download_limit

import org.junit.Assert.*
import org.junit.Test

class DownloadLimitXMLParserIT {

@Test
fun parseSuccessResponseWithValues(){
val xml = """<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>200</statuscode>
<message>OK</message>
</meta>
<data>
<limit>5</limit>
<count>1</count>
</data>
</ocs>
"""

val remoteOperationResult = DownloadLimitXMLParser().parse(true, xml)

assertTrue(remoteOperationResult.isSuccess)
assertEquals(5, remoteOperationResult.resultData.limit)
assertEquals(1, remoteOperationResult.resultData.count)
}


@Test
fun parseSuccessResponseWithNoValues(){
val xml = """<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>200</statuscode>
<message>OK</message>
</meta>
<data>
<limit/>
<count/>
</data>
</ocs>
"""

val remoteOperationResult = DownloadLimitXMLParser().parse(true, xml)

assertTrue(remoteOperationResult.isSuccess)
assertEquals(0, remoteOperationResult.resultData.limit)
assertEquals(0, remoteOperationResult.resultData.count)
}

@Test
fun parseSuccessResponseForUpdateDeleteOperations(){
val xml = """<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>200</statuscode>
<message>OK</message>
</meta>
<data/>
</ocs>
"""

val remoteOperationResult = DownloadLimitXMLParser().parse(true, xml)

assertTrue(remoteOperationResult.isSuccess)
}

@Test
fun parseFailResponse(){
val xml = """<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>403</statuscode>
<message>OK</message>
</meta>
<data/>
</ocs>
"""

val remoteOperationResult = DownloadLimitXMLParser().parse(true, xml)

assertFalse(remoteOperationResult.isSuccess)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Nextcloud Android Library is available under MIT license
*
* @author TSI-mc
* Copyright (C) 2023 TSI-mc
* Copyright (C) 2023 Nextcloud 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.resources.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 com.owncloud.android.lib.resources.download_limit.model.DownloadLimitResponse
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
*
*
* API : //DELETE to /ocs/v2.php/apps/files_downloadlimit/{share_token}/limit
*/
class DeleteShareDownloadLimitRemoteOperation(private val shareToken: String) :
RemoteOperation<DownloadLimitResponse>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<DownloadLimitResponse> {
var result: RemoteOperationResult<DownloadLimitResponse>
val status: Int
var deleteMethod: DeleteMethod? = null
try {
// Post Method
deleteMethod = DeleteMethod(
client.baseUri.toString() + ShareDownloadLimitUtils.getDownloadLimitApiPath(
shareToken
)
)
deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
status = client.executeMethod(deleteMethod)
if (isSuccess(status)) {
val response = deleteMethod.responseBodyAsString
Log_OC.d(TAG, "Delete Download Limit response: $response")
val parser = DownloadLimitXMLParser()
result = parser.parse(true, response)
if (result.isSuccess) {
return result
}
} else {
result = RemoteOperationResult<DownloadLimitResponse>(false, deleteMethod)
}
} catch (e: Exception) {
result = RemoteOperationResult<DownloadLimitResponse>(e)
Log_OC.e(TAG, "Exception while deleting share download limit", e)
} finally {
deleteMethod?.releaseConnection()
}
return result
}

private fun isSuccess(status: Int): Boolean {
return status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST
}

companion object {
private val TAG = DeleteShareDownloadLimitRemoteOperation::class.java.simpleName
}
}
Loading
Loading