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
|
2024-10-24 14:55:27 +03:00
|
|
|
import Alamofire
|
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
|
|
|
|
2024-10-24 14:55:27 +03:00
|
|
|
NextcloudKit.shared.downloadPreview(fileId: metadata.fileId,
|
|
|
|
width: Int(size.width),
|
|
|
|
height: Int(size.height),
|
|
|
|
etag: metadata.etag,
|
|
|
|
account: metadata.account) { _ in
|
|
|
|
} completion: { _, _, _, _, responseData, error in
|
|
|
|
if error == .success, let data = responseData?.data {
|
2024-07-26 10:09:58 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|