Full generation, file type cleanup
This commit is contained in:
240
CHDataManagement/Model/FileType.swift
Normal file
240
CHDataManagement/Model/FileType.swift
Normal file
@ -0,0 +1,240 @@
|
||||
import Foundation
|
||||
|
||||
enum FileTypeCategory: String, CaseIterable {
|
||||
case image
|
||||
case code
|
||||
case model
|
||||
case text
|
||||
case video
|
||||
case resource
|
||||
case asset
|
||||
case audio
|
||||
|
||||
var text: String {
|
||||
switch self {
|
||||
case .image: return "Images"
|
||||
case .code: return "Code"
|
||||
case .model: return "Models"
|
||||
case .text: return "Text"
|
||||
case .video: return "Videos"
|
||||
case .asset: return "Assets"
|
||||
case .resource: return "Other"
|
||||
case .audio: return "Audio"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension FileTypeCategory: Hashable {
|
||||
|
||||
}
|
||||
|
||||
extension FileTypeCategory: Identifiable {
|
||||
|
||||
var id: String {
|
||||
rawValue
|
||||
}
|
||||
}
|
||||
|
||||
enum FileType: String {
|
||||
|
||||
// MARK: Images
|
||||
|
||||
case jpg
|
||||
|
||||
case png
|
||||
|
||||
case avif
|
||||
|
||||
case webp
|
||||
|
||||
case gif
|
||||
|
||||
case svg
|
||||
|
||||
case tiff
|
||||
|
||||
// MARK: Code
|
||||
|
||||
case html
|
||||
|
||||
case cpp
|
||||
|
||||
case swift
|
||||
|
||||
// MARK: Assets
|
||||
|
||||
case css
|
||||
|
||||
case js
|
||||
|
||||
// MARK: Text
|
||||
|
||||
case json
|
||||
|
||||
case conf
|
||||
|
||||
case yaml
|
||||
|
||||
// MARK: Model
|
||||
|
||||
case stl
|
||||
|
||||
case f3d
|
||||
|
||||
case step
|
||||
|
||||
case glb
|
||||
|
||||
case f3z
|
||||
|
||||
// MARK: Video
|
||||
|
||||
case mp4
|
||||
|
||||
case m4v
|
||||
|
||||
case webm
|
||||
|
||||
// MARK: Audio
|
||||
|
||||
case mp3
|
||||
|
||||
case aac
|
||||
|
||||
// MARK: Other
|
||||
|
||||
case noExtension
|
||||
|
||||
case zip
|
||||
|
||||
case cddx
|
||||
|
||||
case pdf
|
||||
|
||||
case key
|
||||
|
||||
case psd
|
||||
|
||||
// MARK: Unknown
|
||||
|
||||
case unknown
|
||||
|
||||
init(fileExtension: String?) {
|
||||
guard let lower = fileExtension?.lowercased() else {
|
||||
self = .noExtension
|
||||
return
|
||||
}
|
||||
if lower == "jpeg" {
|
||||
self = .jpg
|
||||
return
|
||||
}
|
||||
guard let type = FileType(rawValue: lower) else {
|
||||
self = .unknown
|
||||
return
|
||||
}
|
||||
self = type
|
||||
}
|
||||
|
||||
var category: FileTypeCategory {
|
||||
switch self {
|
||||
case .jpg, .png, .avif, .webp, .gif, .svg, .tiff:
|
||||
return .image
|
||||
case .mp4, .m4v, .webm:
|
||||
return .video
|
||||
case .mp3, .aac:
|
||||
return .audio
|
||||
case .js, .css:
|
||||
return .asset
|
||||
case .json, .conf, .yaml:
|
||||
return .text
|
||||
case .html, .cpp, .swift:
|
||||
return .code
|
||||
case .stl, .f3d, .step, .glb, .f3z:
|
||||
return .model
|
||||
case .zip, .cddx, .pdf, .key, .psd:
|
||||
return .resource
|
||||
case .noExtension, .unknown:
|
||||
return .resource
|
||||
}
|
||||
}
|
||||
|
||||
var fileExtension: String {
|
||||
switch self {
|
||||
case .noExtension, .unknown: return ""
|
||||
default:
|
||||
return rawValue
|
||||
}
|
||||
}
|
||||
|
||||
var isImage: Bool {
|
||||
switch self {
|
||||
case .jpg, .png, .avif, .webp, .gif, .svg, .tiff:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isVideo: Bool {
|
||||
switch self {
|
||||
case .mp4, .m4v, .webm:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isAudio: Bool {
|
||||
switch self {
|
||||
case .mp3, .aac:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isAsset: Bool {
|
||||
switch self {
|
||||
case .js, .css:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isTextFile: Bool {
|
||||
switch self {
|
||||
case .html, .cpp, .swift, .css, .js, .json, .conf, .yaml:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var isOtherFile: Bool {
|
||||
switch self {
|
||||
case .noExtension, .zip, .cddx, .pdf, .key, .psd:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var htmlType: String? {
|
||||
switch self {
|
||||
case .mp4, .m4v:
|
||||
return "video/mp4"
|
||||
case .webm:
|
||||
return "video/webm"
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var isSvg: Bool {
|
||||
guard case .svg = self else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user