GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - jaywcjlove/FileType: This tool checks a file’s MIME type using magic bytes and can retrieve the file extension from the MIME type.

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
Sources/FileType
Sources/FileType
 
 
Tests/FileTypeTests
Tests/FileTypeTests
 
 
scripts
scripts
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 

Repository files navigation

Using my app is also a way to support me:
Scap: Screenshot & Markup Edit Screen Test Deskmark Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Menuist Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

FileType

Buy me a coffee Follow On X

This tool detects a file’s MIME type using magic bytes and can retrieve the file extension based on the MIME type.
It can identify the MIME type of Data, based on Swime and ported from file-type.

The Extensions, mimeTypes, and mimeTypesAll data in the dependency package are all generated by scripts based on file-type.

The function for checking whether the bytes match the MimeType specification is generated from a mapping.js mapping file created using data from the file-type package.
Since the data may not always be accurate, it can be corrected by modifying the mapping.js file.

Installation

Swift Package Manager

Add CodeMirror to your project using Xcode:

  1. In Xcode, go to File → Add Package Dependencies...
  2. Enter the repository URL: https://github.com/jaywcjlove/FileType.git
  3. Click Add Package

Or add it to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/jaywcjlove/FileType.git", from: "1.0.0")
]

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 mimeType = FileType.mimeType(data: data)

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

Get the file extension from a MIME type

let avroMimeType = MimeType.mimeTypesAll.first { $0.mime == "application/avro" }
if let avroMimeType = avroMimeType {
    avroMimeType.mime   // "application/avro"
    avroMimeType.type   // .avro
    avroMimeType.type.rawValue // "avro"
}
let mimeTypes = MimeType.mimeTypes.first(where: { $0.key == "application/mp4" })
if let mimeTypes = mimeTypes {
    mimeTypes.compressible
    mimeTypes.extensions // ["mp4","mpg4","mp4s","m4p"]
}

Acknowledgments

Thanks to these projects:

  • https://github.com/sendyhalim/Swime
  • https://github.com/sindresorhus/file-type
  • https://github.com/jshttp/mime-db

License

Licensed under the MIT License.

About

This tool checks a file’s MIME type using magic bytes and can retrieve the file extension from the MIME type.

Topics

filefile-typeswiftswiftui

Resources

Readme
MIT license
Activity

Stars

36 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Sponsor this project

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.