From 2e3cca828d5e2a8ffc211b11e6a4951a3db9f3a3 Mon Sep 17 00:00:00 2001 From: switchupcb Date: Thu, 28 Jul 2022 10:40:27 -0500 Subject: [PATCH] add data uri scheme tool --- _examples/go.mod | 5 ++++- _examples/go.sum | 2 ++ _examples/image/avatar/main.go | 5 ++++- go.work.sum | 1 + tools/.golangci.yml | 32 ++++++++++++++++++++++++++++++++ tools/tools.go | 15 +++++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 go.work.sum create mode 100644 tools/.golangci.yml diff --git a/_examples/go.mod b/_examples/go.mod index 3a9e1a2..4f95ba7 100644 --- a/_examples/go.mod +++ b/_examples/go.mod @@ -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 diff --git a/_examples/go.sum b/_examples/go.sum index 653eb4c..8882775 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -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= diff --git a/_examples/image/avatar/main.go b/_examples/image/avatar/main.go index aa567ba..bd791cd 100644 --- a/_examples/image/avatar/main.go +++ b/_examples/image/avatar/main.go @@ -8,6 +8,7 @@ import ( "net/http" "os" + "github.com/switchupcb/disgo/tools" disgo "github.com/switchupcb/disgo/wrapper" ) @@ -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{ diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..5ff7463 --- /dev/null +++ b/go.work.sum @@ -0,0 +1 @@ +github.com/switchupcb/disgo/tools v0.0.0-20220727073006-e0f01397d626/go.mod h1:meg8UOjeK6vLhi5powODQ8VhmUrH8x2krlPEelARWjE= diff --git a/tools/.golangci.yml b/tools/.golangci.yml new file mode 100644 index 0000000..9f49e85 --- /dev/null +++ b/tools/.golangci.yml @@ -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. \ No newline at end of file diff --git a/tools/tools.go b/tools/tools.go index d3c40d5..4b7b8d0 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -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,