Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
This tool checks a file’s MIME type using magic bytes and can retrieve the file extension from the MIME type.
License
jaywcjlove/FileType
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 ofData, based onSwime and ported fromfile-type.
TheExtensions,mimeTypes, andmimeTypesAll data in the dependency package are all generated by scripts based onfile-type.
The function for checking whether the bytes match theMimeType specification is generated from amapping.js mapping file created using data from thefile-type package.
Since the data may not always be accurate, it can be corrected by modifying themapping.js file.
Add CodeMirror to your project using Xcode:
- In Xcode, go to
File→Add Package Dependencies... - Enter the repository URL:
https://github.com/jaywcjlove/FileType.git - Click
Add Package
Or add it to yourPackage.swift file:
dependencies:[.package(url:"https://github.com/jaywcjlove/FileType.git", from:"1.0.0")]
Inspect mime type
import FileTypeletpath="/path/to/some-file.jpg"leturl=URL(fileURLWithPath: path, isDirectory:false)letdata=try!Data(contentsOf: url)letmimeType=FileType.mimeType(data: data)mimeType?.type==.jpg // truemimeType! // MimeType(mime: "image/jpeg", ext: "jpg", type: .jpg)
Get the file extension from a MIME type
letavroMimeType=MimeType.mimeTypesAll.first{ $0.mime=="application/avro"}iflet avroMimeType= avroMimeType{ avroMimeType.mime // "application/avro" avroMimeType.type // .avro avroMimeType.type.rawValue // "avro"}
letmimeTypes=MimeType.mimeTypes.first(where:{ $0.key=="application/mp4"})iflet mimeTypes= mimeTypes{ mimeTypes.compressible mimeTypes.extensions // ["mp4","mpg4","mp4s","m4p"]}
Thanks to these projects:
- https://github.com/sendyhalim/Swime
- https://github.com/sindresorhus/file-type
- https://github.com/jshttp/mime-db
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
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.