impr: backward-compatible compliance with rfc 2397
This commit is contained in:
@@ -72,24 +72,31 @@ func ReadJson[T any](path string) (T, error) {
|
|||||||
|
|
||||||
// WriteFile writes the file to disk.
|
// WriteFile writes the file to disk.
|
||||||
// The content can be text or binary (encoded as a data URL),
|
// 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) {
|
func WriteFile(path, content string, perm fs.FileMode) (err error) {
|
||||||
var data []byte
|
var data []byte
|
||||||
// TODO: only check for "data:;base64," to comply with RFC 2397.
|
if !strings.HasPrefix(content, "data:") {
|
||||||
// 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 {
|
|
||||||
// text file
|
// text file
|
||||||
data = []byte(content)
|
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)
|
return os.WriteFile(path, data, perm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,8 +173,24 @@ func TestWriteFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("binary", func(t *testing.T) {
|
t.Run("data-octet-stream", func(t *testing.T) {
|
||||||
path := filepath.Join(dir, "data.bin")
|
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)
|
err = WriteFile(path, "data:;base64,MTIz", 0444)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("expected nil err, got %v", err)
|
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) {
|
t.Run("perm", func(t *testing.T) {
|
||||||
const perm = 0444
|
const perm = 0444
|
||||||
path := filepath.Join(dir, "perm.txt")
|
path := filepath.Join(dir, "perm.txt")
|
||||||
|
|||||||
Reference in New Issue
Block a user