https://github.com/jaywcjlove/filetype
This tool checks a file’s MIME type using magic bytes and can retrieve the file extension from the MIME type.
https://github.com/jaywcjlove/filetype
file file-type swift swiftui
Last synced: 8 months ago
JSON representation
This tool checks a file’s MIME type using magic bytes and can retrieve the file extension from the MIME type.
- Host: GitHub
- URL: https://github.com/jaywcjlove/filetype
- Owner: jaywcjlove
- License: mit
- Created: 2025-08-14T14:35:24.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-18T14:43:36.000Z (9 months ago)
- Last Synced: 2025-10-01T06:36:38.108Z (9 months ago)
- Topics: file, file-type, swift, swiftui
- Language: Swift
- Homepage:
- Size: 46.9 KB
- Stars: 29
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
FileType
===
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](https://github.com/sendyhalim/Swime) and ported from [file-type](https://www.npmjs.com/package/file-type).
The [`Extensions`](./Sources/FileType/Supported.swift), [`mimeTypes`](./Sources/FileType/Supported.swift), and [`mimeTypesAll`](./Sources/FileType/Supported.swift) data in the dependency package are all generated by scripts based on [file-type](https://www.npmjs.com/package/file-type).
The function for checking whether the bytes match the `MimeType` specification is generated from a [`mapping.js`](./scripts/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`](./scripts/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:
```swift
dependencies: [
.package(url: "https://github.com/jaywcjlove/FileType.git", from: "1.0.0")
]
```
## Usage
Inspect mime type
```swift
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
```swift
let avroMimeType = MimeType.mimeTypesAll.first { $0.mime == "application/avro" }
if let avroMimeType = avroMimeType {
avroMimeType.mime // "application/avro"
avroMimeType.type // .avro
avroMimeType.type.rawValue // "avro"
}
```
```swift
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.