-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added capabilities to manage share download limits.
- Requesting share download limit capability of files_downloadlimit app. - Augmented WebDAV metadata requests and responses with optional share download limits. - Extended NextcloudKit with methods to manage share download limits via OCS. Signed-off-by: Iva Horn <[email protected]>
- Loading branch information
Showing
5 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-FileCopyrightText: Nextcloud GmbH | ||
// SPDX-FileCopyrightText: 2024 Iva Horn | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import Foundation | ||
|
||
/// | ||
/// Data model for a download limit as returned in the WebDAV response for file properties. | ||
/// | ||
/// Each relates to a share of a file and is optionally provided by the [Files Download Limit](https://github.com/nextcloud/files_downloadlimit) app for Nextcloud server. | ||
/// | ||
public class NKDownloadLimit: NSObject { | ||
/// | ||
/// The number of downloads which already happened. | ||
/// | ||
public let count: Int | ||
|
||
/// | ||
/// Total number of allowed downloas. | ||
/// | ||
public let limit: Int | ||
|
||
/// | ||
/// The token identifying the related share. | ||
/// | ||
public let token: String | ||
|
||
init(count: Int, limit: Int, token: String) { | ||
self.count = count | ||
self.limit = limit | ||
self.token = token | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Sources/NextcloudKit/NextcloudKit+ShareDownloadLimit.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// SPDX-FileCopyrightText: Nextcloud GmbH | ||
// SPDX-FileCopyrightText: 2024 Iva Horn | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import Alamofire | ||
import Foundation | ||
|
||
public extension NextcloudKit { | ||
private func makeEndpoint(with token: String) -> String { | ||
"ocs/v2.php/apps/files_downloadlimit/api/v1/\(token)/limit" | ||
} | ||
|
||
func removeShareDownloadLimit(account: String, token: String, completion: @escaping (_ error: NKError) -> Void) { | ||
let endpoint = makeEndpoint(with: token) | ||
let options = NKRequestOptions() | ||
|
||
guard let nkSession = nkCommonInstance.getSession(account: account), | ||
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), | ||
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { | ||
return options.queue.async { | ||
completion(.urlError) | ||
} | ||
} | ||
|
||
nkSession | ||
.sessionData | ||
.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil) | ||
.validate(statusCode: 200..<300) | ||
.response(queue: self.nkCommonInstance.backgroundQueue) { response in | ||
if self.nkCommonInstance.levelLog > 0 { | ||
debugPrint(response) | ||
} | ||
|
||
switch response.result { | ||
case .failure(let error): | ||
let error = NKError(error: error, afResponse: response, responseData: response.data) | ||
|
||
options.queue.async { | ||
completion(error) | ||
} | ||
case .success: | ||
options.queue.async { | ||
completion(.success) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func setShareDownloadLimit(account: String, token: String, limit: Int, completion: @escaping (_ error: NKError) -> Void) { | ||
let endpoint = makeEndpoint(with: token) | ||
let options = NKRequestOptions() | ||
options.contentType = "application/json" | ||
|
||
guard let nkSession = nkCommonInstance.getSession(account: account), | ||
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), | ||
let headers = nkCommonInstance.getStandardHeaders(account: account, options: options), | ||
var urlRequest = try? URLRequest(url: url, method: .put, headers: headers) else { | ||
return options.queue.async { | ||
completion(.urlError) | ||
} | ||
} | ||
|
||
urlRequest.httpBody = try? JSONEncoder().encode([ | ||
"limit": limit | ||
]) | ||
|
||
nkSession | ||
.sessionData | ||
.request(urlRequest) | ||
.validate(statusCode: 200..<300) | ||
.response(queue: self.nkCommonInstance.backgroundQueue) { response in | ||
if self.nkCommonInstance.levelLog > 0 { | ||
debugPrint(response) | ||
} | ||
|
||
switch response.result { | ||
case .failure(let error): | ||
let error = NKError(error: error, afResponse: response, responseData: response.data) | ||
|
||
options.queue.async { | ||
completion(error) | ||
} | ||
case .success: | ||
options.queue.async { | ||
completion(.success) | ||
} | ||
} | ||
} | ||
} | ||
} |