From d6945f00480eb1a0af73f667044bf3860d4666b6 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 20 Feb 2024 12:10:21 +0500 Subject: [PATCH] impr: backward-compatible compliance with rfc 2397 --- internal/fileio/fileio.go | 35 ++++++++++++++++++++------------- internal/fileio/fileio_test.go | 36 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/internal/fileio/fileio.go b/internal/fileio/fileio.go index 3232218..e772383 100644 --- a/internal/fileio/fileio.go +++ b/internal/fileio/fileio.go @@ -72,24 +72,31 @@ 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:;base64,MTIz +// e.g. data:application/octet-stream;base64,MTIz func WriteFile(path, content string, perm fs.FileMode) (err error) { var data []byte - // 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 { - return errors.New("invalid data-url encoding") - } - data, err = base64.StdEncoding.DecodeString(encoded) - if err != nil { - return err - } - } else { + if !strings.HasPrefix(content, "data:") { // text file data = []byte(content) + return os.WriteFile(path, data, perm) + } + + // data-url encoded file + meta, encoded, found := strings.Cut(content, ",") + if !found { + return errors.New("invalid data-url encoding") + } + + if !strings.HasSuffix(meta, "base64") { + // no need to decode + data = []byte(encoded) + return os.WriteFile(path, data, perm) + } + + // decode base64-encoded data + data, err = base64.StdEncoding.DecodeString(encoded) + if err != nil { + return err } return os.WriteFile(path, data, perm) } diff --git a/internal/fileio/fileio_test.go b/internal/fileio/fileio_test.go index e4ad1a4..fd82024 100644 --- a/internal/fileio/fileio_test.go +++ b/internal/fileio/fileio_test.go @@ -173,8 +173,24 @@ func TestWriteFile(t *testing.T) { } }) - t.Run("binary", func(t *testing.T) { - path := filepath.Join(dir, "data.bin") + t.Run("data-octet-stream", func(t *testing.T) { + path := filepath.Join(dir, "data-1.bin") + err = WriteFile(path, "data:application/octet-stream;base64,MTIz", 0444) + if err != nil { + t.Fatalf("expected nil err, got %v", err) + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read file: expected nil err, got %v", err) + } + want := []byte("123") + if !reflect.DeepEqual(got, want) { + t.Errorf("read file: expected %v, got %v", want, got) + } + }) + + t.Run("data-base64", func(t *testing.T) { + path := filepath.Join(dir, "data-2.bin") err = WriteFile(path, "data:;base64,MTIz", 0444) if err != nil { t.Fatalf("expected nil err, got %v", err) @@ -189,6 +205,22 @@ func TestWriteFile(t *testing.T) { } }) + t.Run("data-text-plain", func(t *testing.T) { + path := filepath.Join(dir, "data-3.bin") + err = WriteFile(path, "data:text/plain;,123", 0444) + if err != nil { + t.Fatalf("expected nil err, got %v", err) + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read file: expected nil err, got %v", err) + } + want := []byte("123") + if !reflect.DeepEqual(got, want) { + t.Errorf("read file: expected %v, got %v", want, got) + } + }) + t.Run("perm", func(t *testing.T) { const perm = 0444 path := filepath.Join(dir, "perm.txt")