Giter Site home page Giter Site logo

sendyhalim / swime Goto Github PK

View Code? Open in Web Editor NEW
179.0 3.0 36.0 17.22 MB

πŸ—‚ Swift MIME type checking based on magic bytes

License: MIT License

Swift 93.69% PostScript 1.57% Makefile 0.80% Ruby 2.72% Rich Text Format 1.22%
swift mime-types mime mime-parser mimetype

swime's Introduction

πŸ—‚ Swime

Swift check MIME type based on magic bytes. Swime detects MIME type of a Data, ported from sindresorhus/file-type

Build Status

Maintainers wanted

This project is not under active development, please go to maintainers ticket if you're interested.

Maintainers Wanted

Installation

Swift Package Manager

import PackageDescription

let package = Package(
  name: "MyApp",
  dependencies: [
    .Package(url: "https://github.com/sendyhalim/Swime", majorVersion: 3)
  ]
)

Usage

Inspect mime type

import Swime

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

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

switch mimeType?.type {
  case .jpg?:
    ....
  case .png?:
    ....
  case .wmv?:
    ....

  case ...
}

Pass bytes to it

import Swime

let bytes: [UInt8] = [255, 216, 255]
let mimeType = Swime.mimeType(bytes: bytes)

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

MimeTypeExtension

Here are the list of available MimeTypeExtension.

public enum MimeTypeExtension {
  case amr
  case ar
  case avi
  case bmp
  case bz2
  case cab
  case cr2
  case crx
  case deb
  case dmg
  case eot
  case epub
  case exe
  case flac
  case flif
  case flv
  case gif
  case gz
  case ico
  case jpg
  case jxr
  case lz
  case m4a
  case m4v
  case mid
  case mkv
  case mov
  case mp3
  case mp4
  case mpg
  case msi
  case mxf
  case nes
  case ogg
  case opus
  case otf
  case pdf
  case png
  case ps
  case psd
  case rar
  case rpm
  case rtf
  case sevenZ // 7z, Swift does not let us define enum that starts with a digit
  case sqlite
  case swf
  case tar
  case tif
  case ttf
  case wav
  case webm
  case webp
  case wmv
  case woff
  case woff2
  case xpi
  case xz
  case z
  case zip
  case heic
}

Testing

make test

swime's People

Contributors

alobaili avatar buh avatar gdavis avatar hishnash avatar idomo avatar makehui avatar muhtasimtanmoy avatar nimajalali avatar sendyhalim avatar theoriginalbit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swime's Issues

Bad m4v bytes array

Hello!
Faced with incorrect matches bytes for m4v format.

Actual: [0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x56]
Worked: [0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34]
Checked at more than 1 file.

Please make a fix. Thanks!

Quick depends version

hi, please up to date Swime version in u dep packages

because Swime 3.0.7 depends on Quick 1.3.4..<2.0.0 and no versions of Swime match the requirement 3.0.8..<4.0.0, Swime >=3.0.7 requires Quick 1.3.4..<2.0.0.
And because root depends on Quick 3.1.2..<4.0.0 and root depends on Swime 3.0.7..<4.0.0, version solving failed.

Determine extension from String/URL

Hi,
First of all, thanks for this great helper!

I thought that if there is already a list of extensions, maybe an option to get a MimeType from String/NSString/URL/NSURL won’t be that hard-to-add functionality and I think it would be an great addition to this helper.

Hope you’ll be able to add this soon,
Ido.

Swime-library when change target to iOS 11

Swime-library does not specify a Swift version and none of the targets (Pods) integrating it have the SWIFT_VERSION attribute set. Please contact the author or set the SWIFT_VERSION attribute in at least one of the targets that integrate this pod.

stepping may behave oddly; variables may not be available - Read bytes from file data

using Swift 5, Xcode 10.2

im using it like this:

 for file in data {
                if let mimeType = Swime.mimeType(data: file) {
                    multipartFormData.append(file, withName: "dumdum", fileName: "file.\(mimeType.ext)", mimeType: mimeType.mime)
                }

the issue happens in the Swime Struct itself,
in this function:

  internal func readBytes(count: Int) -> [UInt8] {
    var bytes = [UInt8](repeating: 0, count: count)

    data.copyBytes(to: &bytes, count: count)

    return bytes
  }

ps: the count param value is equal to 262

im getting the following in my console:

Swime was compiled with optimization - stepping may behave oddly; variables may not be available.

Looking for maintainers for this project

Hi guys πŸ‘‹ ,

I'm Sendy, current maintainer of this project. I created Swime for a hobby project requirement in my sparetime, it's truly a fun project πŸ–οΈ ! But after some years of it, life happens and the projects I'm working on do not intersected with Swift anymore, that being said, the spare time that I have to work on this project is much-much less than what it used to be.

I'm looking for people to help out maintaining this project, I can help with the repository logistics (moving ownership to an open github organization e.g. Swime/Swime, etc).

Please feel free to comment on this issue if you're interested, also feel free to give inputs as well.

Cheers 🍻 ,
Sendy

Crash when using a fake png with 15 bytes

Hello everyone,

I stubbled upon this package after a long Google search and landing on: https://sendyhalim.github.io/blog/posts/swift/detect-mime-type-with-swime/

Thank you very much for this package.

I decided to use it on my Vapor app to get the mime type of file uploads (not trusting the user provided mime type and extension).

I created a file called fake.png with the following 15 bytes:

evil text here

On my application I have the following code in my request handler:

guard
    let data = request.body.data, // This is a SwiftNIO.ByteBuffer
    let bytes = data.getBytes(at: 0, length: data.readableBytes) // This is a [UInt8] with count 15
else {
    throw Abort(.badRequest)
}

guard
    let mimeType = Swime.mimeType(bytes: bytes), // crash here
    mimeType.mime.lowercased().contains("image")
else {
    throw Abort(.unsupportedMediaType)
}

Specifically, the crash happens on line 577 in MimeType.swift

image

Is this a bug in the package or am I somehow using it incorrectly?
I think there might be a better way to handle this case.

Trunk Please

As far as I know, the latest version of Swime on CocoaPods is 3.0.1, and the Swime.podspec file has not been updated to the correct state in this repo. Do you give up support for CocoaPods?😒

AAC file type ?

First thanks for this great library !

I would like to know why there is no .aac file type ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.