Skip to content

Commit

Permalink
add data uri scheme tool
Browse files Browse the repository at this point in the history
  • Loading branch information
switchupcb committed Jul 28, 2022
1 parent e0f0139 commit 2e3cca8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
5 changes: 4 additions & 1 deletion _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/switchupcb/disgo/_examples

go 1.18

require github.com/switchupcb/disgo v0.0.0-20220726210903-501dcb40b012
require (
github.com/switchupcb/disgo v0.0.0-20220726210903-501dcb40b012
github.com/switchupcb/disgo/tools v0.0.0-20220727073006-e0f01397d626
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQan
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/switchupcb/disgo v0.0.0-20220726210903-501dcb40b012 h1:8K0Cs/qWX0p21TH6WYeb9ihYT+nLzabfsGyfm/g/Xv4=
github.com/switchupcb/disgo v0.0.0-20220726210903-501dcb40b012/go.mod h1:Wl43EK7HOIj+qYkKGY7bfmhHcs4kOamgHeMk8ZujROQ=
github.com/switchupcb/disgo/tools v0.0.0-20220727073006-e0f01397d626 h1:M9XQfGcnMXzlAXWFzxTLnwY65Y0K4VD4LlsyPt55MSQ=
github.com/switchupcb/disgo/tools v0.0.0-20220727073006-e0f01397d626/go.mod h1:meg8UOjeK6vLhi5powODQ8VhmUrH8x2krlPEelARWjE=
github.com/switchupcb/websocket v1.8.8 h1:0x7RIs90NJ8YggqcLdKeb/LTofJ1BY79n784pkLyk5o=
github.com/switchupcb/websocket v1.8.8/go.mod h1:HdhyzCLfOFPrBv+QNcnDSbv8L8rfJ7ZCulrBKZJHip0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
5 changes: 4 additions & 1 deletion _examples/image/avatar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"

"github.com/switchupcb/disgo/tools"
disgo "github.com/switchupcb/disgo/wrapper"
)

Expand Down Expand Up @@ -86,9 +87,11 @@ func main() {
}

// format the Image Data into a Data URI.
//
// The following code is equivalent to tools.ImageDataURI(image).
contentType := http.DetectContentType(image)
encodedImage := base64.StdEncoding.EncodeToString(image)
data := fmt.Sprintf("data:%s;base64,%s", contentType, encodedImage)
data := tools.DataURI(contentType, encodedImage)

// create a new Bot Client with the information required to send a request.
bot := &disgo.Client{
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/switchupcb/disgo/tools v0.0.0-20220727073006-e0f01397d626/go.mod h1:meg8UOjeK6vLhi5powODQ8VhmUrH8x2krlPEelARWjE=
32 changes: 32 additions & 0 deletions tools/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
run:
timeout: 5m
tests: false

linters:
fast: false
enable-all: true
disable:
- deadcode
- unused
- gofumpt

- golint # Deprecated
- interfacer # Deprecated
- scopelint # Deprecated
- maligned # Deprecated
- exhaustivestruct # Deprecated

linters-settings:
revive:
rules:
- name: var-naming
disabled: true # Flags defined by Discord.
arguments:
- [] # AllowList
- [] # DenyList

govet:
enable-all: true
disable:
- shadow
- fieldalignment # Completed at bundle-time.
15 changes: 15 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package tools

import (
"encoding/base64"
"net/http"

disgo "github.com/switchupcb/disgo/wrapper"
)

// DataURI returns a Data URI from the given HTTP Content Type Header and base64 encoded data.
//
// https://en.wikipedia.org/wiki/Data_URI_scheme
func DataURI(contentType, base64EncodedData string) string {
return "data:" + contentType + ";base64," + base64EncodedData
}

// ImageDataURI returns a Data URI from the given image data.
func ImageDataURI(img []byte) string {
return DataURI(http.DetectContentType(img), base64.StdEncoding.EncodeToString(img))
}

// OptionsToMap parses an array of options and suboptions into an OptionMap.
func OptionsToMap(
optionMap map[string]*disgo.ApplicationCommandInteractionDataOption,
Expand Down

0 comments on commit 2e3cca8

Please sign in to comment.