add read bytes and contentType

This commit is contained in:
2023-06-22 17:22:08 +01:00
parent 1d3eaf9990
commit ef951afab5
4 changed files with 38 additions and 0 deletions

View File

@@ -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{}
}

View File

@@ -108,6 +108,7 @@ func ArSocket(args ...any) (any, ArErr) {
"close",
func(args ...any) (any, ArErr) {
conn.Close()
conn = nil
return nil, ArErr{}
},
},
@@ -124,6 +125,7 @@ func ArSocket(args ...any) (any, ArErr) {
"close",
func(args ...any) (any, ArErr) {
ln.Close()
ln = nil
return nil, ArErr{}
},
},