dataurl

Parse and encode RFC2397 “data’ URL scheme.

SYNOPSIS

Encoding

package examples

import (
  "encoding/base64"
  "fmt"

  "github.com/lestrrat-go/dataurl"
)

func ExampleEncode() {
  { // Let dataurl.Encode fiture out the media type
    encoded, err := dataurl.Encode([]byte(`Hello, World!`))
    if err != nil {
      fmt.Printf("failed to encode: %s", err)
      return
    }
    const expected = `data:text/plain;charset=utf-8,Hello%2C%20World!`
    if string(encoded) != expected {
      fmt.Printf("expected: %q\n", expected)
      fmt.Printf("actual:   %q\n", encoded)
      return
    }
  }

  { // It works on binary files too
    rawimg, err := base64.StdEncoding.DecodeString(gopher)
    if err != nil {
      fmt.Printf("failed to decode gopher image: %s", err)
      return
    }

    encoded, err := dataurl.Encode(rawimg)
    if err != nil {
      fmt.Printf("failed to encode: %s", err)
      return
    }

    var expected = `data:image/png;base64,` + gopher
    if string(encoded) != expected {
      fmt.Printf("expected: %q\n", expected)
      fmt.Printf("actual:   %q\n", encoded)
      return
    }
  }

  { // You can overwrite the media type

    { // First case, only supply the main media type
      encoded, err := dataurl.Encode(
        []byte(`{"Hello":"World!"}`),
        dataurl.WithMediaType(`application/json`),
      )

      if err != nil {
        fmt.Printf("failed to encode: %s", err)
        return
      }
      const expected = `data:application/json;base64,eyJIZWxsbyI6IldvcmxkISJ9`
      if string(encoded) != expected {
        fmt.Printf("expected: %q\n", expected)
        fmt.Printf("actual:   %q\n", encoded)
        return
      }
    }

    { // Second case, supply the mdia type and parameters as a string
      encoded, err := dataurl.Encode(
        []byte(`{"Hello":"World!"}`),
        dataurl.WithMediaType(`application/json; charset=utf-8`),
      )

      if err != nil {
        fmt.Printf("failed to encode: %s", err)
        return
      }
      const expected = `data:application/json;charset=utf-8;base64,eyJIZWxsbyI6IldvcmxkISJ9`
      if string(encoded) != expected {
        fmt.Printf("expected: %q\n", expected)
        fmt.Printf("actual:   %q\n", encoded)
        return
      }
    }

    { // Third case, supply the mdia type as string, and parameters as a map
      // Notice that the parameters OVERWRITE the value given in the string media type
      encoded, err := dataurl.Encode(
        []byte(`{"Hello":"World!"}`),
        dataurl.WithMediaType(`application/json; charset=us-ascii`),
        dataurl.WithMediaTypeParams(map[string]string{
          `charset`: `utf-8`,
        }),
      )

      if err != nil {
        fmt.Printf("failed to encode: %s", err)
        return
      }
      const expected = `data:application/json;charset=utf-8;base64,eyJIZWxsbyI6IldvcmxkISJ9`
      if string(encoded) != expected {
        fmt.Printf("expected: %q\n", expected)
        fmt.Printf("actual:   %q\n", encoded)
        return
      }
    }
  }

  { // Explicitly specify to enable or disable base64

    { // First case: by default this would NOT be base64 encoded,
      // but you can force it to do so
      encoded, err := dataurl.Encode(
        []byte(`Hello, World!`),
        dataurl.WithBase64Encoding(true),
      )
      if err != nil {
        fmt.Printf("failed to encode: %s", err)
        return
      }
      const expected = `data:text/plain;charset=utf-8;base64,SGVsbG8sIFdvcmxkIQ==`
      if string(encoded) != expected {
        fmt.Printf("expected: %q\n", expected)
        fmt.Printf("actual:   %q\n", encoded)
        return
      }
    }

    { // Second case: by defualt his would be base64 encoded,
      // but you can force it to emit plain text
      // Note that this would produce really bad results if your data is
      // actually binary
      encoded, err := dataurl.Encode(
        []byte(`{"Hello":"World!"}`),
        dataurl.WithMediaType(`application/json`),
        dataurl.WithBase64Encoding(false),
      )

      if err != nil {
        fmt.Printf("failed to encode: %s", err)
        return
      }
      const expected = `data:application/json,%7B%22Hello%22%3A%22World!%22%7D`
      if string(encoded) != expected {
        fmt.Printf("expected: %q\n", expected)
        fmt.Printf("actual:   %q\n", encoded)
        return
      }
    }
  }

  // OUTPUT:
  //
}

const gopher = `iVBORw0KGgoAAAANSUhEUgAAAUAAAABzCAYAAADpCdsdAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAsf0lEQVR42u2deXwUVbbHf1XVazqdnUBCEkISAgYBkwcEjEAAlTUijGhwe/pgnAFExzfjKOo8Rcb5uICijgoCOsjqgoqASsAgOyLEBJJIIPu+ka33ru6674+6nXQg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MTQtMDktMTZUMDM6NTc6MDQrMDA6MDCH/7TvAAAARnRFWHRzb2Z0d2FyZQBJbWFnZU1hZ2ljayA2LjYuOS03IDIwMTQtMDMtMDYgUTE2IGh0dHA6Ly93d3cuaW1hZ2VtYWdpY2sub3JngdOzwwAAABh0RVh0VGh1bWI6OkRvY3VtZW50OjpQYWdlcwAxp/+7LwAAABh0RVh0VGh1bWI6OkltYWdlOjpoZWlnaHQANDQwUmuvDwAAABh0RVh0VGh1bWI6OkltYWdlOjpXaWR0aAAxMjI08evQygAAABl0RVh0VGh1bWI6Ok1pbWV0eXBlAGltYWdlL3BuZz+yVk4AAAAXdEVYdFRodW1iOjpNVGltZQAxNDEwODM5ODI0DeAXbwAAABN0RVh0VGh1bWI6OlNpemUAODAuMUtCQr9J6A8AAAAzdEVYdFRodW1iOjpVUkkAZmlsZTovLy90bXAvbG9jYWxjb3B5XzQ2YjJiMTRkMDQwOS0xLnBuZ8kGguYAAAAASUVORK5CYII=`

source: examples/encode_example_test.go

Parsing

package examples

import (
  "fmt"

  "github.com/lestrrat-go/dataurl"
)

func ExampleParse() {
  u, err := dataurl.Parse([]byte(`data:application/json;charset=utf-8;base64,eyJIZWxsbyI6IldvcmxkISJ9`))
  if err != nil {
    fmt.Printf("failed to parse: %s", err)
    return
  }

  fmt.Printf("media type: %q\n", u.MediaType.Type)
  fmt.Printf("params:\n")
  for k, v := range u.MediaType.Params {
    fmt.Printf("  %s: %s\n", k, v)
  }
  fmt.Printf("data: %s\n", u.Data)

  // OUTPUT:
  // media type: "application/json"
  // params:
  //   charset: utf-8
  // data: {"Hello":"World!"}
}

source: examples/parse_example_test.go

GitHub

View Github