mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-04 15:49:49 +00:00
33 lines
715 B
Go
33 lines
715 B
Go
package file
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/dop251/goja"
|
|
"io"
|
|
)
|
|
|
|
type File struct {
|
|
io.Reader `json:""`
|
|
io.Closer `json:""`
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func NewJsFile(runtime *goja.Runtime) (goja.Value, error) {
|
|
fileCtor, ok := goja.AssertConstructor(runtime.Get("File"))
|
|
if !ok {
|
|
return nil, errors.New("file is not defined")
|
|
}
|
|
return fileCtor(nil)
|
|
}
|
|
|
|
func Enable(runtime *goja.Runtime) error {
|
|
file := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
|
instance := &File{}
|
|
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
|
instanceValue.SetPrototype(call.This.Prototype())
|
|
return instanceValue
|
|
})
|
|
return runtime.Set("File", file)
|
|
}
|