From 6dbf3ba9654ef612208f9cbf8f53ae2ec7b2211d Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Fri, 23 May 2025 15:04:08 +0800 Subject: [PATCH 1/4] Use better fileId based thumbnail endpoint Signed-off-by: Claudio Cambra --- .../NextcloudFileProviderKit/Metadata/ItemMetadata.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift index 53c7d04d..c08d12a5 100644 --- a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift +++ b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift @@ -162,15 +162,13 @@ public extension ItemMetadata { guard hasPreview else { return nil } - let urlBase = urlBase.urlEncoded! // Leave the leading slash in webdavUrl let webdavUrl = urlBase + Account.webDavFilesUrlSuffix + user let serverFileRelativeUrl = serverUrl.replacingOccurrences(of: webdavUrl, with: "") + "/" + fileName - - let urlString = - "\(urlBase)/index.php/core/preview.png?file=\(serverFileRelativeUrl)&x=\(size.width)&y=\(size.height)&a=1&mode=cover" - return URL(string: urlString) + return URL( + string: "\(urlBase)/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=1" + ) } } From dd8f31ddd83a2ddceceaaa564f6f42cdde91dd27 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Fri, 23 May 2025 15:04:30 +0800 Subject: [PATCH 2/4] If available, use safer URL-based thumbnail url construction Signed-off-by: Claudio Cambra --- .../Metadata/ItemMetadata.swift | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift index c08d12a5..f8aae56f 100644 --- a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift +++ b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift @@ -162,13 +162,23 @@ public extension ItemMetadata { guard hasPreview else { return nil } - let urlBase = urlBase.urlEncoded! - // Leave the leading slash in webdavUrl - let webdavUrl = urlBase + Account.webDavFilesUrlSuffix + user - let serverFileRelativeUrl = - serverUrl.replacingOccurrences(of: webdavUrl, with: "") + "/" + fileName - return URL( - string: "\(urlBase)/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=1" - ) + guard #available(macOS 13.0, iOS 16.0, visionOS 1.0, *) else { + let urlBase = urlBase.urlEncoded! + // Leave the leading slash in webdavUrl + let webdavUrl = urlBase + Account.webDavFilesUrlSuffix + user + let serverFileRelativeUrl = + serverUrl.replacingOccurrences(of: webdavUrl, with: "") + "/" + fileName + return URL( + string: "\(urlBase)/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=1" + ) + } + return URL(string: urlBase)? + .appending(components: "index.php", "core", "preview") + .appending(queryItems: [ + .init(name: "fileId", value: fileId), + .init(name: "x", value: "\(size.width)"), + .init(name: "y", value: "\(size.height)"), + .init(name: "a", value: "true") + ]) } } From 41433a5b0d99e9da6a0c4097308663c417cb9b1b Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Fri, 23 May 2025 15:11:59 +0800 Subject: [PATCH 3/4] Simplify thumbnail url constructions Signed-off-by: Claudio Cambra --- .../NextcloudFileProviderKit/Metadata/ItemMetadata.swift | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift index f8aae56f..dc27e8ea 100644 --- a/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift +++ b/Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift @@ -163,16 +163,11 @@ public extension ItemMetadata { return nil } guard #available(macOS 13.0, iOS 16.0, visionOS 1.0, *) else { - let urlBase = urlBase.urlEncoded! - // Leave the leading slash in webdavUrl - let webdavUrl = urlBase + Account.webDavFilesUrlSuffix + user - let serverFileRelativeUrl = - serverUrl.replacingOccurrences(of: webdavUrl, with: "") + "/" + fileName return URL( - string: "\(urlBase)/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=1" + string: "\(urlBase.urlEncoded ?? "")/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=true" ) } - return URL(string: urlBase)? + return URL(string: urlBase.urlEncoded ?? "")? .appending(components: "index.php", "core", "preview") .appending(queryItems: [ .init(name: "fileId", value: fileId), From 87ff073941e7597b2f512b48e28994d0111f2128 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Fri, 23 May 2025 15:13:58 +0800 Subject: [PATCH 4/4] Add test for item metadata thumbnail url Signed-off-by: Claudio Cambra --- .../ItemMetadataTests.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Tests/NextcloudFileProviderKitTests/ItemMetadataTests.swift diff --git a/Tests/NextcloudFileProviderKitTests/ItemMetadataTests.swift b/Tests/NextcloudFileProviderKitTests/ItemMetadataTests.swift new file mode 100644 index 00000000..4ba3b3f3 --- /dev/null +++ b/Tests/NextcloudFileProviderKitTests/ItemMetadataTests.swift @@ -0,0 +1,23 @@ +// +// ItemMetadataTests.swift +// NextcloudFileProviderKit +// +// Created by Claudio Cambra on 23/5/25. +// + +import Foundation +import Testing +@testable import NextcloudFileProviderKit + +struct ItemMetadataTests { + @Test func thumbnailUrlCorrect() { + let account = + Account(user: "user", id: "id", serverUrl: "https://examplecloud.com", password: "bla") + var item = SendableItemMetadata(ocId: "ec-test", fileName: "test.txt", account: account) + item.fileId = "test" + item.hasPreview = true + let expectedUrl = URL(string: "https://examplecloud.com/index.php/core/preview?fileId=test&x=250.0&y=250.0&a=true") + #expect(expectedUrl != nil) + #expect(item.thumbnailUrl(size: .init(width: 250, height: 250)) == expectedUrl) + } +}