Commit e4582977 by shenyizhong

* download files

1 parent c664042b
......@@ -52,7 +52,7 @@ func Update(postdata string) ([]byte, error) {
return nil, nil
}
func Mkdir(path string) {
func MkdirRemot(path string) {
var tmpStrs = strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) < 2 {
......@@ -94,7 +94,8 @@ func Upload(node *nodetree.NodeLoc) {
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/upload?name=data"
url += "/front/data/upload?name="
url += nodetree.StrDst
url += tmpStrs[1]
log.Println("httpcli.Upload() called, GET request:", url)
......@@ -151,3 +152,72 @@ func Upload(node *nodetree.NodeLoc) {
log.Println(string(opBytes))
}
}
func MkdirLoc(path string) {
tmpStrs := strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) > 1 {
strDir := nodetree.StrDst + "/"
strDir += tmpStrs[1]
err := os.Mkdir(strDir, 0775)
if err != nil {
log.Println(err)
}
}
}
func Download(node *nodetree.NodeRemot) {
tmpStrs := strings.Split(node.Name, nodetree.StrSrc)
if len(tmpStrs) < 2 {
return
}
// "/front/data/download?name=data/folder3/ddd.txt"
url := "http://"
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/download?name="
url += nodetree.StrSrc
url += tmpStrs[1]
log.Println("httpcli.Download() called, GET request:", url)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("token", auth.GetToken())
var resp *http.Response
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
body, _ := ioutil.ReadAll(resp.Body)
var jsonRes nodetree.DownloadFile
json.Unmarshal(body, &jsonRes)
strDstFile := nodetree.StrDst + "/"
strDstFile += tmpStrs[1]
strCmd01 := strDstFile
strCmd02 := "\"" + jsonRes.Data.Url + "\""
cmd := exec.Command("wget", "-O", strCmd01, strCmd02)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
defer stdout.Close()
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
if opBytes, err := ioutil.ReadAll(stdout); err != nil {
log.Fatal(err)
} else {
fmt.Printf("File download successful : [%v] -> [%v]\n", node.Name, strDstFile)
log.Println(string(opBytes))
}
}
......@@ -59,6 +59,7 @@ func main() {
}
if nodetree.StrOp == "upload" {
nodetree.StrDst = "data" + nodetree.StrDst
var option_loc nodetree.OptionLoc
option_loc.RootPath = []string{nodetree.StrSrc}
......@@ -77,9 +78,10 @@ func main() {
//out_loc.WriteTo(os.Stdout)
} else if nodetree.StrOp == "download" {
nodetree.StrSrc = "data" + nodetree.StrSrc
var option_remot nodetree.OptionRemot
option_remot.RootPath = []string{"data/"}
option_remot.RootPath = []string{nodetree.StrSrc}
resp_remot, _ := travremot.Explorer(option_remot)
b_remot, err := json.Marshal(resp_remot)
......
......@@ -71,6 +71,15 @@ type UploadFile struct {
Msg string `json:"msg"`
}
type DownloadFileData struct {
Url string `json:"url"`
}
type DownloadFile struct {
Data DownloadFileData `json:"data"`
Msg string `json:"msg"`
}
type Task struct {
Id string `json:"id"`
Progress int `json:"progress"`
......
......@@ -65,7 +65,7 @@ func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc, op str
if f.IsDir() {
// mkdir remot
httpcli.Mkdir(child.Path)
httpcli.MkdirRemot(child.Path)
if option.SubFlag {
if !IsInSlice(option.IgnorePath, f.Name()) {
......
......@@ -2,9 +2,11 @@ package travremot
import (
"encoding/json"
_ "fmt"
"io/ioutil"
"log"
"minsync/auth"
"minsync/httpcli"
"minsync/nodetree"
"net/http"
"os"
......@@ -55,12 +57,12 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
// "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=20&prefix="
// "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix="
url := "http://"
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/query_list?pages=1&pagesize=20&prefix="
url += "/front/data/query_list?pages=1&pagesize=200&prefix="
url += node.Name
log.Println(url)
......@@ -79,20 +81,29 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
json.Unmarshal(body, &jsonRes)
for _, f := range jsonRes.Data.List {
tmp := node.Name + f.Name
var child nodetree.NodeRemot
child.Name = tmp
child.Type = f.Type
child.UpdtDate = f.UpdtDate
child.Creator = f.Creator
child.Size = f.Size
tmp := node.Name + f.Name
child.Name = tmp
if f.Type == "folder" {
child.Name += "/"
// mkdir
httpcli.MkdirLoc(child.Name)
node.Children = append(node.Children, &child)
exploreRecursive(&child, option)
} else if f.Type == "file" {
// download
httpcli.Download(&child)
node.Children = append(node.Children, &child)
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!