MIME component

Swift MIT License Actions Status Codacy Badge Contributing Twitter

Installation

Swift

Download and install Swift

Swift Package

mkdir MyApp
cd MyApp
swift package init --type executable // Creates an executable app named "MyApp"

Package.swift

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [
        .package(name: "chaqmoq-mime", url: "https://github.com/chaqmoq/mime.git", from: "1.1.0"),
    ],
    targets: [
        .target(name: "MyApp", dependencies: [
            .product(name: "MIME", package: "chaqmoq-mime"),
        ]),
        .testTarget(name: "MyAppTests", dependencies: [
            .target(name: "MyApp")
        ])
    ]
)

Build

swift build -c release

Usage

main.swift

import MIME

var mime = MIME()
print(mime) // "application/octet-stream"
print(mime.ext) // nil

mime = MIME(type: "text", subtype: "html")
print(mime) // "text/html"
print(mime.ext) // "html"

mime = MIME("application/java-archive")
print(mime) // "application/java-archive"
print(mime.ext) // "jar"

mime = MIME("application/java-archive", ext: "war")
print(mime) // "application/java-archive"
print(mime.ext) // "war"

mime = MIME(ext: "css")
print(mime) // "text/css"
print(mime.ext) // "css"

mime = MIME(path: "/public/js/main.js")
print(mime) // "text/javascript"
print(mime.ext) // "js"

mime = MIME(url: URL(string: "https://chaqmoq.dev/public/img/logo.png")!)
print(mime) // "image/png"
print(mime.ext) // "png"

let data = Data([0xFF, 0xD8, 0xFF, ...])
mime = MIME.guess(from: data)
print(mime) // "image/jpeg"
print(mime.ext) // "jpg"

let bytes: [UInt8] = [0x47, 0x49, 0x46, ...]
mime = MIME.guess(from: bytes)
print(mime) // "image/gif"
print(mime.ext) // "gif"

Run

swift run

Tests

swift test --enable-test-discovery --sanitize=thread