Skip to content

Commit 460493e

Browse files
committed
impr: comply with rfc 2397 for binary files in request
1 parent cc3567f commit 460493e

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

internal/fileio/fileio.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ func ReadJson[T any](path string) (T, error) {
7272

7373
// WriteFile writes the file to disk.
7474
// The content can be text or binary (encoded as a data URL),
75-
// e.g. data:application/octet-stream;base64,MTIz
75+
// e.g. data:;base64,MTIz
7676
func WriteFile(path, content string, perm fs.FileMode) (err error) {
7777
var data []byte
78-
if strings.HasPrefix(content, "data:") {
78+
// TODO: only check for "data:;base64," to comply with RFC 2397.
79+
// Remove the "data:" check after the snippet reaches 0.16.
80+
if strings.HasPrefix(content, "data:") || strings.HasPrefix(content, "data:;base64,") {
7981
// data-url encoded file
8082
_, encoded, found := strings.Cut(content, ",")
8183
if !found {

internal/fileio/fileio_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestWriteFile(t *testing.T) {
175175

176176
t.Run("binary", func(t *testing.T) {
177177
path := filepath.Join(dir, "data.bin")
178-
err = WriteFile(path, "data:application/octet-stream;base64,MTIz", 0444)
178+
err = WriteFile(path, "data:;base64,MTIz", 0444)
179179
if err != nil {
180180
t.Fatalf("expected nil err, got %v", err)
181181
}
@@ -215,7 +215,7 @@ func TestWriteFile(t *testing.T) {
215215

216216
t.Run("invalid binary value", func(t *testing.T) {
217217
path := filepath.Join(dir, "data.bin")
218-
err = WriteFile(path, "data:application/octet-stream;base64,12345", 0444)
218+
err = WriteFile(path, "data:;base64,12345", 0444)
219219
if err == nil {
220220
t.Fatal("expected error, got nil")
221221
}

0 commit comments

Comments
 (0)