Skip to content
Merged

code #218

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 63 additions & 96 deletions Sources/NextcloudKit/TypeIdentifiers/NKFilePropertyResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum NKTypeIconFile: String {
case compress = "compress"
case directory = "directory"
case document = "document"
case draw = "draw"
case image = "image"
case video = "video"
case pdf = "pdf"
Expand All @@ -44,119 +45,85 @@ public final class NKFilePropertyResolver {

public init() {}

public func resolve(inUTI: String, capabilities: NKCapabilities.Capabilities) -> NKFileProperty {
public func resolve(
mimeType: String,
fileExtension: String,
typeIdentifier: String,
capabilities: NKCapabilities.Capabilities
) -> NKFileProperty {

let fileProperty = NKFileProperty()
let typeIdentifier = inUTI as String
let utiString = inUTI as String

// Preferred extension
if let type = UTType(utiString),
let ext = type.preferredFilenameExtension {
fileProperty.ext = ext
// MARK: - Custom MIME types

switch mimeType {

case "application/vnd.excalidraw+json":
fileProperty.classFile = .document
fileProperty.iconName = .draw
fileProperty.name = "whiteboard"
fileProperty.ext = "whiteboard"
return fileProperty

default:
break
}

// Collabora Nextcloud Text Office
if capabilities.richDocumentsMimetypes.contains(typeIdentifier) {
// MARK: - Collabora / Office

if capabilities.richDocumentsMimetypes.contains(mimeType) {
fileProperty.classFile = .document
fileProperty.iconName = .document
fileProperty.name = "document"
return fileProperty
}

// MARK: - Resolve UTType

guard let type =
UTType(mimeType: mimeType) ??
UTType(typeIdentifier)
else {
fileProperty.classFile = .unknow
fileProperty.iconName = .unknow
fileProperty.name = "file"
return fileProperty
}

// Special-case identifiers
switch typeIdentifier {
case "text/plain", "text/html", "net.daringfireball.markdown", "text/x-markdown":
// MARK: - Type conformance

if type.conforms(to: .image) {

fileProperty.classFile = .image
fileProperty.iconName = .image
fileProperty.name = "image"

} else if type.conforms(to: .movie) {

fileProperty.classFile = .video
fileProperty.iconName = .video
fileProperty.name = "movie"

} else if type.conforms(to: .audio) {

fileProperty.classFile = .audio
fileProperty.iconName = .audio
fileProperty.name = "audio"

} else if type.conforms(to: .text) {

fileProperty.classFile = .document
fileProperty.iconName = .document
fileProperty.name = "markdown"
return fileProperty
case "com.microsoft.word.doc":
fileProperty.iconName = .txt
fileProperty.name = "text"

} else if type.conforms(to: .content) {

fileProperty.classFile = .document
fileProperty.iconName = .document
fileProperty.name = "document"
return fileProperty
case "com.apple.iwork.keynote.key":
fileProperty.classFile = .document
fileProperty.iconName = .ppt
fileProperty.name = "keynote"
return fileProperty
case "com.microsoft.excel.xls":
fileProperty.classFile = .document
fileProperty.iconName = .xls
fileProperty.name = "sheet"
return fileProperty
case "com.apple.iwork.numbers.numbers":
fileProperty.classFile = .document
fileProperty.iconName = .xls
fileProperty.name = "numbers"
return fileProperty
case "com.microsoft.powerpoint.ppt":
fileProperty.classFile = .document
fileProperty.iconName = .ppt
fileProperty.name = "presentation"
default:
break
}

// Well-known UTI type classifications
if let type = UTType(utiString) {
if type.conforms(to: .image) {
fileProperty.classFile = .image
fileProperty.iconName = .image
fileProperty.name = "image"

} else if type.conforms(to: .movie) {
fileProperty.classFile = .video
fileProperty.iconName = .video
fileProperty.name = "movie"

} else if type.conforms(to: .audio) {
fileProperty.classFile = .audio
fileProperty.iconName = .audio
fileProperty.name = "audio"

} else if type.conforms(to: .zip) {
fileProperty.classFile = .compress
fileProperty.iconName = .compress
fileProperty.name = "archive"

} else if type.conforms(to: .html) {
fileProperty.classFile = .document
fileProperty.iconName = .code
fileProperty.name = "code"

} else if type.conforms(to: .pdf) {
fileProperty.classFile = .document
fileProperty.iconName = .pdf
fileProperty.name = "document"

} else if type.conforms(to: .rtf) {
fileProperty.classFile = .document
fileProperty.iconName = .txt
fileProperty.name = "document"

} else if type.conforms(to: .text) {
// Default to .txt if extension is empty
if fileProperty.ext.isEmpty {
fileProperty.ext = "txt"
}
fileProperty.classFile = .document
fileProperty.iconName = .txt
fileProperty.name = "text"

} else if type.conforms(to: .content) {
fileProperty.classFile = .document
fileProperty.iconName = .document
fileProperty.name = "document"

} else {
fileProperty.classFile = .unknow
fileProperty.iconName = .unknow
fileProperty.name = "file"
}
} else {
// tipo UTI non valido

fileProperty.classFile = .unknow
fileProperty.iconName = .unknow
fileProperty.name = "file"
Expand Down
Loading
Loading