Skip to content

Commit b9b563e

Browse files
Merge pull request #163 from nextcloud/etagresource
etagResource
2 parents 13a1e67 + 670524a commit b9b563e

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public extension NextcloudKit {
411411
/// - fileId: The identifier of the file to generate a preview for.
412412
/// - width: The desired width of the preview image (default is 1024).
413413
/// - height: The desired height of the preview image (default is 1024).
414-
/// - etag: Optional entity tag used for caching validation.
414+
/// - etag: Optional entity tag used for caching validation. ()
415415
/// - crop: Indicates whether the image should be cropped (1 = true, default).
416416
/// - cropMode: The cropping mode (default is "cover").
417417
/// - forceIcon: If set to 1, forces icon generation (default is 0).
@@ -423,7 +423,8 @@ public extension NextcloudKit {
423423
func downloadPreview(fileId: String,
424424
width: Int = 1024,
425425
height: Int = 1024,
426-
etag: String? = nil,
426+
etag: String,
427+
etagResource: String? = nil,
427428
crop: Int = 1,
428429
cropMode: String = "cover",
429430
forceIcon: Int = 0,
@@ -432,16 +433,19 @@ public extension NextcloudKit {
432433
options: NKRequestOptions = NKRequestOptions(),
433434
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
434435
completion: @escaping (_ account: String, _ width: Int, _ height: Int, _ etag: String?, _ responseData: AFDataResponse<Data>?, _ error: NKError) -> Void) {
435-
let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)"
436+
//
437+
// 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.
438+
//
439+
let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)&etag=\(etag)"
436440
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
437441
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options),
438442
var headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else {
439443
return options.queue.async { completion(account, width, height, nil, nil, .urlError) }
440444
}
441445

442-
if var etag = etag {
443-
etag = "\"" + etag + "\""
444-
headers.update(name: "If-None-Match", value: etag)
446+
if var etagResource = etagResource {
447+
etagResource = "\"" + etagResource + "\""
448+
headers.update(name: "If-None-Match", value: etagResource)
445449
}
446450

447451
nkSession.sessionData.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in
@@ -476,7 +480,8 @@ public extension NextcloudKit {
476480
func downloadPreviewAsync(fileId: String,
477481
width: Int = 1024,
478482
height: Int = 1024,
479-
etag: String? = nil,
483+
etag: String,
484+
etagResource: String? = nil,
480485
crop: Int = 1,
481486
cropMode: String = "cover",
482487
forceIcon: Int = 0,
@@ -497,6 +502,7 @@ public extension NextcloudKit {
497502
width: width,
498503
height: height,
499504
etag: etag,
505+
etagResource: etagResource,
500506
crop: crop,
501507
cropMode: cropMode,
502508
forceIcon: forceIcon,
@@ -631,7 +637,7 @@ public extension NextcloudKit {
631637
fileNameLocalPath: String,
632638
sizeImage: Int,
633639
avatarSizeRounded: Int = 0,
634-
etag: String?,
640+
etagResource: String?,
635641
account: String,
636642
options: NKRequestOptions = NKRequestOptions(),
637643
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
@@ -643,9 +649,9 @@ public extension NextcloudKit {
643649
return options.queue.async { completion(account, nil, nil, nil, nil, .urlError) }
644650
}
645651

646-
if var etag = etag {
647-
etag = "\"" + etag + "\""
648-
headers.update(name: "If-None-Match", value: etag)
652+
if var etagResource = etagResource {
653+
etagResource = "\"" + etagResource + "\""
654+
headers.update(name: "If-None-Match", value: etagResource)
649655
}
650656

651657
nkSession.sessionData.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in
@@ -743,7 +749,7 @@ public extension NextcloudKit {
743749
fileNameLocalPath: String,
744750
sizeImage: Int,
745751
avatarSizeRounded: Int = 0,
746-
etag: String?,
752+
etagResource: String?,
747753
account: String,
748754
options: NKRequestOptions = NKRequestOptions(),
749755
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
@@ -760,7 +766,7 @@ public extension NextcloudKit {
760766
fileNameLocalPath: fileNameLocalPath,
761767
sizeImage: sizeImage,
762768
avatarSizeRounded: avatarSizeRounded,
763-
etag: etag,
769+
etagResource: etagResource,
764770
account: account,
765771
options: options,
766772
taskHandler: taskHandler) { account, imageAvatar, imageOriginal, etag, responseData, error in

0 commit comments

Comments
 (0)