Skip to content

Commit b4ba990

Browse files
cod
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent a6dfbc9 commit b4ba990

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 13 additions & 8 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,17 +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 etagResource: String = etag ?? ""
436-
let endpoint = "index.php/core/preview?fileId=\(fileId)&x=\(width)&y=\(height)&a=\(crop)&mode=\(cropMode)&forceIcon=\(forceIcon)&mimeFallback=\(mimeFallback)&etag=\(etagResource)"
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)"
437440
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
438441
let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options),
439442
var headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else {
440443
return options.queue.async { completion(account, width, height, nil, nil, .urlError) }
441444
}
442445

443-
if var etag = etag {
444-
etag = "\"" + etag + "\""
445-
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)
446449
}
447450

448451
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 {
477480
func downloadPreviewAsync(fileId: String,
478481
width: Int = 1024,
479482
height: Int = 1024,
480-
etag: String? = nil,
483+
etag: String,
484+
etagResource: String? = nil,
481485
crop: Int = 1,
482486
cropMode: String = "cover",
483487
forceIcon: Int = 0,
@@ -498,6 +502,7 @@ public extension NextcloudKit {
498502
width: width,
499503
height: height,
500504
etag: etag,
505+
etagResource: etagResource,
501506
crop: crop,
502507
cropMode: cropMode,
503508
forceIcon: forceIcon,

0 commit comments

Comments
 (0)