Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
flocked committed Apr 17, 2024
1 parent 811cf7b commit 266dec0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 69 deletions.
6 changes: 5 additions & 1 deletion Acknowledgment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ AssociatedValues
- Copyright (c) 2014 Skyvive <brad.hilton.nw@gmail.com>
- http://github.com/bradhilton/AssociatedValues

ApproximateEquatable
- Copyright (c) 2020, Adam Bell
- https://github.com/b3ll/Motion

DateFormatComponents
- Copyright (c) 2021 Yurii Lysytsia.
- https://github.com/yurii-lysytsia/AirKit
Expand Down Expand Up @@ -32,5 +36,5 @@ Swizzle


LoremSwiftum
- Copyright (c) 2014-2021 Lukas Kubanek.
- Copyright (c) 2014-2021 Lukas Kubanek
- https://github.com/lukaskubanek/LoremSwiftum
36 changes: 18 additions & 18 deletions Sources/FZSwiftUtils/Extensions/File & URL/FileManager+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
import Foundation

public extension FileManager {
/// An enumeration of file manager errors.
enum Errors: Error {
/// An error that occures if a file coudnl't be moved to trash.
case failedToMoveToTrash
}

/**
Creates a temporary directory inside the file system's default temporary directory.
- Throws: Throws if the temporary directory couldn't be created.
Expand All @@ -31,7 +25,7 @@ public extension FileManager {
return folderURL
}

#if os(macOS) || os(iOS)
#if os(macOS) || os(iOS)
/**
Moves an item to the trash.
Expand All @@ -50,7 +44,13 @@ public extension FileManager {
}
return fileURL
}
#endif

/// An enumeration of file manager errors.
enum Errors: Error {
/// An error that occures if a file coudnl't be moved to trash.
case failedToMoveToTrash
}
#endif

#if os(macOS)
/// The type of appliction support directory.
Expand All @@ -69,17 +69,17 @@ public extension FileManager {
- createIfNeeded: A Boolean value indicating whether the directory should be created if it doesn't exist. The default value is `false`.
*/
func applicationSupportDirectory(using type: ApplicationSupportDirectoryType = .name, createIfNeeded: Bool = false) -> URL? {
if let appSupportURL = urls(for: .applicationSupportDirectory, in: .userDomainMask).first, let pathComponent = (type == .name) ? Bundle.main.bundleName : Bundle.main.bundleIdentifier {
let directoryURL = appSupportURL.appendingPathComponent(pathComponent)
if directoryExists(at: directoryURL) {
guard let appSupportURL = urls(for: .applicationSupportDirectory, in: .userDomainMask).first else { return nil }
guard let name = type == .identifier ? Bundle.main.bundleIdentifier ?? Bundle.main.bundleName : Bundle.main.bundleName ?? Bundle.main.bundleIdentifier else { return nil }
let directoryURL = appSupportURL.appendingPathComponent(name)
if FileManager.default.directoryExists(at: directoryURL) {
return directoryURL
} else if createIfNeeded {
do {
try createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
return directoryURL
} else if createIfNeeded {
do {
try createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
return directoryURL
} catch {
debugPrint(error)
}
} catch {
debugPrint(error)
}
}
return nil
Expand Down
88 changes: 38 additions & 50 deletions Sources/FZSwiftUtils/Extensions/File & URL/FileType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,45 +101,33 @@ public enum FileType: Hashable, CustomStringConvertible, CaseIterable, Codable {

/// Returns the type for the specified file extension.
public init?(fileExtension: String) {
let fileExtension = fileExtension.lowercased()
if fileExtension == "" {
self = .folder
return
}

if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
} else if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
if let contentType = UTType(filenameExtension: fileExtension), let fileType = FileType(contentType: contentType) {
self = fileType
return
} else {
return nil
}
}

let fileExtension = fileExtension.lowercased()
if let fileType = FileType.allCases.first(where: { $0.commonExtensions.contains(fileExtension) }) {
} else if let fileType = FileType.allCases.first(where: { $0.commonExtensions.contains(fileExtension) }) {
self = fileType
return
} else {
return nil
}
}

#if canImport(UniformTypeIdentifiers)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
/// Returns the type for the specified content type.
public init?(contentType: UTType) {
if let fileType = FileType.allCases.first(where: {
if let allContentType = $0.contentType, contentType.conforms(to: allContentType) {
return true
}
return false
}) {
self = fileType
} else if let pathExtension = contentType.preferredFilenameExtension {
self = .other(pathExtension)
} else {
return nil
}
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
/// Returns the type for the specified content type.
public init?(contentType: UTType) {
if let fileType = FileType.allCases.filter({$0.contentType != nil}).first(where: {contentType.conforms(to: $0.contentType!)}) {
self = fileType
} else if let fileExtension = contentType.preferredFilenameExtension {
self = FileType.allCases.first(where: {$0.commonExtensions.contains(fileExtension)}) ?? .other(fileExtension)
} else {
return nil
}
#endif
}
}

@available(macOS, deprecated: 11.0, message: "Use contentType instead")
Expand Down Expand Up @@ -188,20 +176,25 @@ public extension FileType {
case .text: return "public.text"
case .document: return "public.composite-content"
case let .other(pathExtension):
guard pathExtension != "" else {
return "public.folder"
}
if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
if let identifier = UTType(filenameExtension: pathExtension)?.identifier {
return identifier
}
if pathExtension == "" { return "public.folder" }
if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *), let identifier = UTType(filenameExtension: pathExtension)?.identifier {
return identifier
}
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue(), let mimeIdentifier = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimeIdentifier as String
}
return nil
}
}

@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
/// The content type of the file type.
var contentType: UTType? {
if let identifier = identifier {
return UTType(identifier)
}
return nil
}

/// The most common file extensions of the file type.
var commonExtensions: [String] {
Expand Down Expand Up @@ -254,29 +247,23 @@ public extension FileType {

/// A Boolean value indicating whether the file type is a multimedia type (either `audio`, `video`, `image` or `gif`).
var isMultimedia: Bool {
self == .video || self == .audio || self == .gif || self == .image
Self.multimediaTypes.contains(self)
}

/// A Boolean value indicating whether the file type is an image type (either `image` or `gif`).
var isImageType: Bool {
Self.imageTypes.contains(self)
}

/// An array of all file types.
/// All file types.
static let allCases: [FileType] = [.aliasFile, .symbolicLink, .folder, .application, .executable, .video, .audio, .gif, .image, .archive, .diskImage, .document, .pdf, .presentation, .text]

/// An array of all multimedia file types (`audio`, `video`, `image` and `gif`).
static var multimediaTypes: [FileType] = [.gif, .image, .video]
/// All multimedia file types (`audio`, `video`, `image` and `gif`).
static var multimediaTypes: [FileType] = [.gif, .image, .video, .audio]

/// An array of all image file types (`image` and `gif`).
/// All image file types (`image` and `gif`).
static var imageTypes: [FileType] = [.gif, .image]

#if canImport(UniformTypeIdentifiers)
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
/// The content type of the file type.
var contentType: UTType? {
if let identifier = identifier {
return UTType(identifier)
}
return nil
}
#endif

internal var predicate: NSPredicate {
let key: NSExpression
let type: NSComparisonPredicate.Operator
Expand Down Expand Up @@ -315,6 +302,7 @@ public extension FileType {
default:
modifier = .direct
}

return NSComparisonPredicate(leftExpression: key, rightExpression: value, modifier: modifier, type: type)
}
}

0 comments on commit 266dec0

Please sign in to comment.