2022-03-29 16:00:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 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
|
|
|
|
import UniformTypeIdentifiers
|
2023-01-12 23:49:28 +03:00
|
|
|
import NextcloudKit
|
2022-03-29 16:00:59 +03:00
|
|
|
|
|
|
|
class FileProviderItem: NSObject, NSFileProviderItem {
|
|
|
|
|
2023-01-12 23:49:28 +03:00
|
|
|
enum FileProviderItemTransferError: Error {
|
|
|
|
case downloadError
|
|
|
|
case uploadError
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
2023-01-12 23:49:28 +03:00
|
|
|
|
|
|
|
let metadata: NextcloudItemMetadataTable
|
|
|
|
let parentItemIdentifier: NSFileProviderItemIdentifier
|
2022-03-29 16:00:59 +03:00
|
|
|
|
|
|
|
var itemIdentifier: NSFileProviderItemIdentifier {
|
2023-01-12 23:49:28 +03:00
|
|
|
return NSFileProviderItemIdentifier(metadata.ocId)
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var capabilities: NSFileProviderItemCapabilities {
|
2023-01-12 23:49:28 +03:00
|
|
|
guard !metadata.directory else {
|
|
|
|
return [ .allowsAddingSubItems,
|
|
|
|
.allowsContentEnumerating,
|
|
|
|
.allowsReading,
|
|
|
|
.allowsDeleting,
|
|
|
|
.allowsRenaming ]
|
|
|
|
}
|
|
|
|
guard !metadata.lock else {
|
|
|
|
return [ .allowsReading ]
|
|
|
|
}
|
|
|
|
return [ .allowsWriting,
|
|
|
|
.allowsReading,
|
|
|
|
.allowsDeleting,
|
|
|
|
.allowsRenaming,
|
|
|
|
.allowsReparenting ]
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var itemVersion: NSFileProviderItemVersion {
|
2023-01-12 23:49:28 +03:00
|
|
|
NSFileProviderItemVersion(contentVersion: metadata.etag.data(using: .utf8)!,
|
|
|
|
metadataVersion: metadata.etag.data(using: .utf8)!)
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var filename: String {
|
2023-01-12 23:49:28 +03:00
|
|
|
return metadata.fileNameView
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var contentType: UTType {
|
2023-01-12 23:49:28 +03:00
|
|
|
if self.itemIdentifier == .rootContainer || metadata.directory {
|
|
|
|
return .folder
|
|
|
|
}
|
|
|
|
|
|
|
|
let internalType = NKCommon.shared.getInternalType(fileName: metadata.fileNameView,
|
|
|
|
mimeType: "",
|
|
|
|
directory: metadata.directory)
|
|
|
|
return UTType(filenameExtension: internalType.ext) ?? .content
|
|
|
|
}
|
|
|
|
|
|
|
|
var documentSize: NSNumber? {
|
|
|
|
return NSNumber(value: metadata.size)
|
|
|
|
}
|
|
|
|
|
|
|
|
var creationDate: Date? {
|
|
|
|
return metadata.creationDate as Date
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastUsedDate: Date? {
|
|
|
|
return metadata.date as Date
|
|
|
|
}
|
|
|
|
|
|
|
|
var isDownloaded: Bool {
|
|
|
|
return metadata.directory || isFileSynced(metadata: metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
var isDownloading: Bool {
|
|
|
|
return metadata.status == NextcloudItemMetadataTable.Status.downloading.rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
var downloadingError: Error? {
|
|
|
|
if metadata.status == NextcloudItemMetadataTable.Status.downloadError.rawValue {
|
|
|
|
return FileProviderItemTransferError.downloadError
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var isUploaded: Bool {
|
|
|
|
return NextcloudFilesDatabaseManager.shared.localFileMetadataFromOcId(metadata.ocId) != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var isUploading: Bool {
|
|
|
|
return metadata.status == NextcloudItemMetadataTable.Status.uploading.rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadingError: Error? {
|
|
|
|
if metadata.status == NextcloudItemMetadataTable.Status.uploadError.rawValue {
|
|
|
|
return FileProviderItemTransferError.uploadError
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(metadata: NextcloudItemMetadataTable, parentItemIdentifier: NSFileProviderItemIdentifier) {
|
|
|
|
self.metadata = metadata
|
|
|
|
self.parentItemIdentifier = parentItemIdentifier
|
|
|
|
super.init()
|
2022-03-29 16:00:59 +03:00
|
|
|
}
|
|
|
|
}
|