Skip to content

Commit

Permalink
chore: Housekeeping
Browse files Browse the repository at this point in the history
GitHub Actions Workflow CI.
Cleaned up code by fixing lint errors.
  • Loading branch information
ananthb committed Apr 12, 2024
1 parent d28ab02 commit ca82220
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 219 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI 🏗

on:
push:
branches:
- main

pull_request:
branches:
- main

workflow_dispatch:

jobs:
test:
name: Lint & test code.
strategy:
matrix:
os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
go: ['stable', 'oldstable']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- uses: golangci/golangci-lint-action@v3
with:
version: "v1.55.2"

- run: go vet ./...

- run: go test -race -cover ./...
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ The MIT License (MIT)

Copyright (c) 2014 Vincent Petithory

Copyright (c) 2024 Ananth Bhaskararaman

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
Expand Down
83 changes: 14 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,26 @@
# Data URL Schemes for Go [![wercker status](https://app.wercker.com/status/6f9a2e144dfcc59e862c52459b452928/s "wercker status")](https://app.wercker.com/project/bykey/6f9a2e144dfcc59e862c52459b452928) [![GoDoc](https://godoc.org/github.com/vincent-petithory/dataurl?status.png)](https://godoc.org/github.com/vincent-petithory/dataurl)
# Dataurl

This package parses and generates Data URL Schemes for the Go language, according to [RFC 2397](http://tools.ietf.org/html/rfc2397).
[![Go Reference](https://pkg.go.dev/badge/github.com/ananthb/dataurl.svg)](https://pkg.go.dev/github.com/ananthb/dataurl)

Data URLs are small chunks of data commonly used in browsers to display inline data,
typically like small images, or when you use the FileReader API of the browser.

Common use-cases:

* generate a data URL out of a `string`, `[]byte`, `io.Reader` for inclusion in HTML templates,
* parse a data URL sent by a browser in a http.Handler, and do something with the data (save to disk, etc.)
* ...

Install the package with:
~~~
go get github.com/vincent-petithory/dataurl
~~~

## Usage
Data URL Schemes for Go

~~~ go
package main
[![CI 🏗](https://github.com/ananthb/dataurl/actions/workflows/ci.yml/badge.svg)](https://github.com/ananthb/dataurl/actions/workflows/ci.yml)

import (
"github.com/vincent-petithory/dataurl"
"fmt"
)
This package parses and generates Data URL Schemes for the Go language,
according to [RFC 2397](http://tools.ietf.org/html/rfc2397).

func main() {
dataURL, err := dataurl.DecodeString(`data:text/plain;charset=utf-8;base64,aGV5YQ==`)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("content type: %s, data: %s\n", dataURL.MediaType.ContentType(), string(dataURL.Data))
// Output: content type: text/plain, data: heya
}
~~~

From a `http.Handler`:

~~~ go
func handleDataURLUpload(w http.ResponseWriter, r *http.Request) {
dataURL, err := dataurl.Decode(r.Body)
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if dataURL.ContentType() == "image/png" {
ioutil.WriteFile("image.png", dataURL.Data, 0644)
} else {
http.Error(w, "not a png", http.StatusBadRequest)
}
}
~~~
Data URLs are small chunks of data commonly used in browsers to display inline data,
typically like small images, or when you use the FileReader API of the browser.

## Command

For convenience, a `dataurl` command is provided to encode/decode dataurl streams.

~~~
dataurl - Encode or decode dataurl data and print to standard output
Usage: dataurl [OPTION]... [FILE]
Use the [`dataurl`](./cmd/dataurl) command to encode/decode dataurl streams.

dataurl encodes or decodes FILE or standard input if FILE is - or omitted, and prints to standard output.
Unless -mimetype is used, when FILE is specified, dataurl will attempt to detect its mimetype using Go's mime.TypeByExtension (http://golang.org/pkg/mime/#TypeByExtension). If this fails or data is read from STDIN, the mimetype will default to application/octet-stream.
Install it with `go install github.com/ananthb/dataurl/cmd/dataurl@latest`.

Options:
-a=false: encode data using ascii instead of base64
-ascii=false: encode data using ascii instead of base64
-d=false: decode data instead of encoding
-decode=false: decode data instead of encoding
-m="": force the mimetype of the data to encode to this value
-mimetype="": force the mimetype of the data to encode to this value
~~~
## [LICENSE](LICENSE)

## Contributing
Forked from [vincent-petithory/dataurl](https://github.com/vincent-petithory/dataurl)
with contributions from [MagicalTux/dataurl)[https://github.com/MagicalTux/dataurl/tree/fix-issue-5).

Feel free to file an issue/make a pull request if you find any bug, or want to suggest enhancements.
Dataurl is available under the terms of the MIT license.
13 changes: 3 additions & 10 deletions cmd/dataurl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"os"
"path"

"github.com/vincent-petithory/dataurl"
"github.com/ananthb/dataurl"
)

var (
Expand Down Expand Up @@ -63,7 +62,7 @@ func main() {
case 1:
if flag.Arg(0) == "-" {
in = os.Stdin
return
break
}
if f, err := os.Open(flag.Arg(0)); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -109,9 +108,6 @@ func decode(in io.Reader, out io.Writer) (err error) {
}

_, err = out.Write(du.Data)
if err != nil {
return
}
return
}

Expand All @@ -126,7 +122,7 @@ func encode(in io.Reader, out io.Writer, encoding string, mediatype string) (err
return
}
}()
b, err := ioutil.ReadAll(in)
b, err := io.ReadAll(in)
if err != nil {
return
}
Expand All @@ -135,8 +131,5 @@ func encode(in io.Reader, out io.Writer, encoding string, mediatype string) (err
du.Encoding = encoding

_, err = du.WriteTo(out)
if err != nil {
return
}
return
}
12 changes: 5 additions & 7 deletions dataurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -107,11 +106,11 @@ func New(data []byte, mediatype string, paramPairs ...string) *DataURL {
// Note: it doesn't guarantee the returned string is equal to
// the initial source string that was used to create this DataURL.
// The reasons for that are:
// * Insertion of default values for MediaType that were maybe not in the initial string,
// * Various ways to encode the MediaType parameters (quoted string or url encoded string, the latter is used),
// - Insertion of default values for MediaType that were maybe not in the initial string,
// - Various ways to encode the MediaType parameters (quoted string or url encoded string, the latter is used),
func (du *DataURL) String() string {
var buf bytes.Buffer
du.WriteTo(&buf)
_, _ = du.WriteTo(&buf)
return (&buf).String()
}

Expand All @@ -135,8 +134,7 @@ func (du *DataURL) WriteTo(w io.Writer) (n int64, err error) {

if du.Encoding == EncodingBase64 {
encoder := base64.NewEncoder(base64.StdEncoding, w)
ni, err = encoder.Write(du.Data)
if err != nil {
if _, err = encoder.Write(du.Data); err != nil {
return
}
encoder.Close()
Expand Down Expand Up @@ -271,7 +269,7 @@ func DecodeString(s string) (*DataURL, error) {

// Decode decodes a Data URL scheme from a io.Reader.
func Decode(r io.Reader) (*DataURL, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ca82220

Please sign in to comment.