Skip to content

Commit

Permalink
impr: backward-compatible compliance with rfc 2397
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Feb 20, 2024
1 parent 460493e commit d6945f0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
35 changes: 21 additions & 14 deletions internal/fileio/fileio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
36 changes: 34 additions & 2 deletions internal/fileio/fileio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down

0 comments on commit d6945f0

Please sign in to comment.