Skip to content

Latest commit

 

History

History
353 lines (285 loc) · 17.4 KB

File metadata and controls

353 lines (285 loc) · 17.4 KB

FileType

The file type is detected by checking signature bytes and, for some container formats, scanning the full file contents when required.

This is swift port of file-type

Documentation: Swift Package Index DocC

Installation

Requirements

  • Swift 6.2+
  • Xcode 26.3+ or another Swift 6.2-compatible toolchain

Swift Package Manager

import PackageDescription

let package = Package(
  name: "MyApp",
  dependencies: [
    .package(url: "https://github.com/velocityzen/FileType", branch: "main")
  ]
)

If you are consuming a tagged release instead of main, use the latest Swift 6.2-compatible tag.

Usage

Inspect mime type

import FileType

let path = "/path/to/some-file.jpg"
let url = URL(fileURLWithPath: path, isDirectory: false)
let fileType = try FileType.detect(contentsOf: url)

fileType?.type == .jpg // true
fileType // FileType(type: .jpg, ext: "jpg", mime: "image/jpeg")

Limit detection to MIME major groups when you already know the broad file family:

import FileType

let data = try Data(contentsOf: url)
let imageType = FileType.detect(in: data, matching: .image)

imageType?.mimeGroup == .image // true

Inspect the detection requirement before sampling file contents:

import FileType

switch FileType.dataRequirement(for: .docx) {
case let .prefix(byteCount):
  // Read only the prefix for simple signatures.
  print(byteCount)
case .fullFile:
  // Container formats like DOCX require the full file.
  print("Read the full file")
}

FileType.all(for: FileTypeExtension) -> [FileType]

returns all file types and mime information

FileType.all(for: FileTypeMIMEGroup) -> [FileType]

returns all file types whose mime begins with that major type

FileType.detect(in: Data) -> FileType?

returns file type detected by checking the magic number

FileType.detect(in: Data, matching: FileTypeMIMEGroup) -> FileType?

limits detection to a MIME major group such as .image, .audio, or .video

FileType.detect(contentsOf: URL) throws -> FileType?

detects the type from a file URL and only loads the full file when a detector requires it

FileType.detect(contentsOf: URL, matching: FileTypeMIMEGroup) throws -> FileType?

limits URL-based detection to one MIME major group

FileType.detect(using: FileHandle) throws -> FileType?

detects the type from a file handle positioned at the start of the file

FileType.detect(using: FileHandle, matching: FileTypeMIMEGroup) throws -> FileType?

limits file-handle detection to one MIME major group

FileType.minimumPrefixBytes(for: FileTypeExtension) -> Int

returns the minimum prefix size used by the detector for that type

FileType.minimumPrefixBytes(for: FileTypeMIMEGroup) -> Int

returns the minimum prefix size needed for that MIME major group

FileType.dataRequirement(for: FileTypeExtension) -> FileTypeDataRequirement

returns whether the detector can work from a prefix or requires the full file

FileType.dataRequirement(for: FileTypeMIMEGroup) -> FileTypeDataRequirement

returns whether that MIME major group can work from a prefix or requires the full file

FileTypeExtension.canonicalFileExtension -> String

returns the canonical on-disk extension for a public file type case

FileType.mimeGroup -> FileTypeMIMEGroup

returns the first component of the detected MIME type

UTI Detection (Apple platforms)

On Apple platforms with UniformTypeIdentifiers available (macOS 11+, iOS 14+, tvOS 14+, watchOS 7+, visionOS 1+), you can detect file types from UTI identifiers:

import FileType

// From a UTI identifier string
let fileType = FileType.detect(uti: "public.jpeg")
fileType?.type == .jpg // true
import FileType
import UniformTypeIdentifiers

// From a UTType value
let fileType = FileType.detect(utType: .png)
fileType?.type == .png // true

FileType.detect(uti: String) -> FileType?

resolves a UTI identifier string to a file type using the system's UTType mapping

FileType.detect(utType: UTType) -> FileType?

resolves a UTType value to a file type using its MIME type and file extension

Deprecated compatibility APIs

getFor(...) and getBytesCountFor(...) remain as deprecated wrappers for older callers.

Supported file types

  • 3g2 - Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services
  • 3gp - Multimedia container format defined by the Third Generation Partnership Project (3GPP) for 3G UMTS multimedia services
  • 3mf - 3D Manufacturing Format
  • 7z
  • aac - Advanced Audio Coding
  • ac3 - ATSC A/52 Audio File
  • ace
  • ai - Adobe Illustrator Artwork
  • aif
  • alias - macOS Alias file
  • amr
  • ape - Monkey's Audio
  • apk
  • apng - Animated Portable Network Graphics
  • ar
  • arj
  • arrow - Columnar format for tables of data
  • arw - Sony Alpha Raw image file
  • asar - Simple extensive tar-like archive format with indexing
  • asf - Advanced Systems Format
  • avi
  • avif - AV1 Image File Format
  • avro
  • blend
  • bmp
  • bpg
  • bz2
  • cab
  • cfb
  • chm - Microsoft Compiled HTML Help
  • cpio
  • cr2 - Canon Raw image file (v2)
  • cr3 - Canon Raw image file (v3)
  • crx
  • cur
  • dat - Windows Registry hive
  • dcm - DICOM Image File
  • deb
  • dmg
  • dng - Adobe Digital Negative image file
  • docm
  • docx
  • dotm
  • dotx
  • drc
  • dwg
  • dsf - Sony DSD Stream File (DSF)
  • elf - Unix Executable and Linkable Format
  • eot
  • eps - Encapsulated PostScript
  • epub
  • exe
  • f4a - Audio-only ISO base media file format used by Adobe Flash Player
  • f4b - Audiobook and podcast ISO base media file format used by Adobe Flash Player
  • f4p - ISO base media file format protected by Adobe Access DRM used by Adobe Flash Player
  • f4v - ISO base media file format used by Adobe Flash Player
  • fbx
  • flac
  • flif
  • flv
  • gif
  • glb - GL Transmission Format
  • gz
  • heic
  • icc
  • icns
  • ico
  • ics - iCalendar
  • ism - Microsoft Smooth Streaming manifest
  • indd
  • it - Audio module format: Impulse Tracker
  • j2c - JPEG 2000 codestream
  • jar
  • jp2 - JPEG 2000
  • jpg
  • jls
  • jmp - JMP data file
  • jpm - JPEG 2000
  • jpx - JPEG 2000
  • jxl - JPEG XL image format
  • jxr
  • ktx
  • lnk - Microsoft Windows file shortcut
  • lz
  • lz4
  • lzh - LZH archive
  • m3u - HLS playlist when HLS-specific #EXT-X- tags are present
  • m4a - Audio-only MPEG-4 files
  • m4b - Audiobook and podcast MPEG-4 files, which also contain metadata including chapter markers, images, and hyperlinks
  • m4p - MPEG-4 files with audio streams encrypted by FairPlay Digital Rights Management as were sold through the iTunes Store
  • m4v - MPEG-4 Visual bitstreams
  • macho
  • mid
  • mie - Dedicated meta information format which supports storage of binary as well as textual meta information
  • mj2 - Motion JPEG 2000
  • mkv
  • mobi - Mobipocket
  • mov
  • mp1 - MPEG-1 Audio Layer I
  • mp2
  • mp3
  • mp4
  • mpd - MPEG-DASH manifest
  • mpc - Musepack (SV7 & SV8)
  • mpg
  • mts - MPEG-2 Transport Stream, both raw and Blu-ray Disc Audio-Video (BDAV) versions
  • mxf
  • nef - Nikon Electronic Format image file
  • nes
  • odg - OpenDocument graphics
  • odp - OpenDocument for presentations
  • ods - OpenDocument for spreadsheets
  • odt - OpenDocument for word processing
  • oga
  • ogg
  • ogm
  • ogv
  • ogx
  • opus
  • orf - Olympus Raw image file
  • otf
  • otg - OpenDocument graphics template
  • otp - OpenDocument presentation template
  • ots - OpenDocument spreadsheet template
  • ott - OpenDocument text template
  • parquet
  • pcap - Libpcap File Format
  • pdf
  • pgp - Pretty Good Privacy
  • png
  • ppsm
  • ppsx
  • potm
  • potx
  • pptm
  • pptx
  • ps
  • psd
  • pst
  • qcp
  • raf - Fujifilm RAW image file
  • rar
  • reg - Windows Registry export
  • rm
  • rpm
  • rtf
  • rw2 - Panasonic RAW image file
  • s3m - Audio module format: ScreamTracker 3
  • sav - SPSS data file
  • shp - Geospatial vector data format
  • skp - SketchUp
  • spx
  • sqlite
  • stl - Standard Tesselated Geometry File Format (ASCII only)
  • swf
  • tar
  • tif
  • ttc - TrueType Collection
  • ttf
  • vcf - vCard
  • voc - Creative Voice File
  • vsdx
  • vtt
  • wasm
  • wav
  • webm
  • webp
  • woff2
  • woff
  • wv - WavPack
  • xcf - eXperimental Computing Facility
  • xlsm
  • xlsx
  • xltm
  • xltx
  • xm - Audio module format: FastTracker 2
  • xml
  • xpi
  • xz
  • Z
  • zip
  • zst - Archive file

Pull requests are welcome for additional commonly used file types.

Testing

swift test