From a6dfbc96c5ba4ec2febf8b209f2a23111a496761 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Wed, 9 Jul 2025 08:49:45 +0200 Subject: [PATCH 1/3] added etag Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index a56c89a9..f740ad9f 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -432,7 +432,8 @@ public extension NextcloudKit { options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ width: Int, _ height: Int, _ etag: String?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { - let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)" + let etagResource: String = etag ?? "" + let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)&etag=\(etagResource)" guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account), let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), var headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { From b4ba99079bba13ad6308f03dc5a57a2fc72ecc2e Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Wed, 9 Jul 2025 09:04:47 +0200 Subject: [PATCH 2/3] cod Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index f740ad9f..fb2f4738 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -411,7 +411,7 @@ public extension NextcloudKit { /// - fileId: The identifier of the file to generate a preview for. /// - width: The desired width of the preview image (default is 1024). /// - height: The desired height of the preview image (default is 1024). - /// - etag: Optional entity tag used for caching validation. + /// - etag: Optional entity tag used for caching validation. () /// - crop: Indicates whether the image should be cropped (1 = true, default). /// - cropMode: The cropping mode (default is "cover"). /// - forceIcon: If set to 1, forces icon generation (default is 0). @@ -423,7 +423,8 @@ public extension NextcloudKit { func downloadPreview(fileId: String, width: Int = 1024, height: Int = 1024, - etag: String? = nil, + etag: String, + etagResource: String? = nil, crop: Int = 1, cropMode: String = "cover", forceIcon: Int = 0, @@ -432,17 +433,19 @@ public extension NextcloudKit { options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ width: Int, _ height: Int, _ etag: String?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { - let etagResource: String = etag ?? "" - let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)&etag=\(etagResource)" + // + // Adding the etag as a parameter in the endpoint URL is used to prevent URLCache from being used in case the image has been overwritten. + // + let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)&etag=\(etag)" guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account), let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), var headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { return options.queue.async { completion(account, width, height, nil, nil, .urlError) } } - if var etag = etag { - etag = "\"" + etag + "\"" - headers.update(name: "If-None-Match", value: etag) + if var etagResource = etagResource { + etagResource = "\"" + etagResource + "\"" + headers.update(name: "If-None-Match", value: etagResource) } nkSession.sessionData.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in @@ -477,7 +480,8 @@ public extension NextcloudKit { func downloadPreviewAsync(fileId: String, width: Int = 1024, height: Int = 1024, - etag: String? = nil, + etag: String, + etagResource: String? = nil, crop: Int = 1, cropMode: String = "cover", forceIcon: Int = 0, @@ -498,6 +502,7 @@ public extension NextcloudKit { width: width, height: height, etag: etag, + etagResource: etagResource, crop: crop, cropMode: cropMode, forceIcon: forceIcon, From 670524a063b81673a085761ae28ac420cf154e43 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Wed, 9 Jul 2025 09:06:01 +0200 Subject: [PATCH 3/3] cod Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index fb2f4738..b9239d86 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -637,7 +637,7 @@ public extension NextcloudKit { fileNameLocalPath: String, sizeImage: Int, avatarSizeRounded: Int = 0, - etag: String?, + etagResource: String?, account: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, @@ -649,9 +649,9 @@ public extension NextcloudKit { return options.queue.async { completion(account, nil, nil, nil, nil, .urlError) } } - if var etag = etag { - etag = "\"" + etag + "\"" - headers.update(name: "If-None-Match", value: etag) + if var etagResource = etagResource { + etagResource = "\"" + etagResource + "\"" + headers.update(name: "If-None-Match", value: etagResource) } nkSession.sessionData.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in @@ -749,7 +749,7 @@ public extension NextcloudKit { fileNameLocalPath: String, sizeImage: Int, avatarSizeRounded: Int = 0, - etag: String?, + etagResource: String?, account: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in } @@ -766,7 +766,7 @@ public extension NextcloudKit { fileNameLocalPath: fileNameLocalPath, sizeImage: sizeImage, avatarSizeRounded: avatarSizeRounded, - etag: etag, + etagResource: etagResource, account: account, options: options, taskHandler: taskHandler) { account, imageAvatar, imageOriginal, etag, responseData, error in