2018-05-28 16:45:36 +03:00
|
|
|
//
|
|
|
|
// FileProviderExtension+Thumbnail.swift
|
|
|
|
// PickerFileProvider
|
|
|
|
//
|
|
|
|
// Created by Marino Faggiana on 28/05/18.
|
2018-10-20 11:36:00 +03:00
|
|
|
// Copyright © 2018 Marino Faggiana. All rights reserved.
|
2018-05-28 16:45:36 +03:00
|
|
|
//
|
2018-11-29 14:13:29 +03:00
|
|
|
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
|
2018-06-28 16:13:25 +03:00
|
|
|
//
|
|
|
|
// 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 3 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
2018-05-28 16:45:36 +03:00
|
|
|
|
2021-05-12 09:17:32 +03:00
|
|
|
import UIKit
|
2018-05-28 16:45:36 +03:00
|
|
|
import FileProvider
|
2022-09-03 11:54:18 +03:00
|
|
|
import NextcloudKit
|
2018-05-28 16:45:36 +03:00
|
|
|
|
|
|
|
extension FileProviderExtension {
|
|
|
|
override func fetchThumbnails(for itemIdentifiers: [NSFileProviderItemIdentifier], requestedSize size: CGSize, perThumbnailCompletionHandler: @escaping (NSFileProviderItemIdentifier, Data?, Error?) -> Void, completionHandler: @escaping (Error?) -> Void) -> Progress {
|
|
|
|
let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
|
|
|
|
var counterProgress: Int64 = 0
|
2021-12-16 16:41:14 +03:00
|
|
|
|
2018-05-28 16:45:36 +03:00
|
|
|
for itemIdentifier in itemIdentifiers {
|
2024-07-26 10:09:58 +03:00
|
|
|
guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier), metadata.hasPreview else {
|
2019-04-02 17:09:32 +03:00
|
|
|
counterProgress += 1
|
2021-12-16 16:41:14 +03:00
|
|
|
if counterProgress == progress.totalUnitCount { completionHandler(nil) }
|
2019-04-02 17:09:32 +03:00
|
|
|
continue
|
|
|
|
}
|
2024-07-26 10:09:58 +03:00
|
|
|
let fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)
|
|
|
|
|
2024-08-07 13:24:28 +03:00
|
|
|
NextcloudKit.shared.downloadPreview(fileId: metadata.fileId, widthPreview: Int(size.width), heightPreview: Int(size.height), etag: metadata.etag, account: metadata.account) { _ in
|
2024-07-26 10:09:58 +03:00
|
|
|
} completion: { _, data, error in
|
|
|
|
if error == .success, let data {
|
|
|
|
do {
|
|
|
|
try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
|
|
|
|
} catch { }
|
|
|
|
perThumbnailCompletionHandler(itemIdentifier, data, nil)
|
|
|
|
} else {
|
|
|
|
perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
|
2019-11-11 13:19:29 +03:00
|
|
|
}
|
2018-05-28 16:45:36 +03:00
|
|
|
counterProgress += 1
|
2021-12-16 16:41:14 +03:00
|
|
|
if counterProgress == progress.totalUnitCount {
|
2020-07-07 18:24:00 +03:00
|
|
|
completionHandler(nil)
|
|
|
|
}
|
2018-05-28 16:45:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return progress
|
|
|
|
}
|
|
|
|
}
|