Skip to content

Commit

Permalink
convert to brotli compression match rust mods
Browse files Browse the repository at this point in the history
  • Loading branch information
stackdump committed Mar 6, 2024
1 parent 228d429 commit 9f11a3f
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 299 deletions.
50 changes: 50 additions & 0 deletions compression/compression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package compression

import (
"bytes"
"encoding/base64"
"github.com/andybalholm/brotli"
"io/ioutil"
"net/url"
"strings"
)

func DecompressBrotliDecode(base64String string) (sourceJson string, ok bool) {
data, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
return "", false
}
reader := bytes.NewReader(data)
br := brotli.NewReader(reader)
decompressedData, err := ioutil.ReadAll(br)
if err != nil {
return "", false
}
return string(decompressedData), true
}

func DecompressEncodedUrl(urlString string) (sourceJson string, ok bool) {
parsedUrl, err := url.Parse(urlString)
if err != nil {
return "", false
}
base64String := parsedUrl.Query().Get("z")
if base64String == "" {
return "", false
}
return DecompressBrotliDecode(strings.ReplaceAll(base64String, " ", "+"))
}

func CompressBrotliEncode(fileData []byte) (base64String string, ok bool) {
var buffer bytes.Buffer
bw := brotli.NewWriter(&buffer)
_, err := bw.Write(fileData)
if err != nil {
return "", false
}
err = bw.Close()
if err != nil {
return "", false
}
return base64.StdEncoding.EncodeToString(buffer.Bytes()), true
}
32 changes: 32 additions & 0 deletions compression/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package compression

import (
"testing"
)

func TestCompressBrotliEncodeAndDecode(t *testing.T) {
fileData := "Test Compress Brotli Encode"
base64String, ok := CompressBrotliEncode([]byte(fileData))
if !ok {
t.Fatalf("failed to compress")
}
t.Logf("base64String: %s", base64String)
sourceJson, ok := DecompressBrotliDecode(base64String)
if !ok {
t.Fatalf("failed to decompress")
}
if sourceJson != fileData {
t.Fatalf("mismatch %v", sourceJson)
}
}

func TestDecompressEncodedUrl(t *testing.T) {
rawData := "GxUBIBwHdqMPWUayBmvB036CyG2p/i4T/vkhUIqhOINzBItzjIAjQRT6UodFYb3lf/s+tUpixqYotpvtm6rosihFrSj0hinJ5yEzylwRkOHP3/C10Z+9DY4wrECyVuOuK6yIowpW+wANmGBbDfiT25M8ymZktaqaaJI4OGKn72O+5sEtRzooqYttsJIJXHe9+n0VHHo2"
urlString := "https://pflow-dev.github.io/pflow-js/p/?z=" + rawData
sourceJson, ok := DecompressEncodedUrl(urlString)
if !ok {
t.Fatalf("failed to decompress")
}
t.Logf("%s", urlString)
t.Logf("sourceJson: %s", sourceJson)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/pflow-dev/go-metamodel/v2
go 1.19

require (
github.com/andybalholm/brotli v1.1.0
github.com/gibson042/canonicaljson-go v1.0.3
github.com/ipfs/go-cid v0.4.1
github.com/multiformats/go-multibase v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/gibson042/canonicaljson-go v1.0.3 h1:EAyF8L74AWabkyUmrvEFHEt/AGFQeD6RfwbAuf0j1bI=
github.com/gibson042/canonicaljson-go v1.0.3/go.mod h1:DsLpJTThXyGNO+KZlI85C1/KDcImpP67k/RKVjcaEqo=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
3 changes: 2 additions & 1 deletion image/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"github.com/pflow-dev/go-metamodel/v2/metamodel"
"github.com/pflow-dev/go-metamodel/v2/vasm"
"io"
"math"
"os"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (i *SvgImage) MarkerEnd() {
}
func (i *SvgImage) Render(m metamodel.MetaModel, initialVectors ...metamodel.Vector) {
net := m.Net()
i.stateMachine = m.Execute(initialVectors...)
i.stateMachine = vasm.Execute(m.Net(), initialVectors...)
for _, a := range net.Arcs {
i.arc(a)
}
Expand Down
7 changes: 4 additions & 3 deletions image/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package image_test
import (
"github.com/pflow-dev/go-metamodel/v2/image"
"github.com/pflow-dev/go-metamodel/v2/model"
"github.com/pflow-dev/go-metamodel/v2/zblob"
"testing"
)

const sampleUrl = "UEsDBAoAAAAAAGMXNlhs0OtB1wwAANcMAAAKAAAAbW9kZWwuanNvbnsKICAibW9kZWxUeXBlIjogInBldHJpTmV0IiwKICAidmVyc2lvbiI6ICJ2MCIsCiAgInBsYWNlcyI6IHsKICAgICJyaWdodDIiOiB7ICJvZmZzZXQiOiAwLCAieCI6IDgxMCwgInkiOiAxNDkgfSwKICAgICJsZWZ0MiI6IHsgIm9mZnNldCI6IDEsICJ4IjogOTQyLCAieSI6IDE1MyB9LAogICAgInJpZ2h0MyI6IHsgIm9mZnNldCI6IDIsICJ4IjogMTE4MiwgInkiOiAyMTggfSwKICAgICJsZWZ0MyI6IHsgIm9mZnNldCI6IDMsICJ4IjogMTI2MCwgInkiOiAzMzkgfSwKICAgICJyaWdodDQiOiB7ICJvZmZzZXQiOiA0LCAieCI6IDExNjksICJ5IjogNzQ0IH0sCiAgICAibGVmdDQiOiB7ICJvZmZzZXQiOiA1LCAieCI6IDEwODIsICJ5IjogODQzIH0sCiAgICAicmlnaHQ1IjogeyAib2Zmc2V0IjogNiwgIngiOiA2MzAsICJ5IjogODU2IH0sCiAgICAibGVmdDUiOiB7ICJvZmZzZXQiOiA3LCAieCI6IDUzMSwgInkiOiA3MjggfSwKICAgICJyaWdodDEiOiB7ICJvZmZzZXQiOiA4LCAieCI6IDQ0MSwgInkiOiAzNTkgfSwKICAgICJsZWZ0MSI6IHsgIm9mZnNldCI6IDksICJ4IjogNTAxLCAieSI6IDI0NCB9LAogICAgImNob3BzdGljazEiOiB7ICJvZmZzZXQiOiAxMCwgImluaXRpYWwiOiAxLCAieCI6IDgxMSwgInkiOiA0MjYgfSwKICAgICJjaG9wc3RpY2syIjogeyAib2Zmc2V0IjogMTEsICJpbml0aWFsIjogMSwgIngiOiA5MzEsICJ5IjogNDM0IH0sCiAgICAiY2hvcHN0aWNrMyI6IHsgIm9mZnNldCI6IDEyLCAiaW5pdGlhbCI6IDEsICJ4IjogOTY5LCAieSI6IDU0NSB9LAogICAgImNob3BzdGljazQiOiB7ICJvZmZzZXQiOiAxMywgImluaXRpYWwiOiAxLCAieCI6IDg2MywgInkiOiA2MTQgfSwKICAgICJjaG9wc3RpY2s1IjogeyAib2Zmc2V0IjogMTQsICJpbml0aWFsIjogMSwgIngiOiA3NzQsICJ5IjogNTM2IH0KICB9LAogICJ0cmFuc2l0aW9ucyI6IHsKICAgICJlYXQxIjogeyAieCI6IDYxMCwgInkiOiAzNzAgfSwKICAgICJ0aGluazEiOiB7ICJ4IjogMzcyLCAieSI6IDI0NyB9LAogICAgImVhdDIiOiB7ICJ4IjogODc0LCAieSI6IDI4MSB9LAogICAgInRoaW5rMiI6IHsgIngiOiA4NzYsICJ5IjogNDIgfSwKICAgICJlYXQzIjogeyAieCI6IDExMTUsICJ5IjogMzQ4IH0sCiAgICAidGhpbmszIjogeyAieCI6IDEzMDksICJ5IjogMjE1IH0sCiAgICAiZWF0NCI6IHsgIngiOiAxMDM0LCAieSI6IDY5MSB9LAogICAgInRoaW5rNCI6IHsgIngiOiAxMjI3LCAieSI6IDg5NiB9LAogICAgImVhdDUiOiB7ICJ4IjogNjczLCAieSI6IDY4OCB9LAogICAgInRoaW5rNSI6IHsgIngiOiA0ODMsICJ5IjogODg3IH0KICB9LAogICJhcmNzIjogWwogICAgeyAic291cmNlIjogImNob3BzdGljazEiLCAidGFyZ2V0IjogImVhdDEiIH0sCiAgICB7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ICJ0aGluazMiIH0sCiAgICB7ICJzb3VyY2UiOiAidGhpbmszIiwgInRhcmdldCI6ICJjaG9wc3RpY2syIiB9LAogICAgeyAic291cmNlIjogInRoaW5rMyIsICJ0YXJnZXQiOiAiY2hvcHN0aWNrMyIgfSwKICAgIHsgInNvdXJjZSI6ICJyaWdodDIiLCAidGFyZ2V0IjogInRoaW5rMiIgfSwKICAgIHsgInNvdXJjZSI6ICJsZWZ0MiIsICJ0YXJnZXQiOiAidGhpbmsyIiB9CiAgXQp9UEsBAhQACgAAAAAAYxc2WGzQ60HXDAAA1wwAAAoAAAAAAAAAAAAAAAAAAAAAAG1vZGVsLmpzb25QSwUGAAAAAAEAAQA4AAAA/wwAAAAA"
const sampleUrl = "GwkCAJwHto1sm0wlY/ApWO0mo82luvuMaSkLZQmYJtMGEenqmzosCqUAMnGuGyZH8UY2wGjCiAYdjeO/WUq3CVp01RVT12z9Uaj70Sex/h7bVHcHRtlqsExce2Q/wx/Uz/PughgHBfEfZE9Qbh+onTnQu1OuejznDb3bxGRqWy+aUcbhSth0mKWcNlSLjY6CjzXeYUZL9jOXttgdzSVthR/UOHhP0g642uaJ3IEiQYF7tEw/zyr2M0tz2oJwr+FAqjXmxylPh9Pyt6t6kjxzeyzr"

func TestNewSvg(t *testing.T) {
// Load a model from a URL
zm := new(model.Zblob)
zm := new(zblob.Zblob)
zm.Base64Zipped = sampleUrl
m := zm.ToModel()
m := model.FromZblob(zm)
_, mm := m.MetaModel()
x1, y1, width, height := mm.GetViewPort()
i := image.NewSvgFile("/tmp/test.svg", width, height, x1, y1, width, height)
Expand Down
Loading

0 comments on commit 9f11a3f

Please sign in to comment.