Skip to content

Commit

Permalink
Merge pull request #234 from trheyi/main
Browse files Browse the repository at this point in the history
Implement Download method in FS object
  • Loading branch information
trheyi authored Nov 5, 2024
2 parents 3efe662 + dd82d00 commit 572f62d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
42 changes: 42 additions & 0 deletions runtime/v8/objects/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions runtime/v8/objects/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 572f62d

Please sign in to comment.