media

package
v1.0.0-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

package media provides capabilities for inspecting the content of data and identifying its media (Multipurpose Internet Mail Extensions, MIME) type.

Example (Switch)
package main

import (
	"github.com/brexhq/substation/internal/media"
)

func main() {
	bytes := [][]byte{
		// application/x-bzip2
		[]byte("\x42\x5a\x68"),
		// application/x-gzip
		[]byte("\x1f\x8b\x08"),
		// text/html
		[]byte("\x3c\x68\x74\x6d\x6c\x3e"),
	}

	for _, b := range bytes {
		// use a switch statement to contextually distribute data to other functions
		switch media.Bytes(b) {
		case "application/x-bzip2":
			continue
			// bzip2(b)
		case "application/x-gzip":
			continue
			// gzip(b)
		case "text/html; charset=utf-8":
			continue
			// html(b)
		default:
			continue
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(b []byte) string

Bytes returns the media type of a byte slice.

Example
package main

import (
	"fmt"

	"github.com/brexhq/substation/internal/media"
)

func main() {
	b := []byte("\x42\x5a\x68")
	mediaType := media.Bytes(b)

	fmt.Println(mediaType)
}
Output:

application/x-bzip2

func File

func File(f *os.File) (string, error)

File returns the media type of an open file. The caller is responsible for resetting the position of the file.

Example
package main

import (
	"fmt"
	"os"

	"github.com/brexhq/substation/internal/media"
)

func main() {
	// temp file is used to simulate an open file and must be removed after the test completes
	file, _ := os.CreateTemp("", "substation")
	defer os.Remove(file.Name())
	defer file.Close()

	_, _ = file.Write([]byte("\x42\x5a\x68"))

	// media.File moves the file offset to zero
	mediaType, err := media.File(file)
	if err != nil {
		// handle err
		panic(err)
	}

	fmt.Println(mediaType)
}
Output:

application/x-bzip2

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL