Swiftpack.co - velocityzen/FileType as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by velocityzen.
velocityzen/FileType 1.1.2
File type and mime detection by magic bytes
⭐️ 4
🕓 3 weeks ago
.package(url: "https://github.com/velocityzen/FileType.git", from: "1.1.2")

FileType

The file type is detected by checking the magic number of the data.

This is swift port of file-type

Installation

Swift Package Manager

import PackageDescription

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

Usage

Inspect mime type

import FileType

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

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

.getFor(type: FileTypeExtension) -> [FileType]

returns all file types and mime information

.getFor(data: Data) -> FileType?

returns file type detected by checking the magic number

.getBytesCountFor(type: FileTypeExtension) -> Int

returns bytes count needed to detect file type

.getBytesCountFor(types: [FileTypeExtension]) -> Int

returns max bytes count needed to detect file types

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
  • ai - Adobe Illustrator Artwork
  • aif
  • alias - macOS Alias file
  • amr
  • ape - Monkey's Audio
  • apng - Animated Portable Network Graphics
  • ar
  • 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
  • blend
  • bmp
  • bpg
  • bz2
  • cab
  • cfb
  • chm - Microsoft Compiled HTML Help
  • cr2 - Canon Raw image file (v2)
  • cr3 - Canon Raw image file (v3)
  • crx
  • cur
  • dcm - DICOM Image File
  • deb
  • dmg
  • dng - Adobe Digital Negative image file
  • docx
  • 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
  • flac
  • flif
  • flv
  • gif
  • glb - GL Transmission Format
  • gz
  • heic
  • icns
  • ico
  • ics - iCalendar
  • indd
  • it - Audio module format: Impulse Tracker
  • jp2 - JPEG 2000
  • jpg
  • jpm - JPEG 2000
  • jpx - JPEG 2000
  • jxl - JPEG XL image format
  • jxr
  • ktx
  • lnk - Microsoft Windows file shortcut
  • lz
  • lzh - LZH archive
  • 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
  • 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
  • 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
  • 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
  • pcap - Libpcap File Format
  • pdf
  • pgp - Pretty Good Privacy
  • png
  • pptx
  • ps
  • psd
  • qcp
  • raf - Fujifilm RAW image file
  • rar
  • rpm
  • rtf
  • rw2 - Panasonic RAW image file
  • s3m - Audio module format: ScreamTracker 3
  • shp - Geospatial vector data format
  • skp - SketchUp
  • spx
  • sqlite
  • stl - Standard Tesselated Geometry File Format (ASCII only)
  • swf
  • tar
  • tif
  • ttf
  • vcf - vCard
  • voc - Creative Voice File
  • wasm
  • wav
  • webm
  • webp
  • woff2
  • woff
  • wv - WavPack
  • xcf - eXperimental Computing Facility
  • xlsx
  • 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

GitHub

link
Stars: 4
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.1.2
3 weeks ago

Improve epub detection

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics