Skip to content

Commit 1fd2ee1

Browse files
authored
Merge pull request #79 from claucambra/bugfix/thumbnail-urls
Improve stability of thumbnail url generation
2 parents fac7cf3 + 87ff073 commit 1fd2ee1

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

Sources/NextcloudFileProviderKit/Metadata/ItemMetadata.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,18 @@ public extension ItemMetadata {
162162
guard hasPreview else {
163163
return nil
164164
}
165-
166-
let urlBase = urlBase.urlEncoded!
167-
// Leave the leading slash in webdavUrl
168-
let webdavUrl = urlBase + Account.webDavFilesUrlSuffix + user
169-
let serverFileRelativeUrl =
170-
serverUrl.replacingOccurrences(of: webdavUrl, with: "") + "/" + fileName
171-
172-
let urlString =
173-
"\(urlBase)/index.php/core/preview.png?file=\(serverFileRelativeUrl)&x=\(size.width)&y=\(size.height)&a=1&mode=cover"
174-
return URL(string: urlString)
165+
guard #available(macOS 13.0, iOS 16.0, visionOS 1.0, *) else {
166+
return URL(
167+
string: "\(urlBase.urlEncoded ?? "")/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=true"
168+
)
169+
}
170+
return URL(string: urlBase.urlEncoded ?? "")?
171+
.appending(components: "index.php", "core", "preview")
172+
.appending(queryItems: [
173+
.init(name: "fileId", value: fileId),
174+
.init(name: "x", value: "\(size.width)"),
175+
.init(name: "y", value: "\(size.height)"),
176+
.init(name: "a", value: "true")
177+
])
175178
}
176179
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// ItemMetadataTests.swift
3+
// NextcloudFileProviderKit
4+
//
5+
// Created by Claudio Cambra on 23/5/25.
6+
//
7+
8+
import Foundation
9+
import Testing
10+
@testable import NextcloudFileProviderKit
11+
12+
struct ItemMetadataTests {
13+
@Test func thumbnailUrlCorrect() {
14+
let account =
15+
Account(user: "user", id: "id", serverUrl: "https://examplecloud.com", password: "bla")
16+
var item = SendableItemMetadata(ocId: "ec-test", fileName: "test.txt", account: account)
17+
item.fileId = "test"
18+
item.hasPreview = true
19+
let expectedUrl = URL(string: "https://examplecloud.com/index.php/core/preview?fileId=test&x=250.0&y=250.0&a=true")
20+
#expect(expectedUrl != nil)
21+
#expect(item.thumbnailUrl(size: .init(width: 250, height: 250)) == expectedUrl)
22+
}
23+
}

0 commit comments

Comments
 (0)