Skip to content

Commit

Permalink
impr: comply with rfc 2397 for binary files in request
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Feb 20, 2024
1 parent cc3567f commit 460493e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions internal/fileio/fileio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/fileio/fileio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 460493e

Please sign in to comment.