mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 08:56:07 +00:00
add read bytes and contentType
This commit is contained in:
29
src/file.go
29
src/file.go
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
)
|
||||
|
||||
var ArFile = Map(anymap{
|
||||
@@ -21,6 +23,15 @@ func readtext(file *os.File) (string, error) {
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func readbinary(file *os.File) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
_, err := io.Copy(&buf, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func ArRead(args ...any) (any, ArErr) {
|
||||
if len(args) != 1 {
|
||||
return ArObject{}, ArErr{TYPE: "Runtime Error", message: "read takes 1 argument, got " + fmt.Sprint(len(args)), EXISTS: true}
|
||||
@@ -49,6 +60,24 @@ func ArRead(args ...any) (any, ArErr) {
|
||||
}
|
||||
return jsonparse(text), ArErr{}
|
||||
}},
|
||||
"contentType": builtinFunc{"contentType", func(...any) (any, ArErr) {
|
||||
mimetype, err := mimetype.DetectFile(filename)
|
||||
if err != nil {
|
||||
return ArObject{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
|
||||
}
|
||||
return mimetype.String(), ArErr{}
|
||||
}},
|
||||
"bytes": builtinFunc{"bytes", func(...any) (any, ArErr) {
|
||||
bytes, err := readbinary(file)
|
||||
if err != nil {
|
||||
return ArObject{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true}
|
||||
}
|
||||
ArBinary := []any{}
|
||||
for _, b := range bytes {
|
||||
ArBinary = append(ArBinary, newNumber().SetInt64(int64(b)))
|
||||
}
|
||||
return ArBinary, ArErr{}
|
||||
}},
|
||||
}), ArErr{}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user