From 460493eeaa4dc440e4e48d894bf18a638463b80a Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 20 Feb 2024 11:46:06 +0500 Subject: [PATCH] impr: comply with rfc 2397 for binary files in request --- internal/fileio/fileio.go | 6 ++++-- internal/fileio/fileio_test.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/fileio/fileio.go b/internal/fileio/fileio.go index 909fc51..3232218 100644 --- a/internal/fileio/fileio.go +++ b/internal/fileio/fileio.go @@ -72,10 +72,12 @@ func ReadJson[T any](path string) (T, error) { // WriteFile writes the file to disk. // The content can be text or binary (encoded as a data URL), -// e.g. data:application/octet-stream;base64,MTIz +// e.g. data:;base64,MTIz func WriteFile(path, content string, perm fs.FileMode) (err error) { var data []byte - if strings.HasPrefix(content, "data:") { + // TODO: only check for "data:;base64," to comply with RFC 2397. + // Remove the "data:" check after the snippet reaches 0.16. + if strings.HasPrefix(content, "data:") || strings.HasPrefix(content, "data:;base64,") { // data-url encoded file _, encoded, found := strings.Cut(content, ",") if !found { diff --git a/internal/fileio/fileio_test.go b/internal/fileio/fileio_test.go index 08cd68e..e4ad1a4 100644 --- a/internal/fileio/fileio_test.go +++ b/internal/fileio/fileio_test.go @@ -175,7 +175,7 @@ func TestWriteFile(t *testing.T) { t.Run("binary", func(t *testing.T) { path := filepath.Join(dir, "data.bin") - err = WriteFile(path, "data:application/octet-stream;base64,MTIz", 0444) + err = WriteFile(path, "data:;base64,MTIz", 0444) if err != nil { t.Fatalf("expected nil err, got %v", err) } @@ -215,7 +215,7 @@ func TestWriteFile(t *testing.T) { t.Run("invalid binary value", func(t *testing.T) { path := filepath.Join(dir, "data.bin") - err = WriteFile(path, "data:application/octet-stream;base64,12345", 0444) + err = WriteFile(path, "data:;base64,12345", 0444) if err == nil { t.Fatal("expected error, got nil") }