-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert to brotli compression match rust mods
- Loading branch information
Showing
14 changed files
with
297 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.