impr: support binary files
This commit is contained in:
@@ -2,10 +2,14 @@
|
||||
package fileio
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CopyFile copies all files matching the pattern
|
||||
@@ -52,3 +56,25 @@ func ReadJson[T any](path string) (T, error) {
|
||||
}
|
||||
return obj, err
|
||||
}
|
||||
|
||||
// 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
|
||||
func WriteFile(path, content string, perm fs.FileMode) (err error) {
|
||||
var data []byte
|
||||
if strings.HasPrefix(content, "data:") {
|
||||
// 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
|
||||
data = []byte(content)
|
||||
}
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user