2023-04-06 12:03:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import FileProvider
|
2024-01-22 09:40:47 +03:00
|
|
|
import Foundation
|
2023-04-06 12:03:59 +03:00
|
|
|
import NextcloudKit
|
|
|
|
import OSLog
|
|
|
|
|
|
|
|
extension FileProviderExtension: NSFileProviderThumbnailing {
|
2024-01-22 09:40:47 +03:00
|
|
|
func fetchThumbnails(
|
|
|
|
for itemIdentifiers: [NSFileProviderItemIdentifier],
|
|
|
|
requestedSize size: CGSize,
|
|
|
|
perThumbnailCompletionHandler: @escaping (
|
|
|
|
NSFileProviderItemIdentifier,
|
|
|
|
Data?,
|
|
|
|
Error?
|
|
|
|
) -> Void,
|
|
|
|
completionHandler: @escaping (Error?) -> Void
|
|
|
|
) -> Progress {
|
2023-04-06 12:03:59 +03:00
|
|
|
let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
|
|
|
|
var progressCounter: Int64 = 0
|
|
|
|
|
|
|
|
func finishCurrent() {
|
|
|
|
progressCounter += 1
|
|
|
|
|
|
|
|
if progressCounter == progress.totalUnitCount {
|
|
|
|
completionHandler(nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for itemIdentifier in itemIdentifiers {
|
2024-01-22 09:40:47 +03:00
|
|
|
Logger.fileProviderExtension.debug(
|
|
|
|
"Fetching thumbnail for item with identifier:\(itemIdentifier.rawValue, privacy: .public)"
|
|
|
|
)
|
|
|
|
guard
|
|
|
|
let metadata = NextcloudFilesDatabaseManager.shared
|
|
|
|
.itemMetadataFromFileProviderItemIdentifier(itemIdentifier),
|
|
|
|
let thumbnailUrl = metadata.thumbnailUrl(size: size)
|
|
|
|
else {
|
2023-04-06 12:03:59 +03:00
|
|
|
Logger.fileProviderExtension.debug("Did not fetch thumbnail URL")
|
|
|
|
finishCurrent()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
Logger.fileProviderExtension.debug(
|
|
|
|
"Fetching thumbnail for file:\(metadata.fileName) at:\(thumbnailUrl.absoluteString, privacy: .public)"
|
|
|
|
)
|
2023-04-06 12:03:59 +03:00
|
|
|
|
2024-01-22 09:40:47 +03:00
|
|
|
ncKit.getPreview(url: thumbnailUrl) { _, data, error in
|
|
|
|
if error == .success, data != nil {
|
2023-04-06 12:03:59 +03:00
|
|
|
perThumbnailCompletionHandler(itemIdentifier, data, nil)
|
|
|
|
} else {
|
2024-01-22 09:40:47 +03:00
|
|
|
perThumbnailCompletionHandler(
|
|
|
|
itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
|
2023-04-06 12:03:59 +03:00
|
|
|
}
|
|
|
|
finishCurrent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress
|
|
|
|
}
|
|
|
|
}
|