Skip to content

Latest commit

 

History

85 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Pull requests are welcome for additional commonly used file types.

Testing

swift test

About

File type and mime detection by magic bytes

Topics

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages