diff --git a/runtime/v8/objects/fs/fs.go b/runtime/v8/objects/fs/fs.go index 955af46..2eebef1 100644 --- a/runtime/v8/objects/fs/fs.go +++ b/runtime/v8/objects/fs/fs.go @@ -20,6 +20,8 @@ import ( // var dataString = fs.ReadFile("/root/path/name.file") // var dataUnit8Array = fs.ReadFileBuffer("/root/path/name.file") // var dataBase64 = fs.ReadFileBase64("/root/path/name.file") +// var handler = fs.ReadCloser("/root/path/name.file") +// var res = fs.Download('/data/path/file.txt') // { type: MimeType, content: ReadCloser } // var length = fs.WriteFile("/root/path/name.file", "Hello") // var length = fs.WriteFile("/root/path/name.file", "Hello", 0644 ) // var length = fs.WriteFileBuffer("/root/path/name.file", dataUnit8Array) @@ -94,6 +96,9 @@ func (obj *Object) ExportObject(iso *v8go.Isolate) *v8go.ObjectTemplate { tmpl.Set("Remove", obj.remove(iso)) tmpl.Set("RemoveAll", obj.removeAll(iso)) + // Download + tmpl.Set("Download", obj.download(iso)) + // Directory tmpl.Set("ReadDir", obj.readdir(iso)) tmpl.Set("Mkdir", obj.mkdir(iso)) @@ -783,6 +788,35 @@ func (obj *Object) readCloser(iso *v8go.Isolate) *v8go.FunctionTemplate { }) } +func (obj *Object) download(iso *v8go.Isolate) *v8go.FunctionTemplate { + return v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value { + args := info.Args() + if len(args) < 1 { + return obj.errorString(info, "Missing parameters") + } + + stor, err := obj.getFS(info) + if err != nil { + return obj.error(info, err) + } + + var file = args[0].String() + + mimetype, err := fs.MimeType(stor, file) + if err != nil { + return obj.error(info, err) + } + + readCloser, err := fs.ReadCloser(stor, args[0].String()) + if err != nil { + return obj.error(info, err) + } + + var res = map[string]interface{}{"type": mimetype, "content": readCloser} + return obj.downloadValue(info, res) + }) +} + func (obj *Object) writeFile(iso *v8go.Isolate) *v8go.FunctionTemplate { return v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value { args := info.Args() @@ -1176,6 +1210,14 @@ func (obj *Object) readCloserValue(info *v8go.FunctionCallbackInfo, value io.Rea return res } +func (obj *Object) downloadValue(info *v8go.FunctionCallbackInfo, value map[string]interface{}) *v8go.Value { + res, err := v8go.NewExternal(info.Context().Isolate(), value) + if err != nil { + return obj.error(info, err) + } + return res +} + func (obj *Object) arrayBufferValue(info *v8go.FunctionCallbackInfo, value []byte) *v8go.Value { res, err := bridge.JsValue(info.Context(), value) if err != nil { diff --git a/runtime/v8/objects/fs/fs_test.go b/runtime/v8/objects/fs/fs_test.go index e9ead58..cd21273 100644 --- a/runtime/v8/objects/fs/fs_test.go +++ b/runtime/v8/objects/fs/fs_test.go @@ -116,6 +116,40 @@ func TestFSObjectReadFile(t *testing.T) { err = readCloser.Close() assert.Nil(t, err) + + // Download + v, err = ctx.RunScript(fmt.Sprintf(` + function Download() { + var fs = new FS("system") + var hd = fs.Download("%s"); + return hd + } + Download() + `, f["F1"]), "") + + if err != nil { + t.Fatal(err) + } + + res, err = bridge.GoValue(v, ctx) + download, ok := res.(map[string]interface{}) + if !ok { + t.Fatal("res is not not map[string]interface{}") + } + + readCloser, ok = download["content"].(*os.File) + if !ok { + t.Fatal("res is not *os.File") + } + err = readCloser.Close() + assert.Nil(t, err) + + mimetype, ok := download["type"].(string) + if !ok { + t.Fatal("res is not string") + } + + assert.Equal(t, "text/plain; charset=utf-8", mimetype) } func TestFSObjectRootFs(t *testing.T) {