impr: backward-compatible compliance with rfc 2397
This commit is contained in:
@@ -72,25 +72,32 @@ 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,") {
|
||||
if !strings.HasPrefix(content, "data:") {
|
||||
// text file
|
||||
data = []byte(content)
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
// data-url encoded file
|
||||
_, encoded, found := strings.Cut(content, ",")
|
||||
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
|
||||
}
|
||||
} else {
|
||||
// text file
|
||||
data = []byte(content)
|
||||
}
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user