Commit 76bb4781 by shenyizhong

* support pages

1 parent 78a96136
...@@ -154,16 +154,32 @@ func Upload(node *nodetree.NodeLoc) { ...@@ -154,16 +154,32 @@ func Upload(node *nodetree.NodeLoc) {
} }
} }
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func MkdirLoc(path string) { func MkdirLoc(path string) {
tmpStrs := strings.Split(path, nodetree.StrSrc) tmpStrs := strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) > 1 { if len(tmpStrs) > 1 {
strDir := nodetree.StrDst + "/" strDir := nodetree.StrDst + "/"
strDir += tmpStrs[1] strDir += tmpStrs[1]
err := os.Mkdir(strDir, 0775)
exist, err := PathExists(strDir)
if !exist {
err = os.Mkdir(strDir, 0775)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
} }
}
} }
func Download(node *nodetree.NodeRemot) { func Download(node *nodetree.NodeRemot) {
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
_ "io/ioutil" "io/ioutil"
"log" "log"
_ "minsync/auth" _ "minsync/auth"
_ "minsync/download" _ "minsync/download"
...@@ -25,7 +25,7 @@ func printUsage() { ...@@ -25,7 +25,7 @@ func printUsage() {
func main() { func main() {
fmt.Println("minsync v0.0.1") fmt.Println("minsync v0.0.2")
// minsync --token ef0c26da821c upload /home/syz/tmp/abc /part1/box2 // minsync --token ef0c26da821c upload /home/syz/tmp/abc /part1/box2
if len(os.Args) < 6 { if len(os.Args) < 6 {
...@@ -51,11 +51,31 @@ func main() { ...@@ -51,11 +51,31 @@ func main() {
} }
fmt.Printf("%v %v %v %v\n", nodetree.StrToken, nodetree.StrOp, nodetree.StrSrc, nodetree.StrDst) fmt.Printf("%v %v %v %v\n", nodetree.StrToken, nodetree.StrOp, nodetree.StrSrc, nodetree.StrDst)
// check dst format
if nodetree.StrDst[len(nodetree.StrDst)-1:] != "/" {
fmt.Printf("target path [%v] format error, need end with /\n", nodetree.StrDst)
return
}
if nodetree.StrSrc[len(nodetree.StrSrc)-1:] != "/" {
fmt.Printf("source path [%v] format error, need end with /\n", nodetree.StrSrc)
return
}
_, err := os.Lstat(nodetree.StrToken)
if !os.IsNotExist(err) {
data, err := ioutil.ReadFile(nodetree.StrToken)
if err != nil {
return
}
nodetree.StrToken = string(data)
}
file, _ := os.Open("./config.json") file, _ := os.Open("./config.json")
defer file.Close() defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
conf := nodetree.Configuration{} conf := nodetree.Configuration{}
err := decoder.Decode(&conf) err = decoder.Decode(&conf)
if err != nil { if err != nil {
log.Println("Error:", err) log.Println("Error:", err)
} }
...@@ -63,6 +83,8 @@ func main() { ...@@ -63,6 +83,8 @@ func main() {
if nodetree.StrOp == "upload" { if nodetree.StrOp == "upload" {
nodetree.StrDst = "data" + nodetree.StrDst nodetree.StrDst = "data" + nodetree.StrDst
// TODO, check if nodetree.StrDst is FILE or DIR
var option_loc nodetree.OptionLoc var option_loc nodetree.OptionLoc
option_loc.RootPath = []string{nodetree.StrSrc} option_loc.RootPath = []string{nodetree.StrSrc}
option_loc.SubFlag = true option_loc.SubFlag = true
...@@ -82,6 +104,8 @@ func main() { ...@@ -82,6 +104,8 @@ func main() {
} else if nodetree.StrOp == "download" { } else if nodetree.StrOp == "download" {
nodetree.StrSrc = "data" + nodetree.StrSrc nodetree.StrSrc = "data" + nodetree.StrSrc
// TODO, check if nodetree.StrDst is FILE or DIR
var option_remot nodetree.OptionRemot var option_remot nodetree.OptionRemot
option_remot.RootPath = []string{nodetree.StrSrc} option_remot.RootPath = []string{nodetree.StrSrc}
resp_remot, _ := travremot.Explorer(option_remot) resp_remot, _ := travremot.Explorer(option_remot)
......
...@@ -58,13 +58,13 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) { ...@@ -58,13 +58,13 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name) log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
// "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix=" // "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=200&prefix="
url := "http://" url := "http://"
url += host url += host
url += ":" url += ":"
url += strconv.Itoa(port) url += strconv.Itoa(port)
url += "/front/data/query_list?pages=1&pagesize=200&prefix=" url += "/front/data/query_list?pages=1&pagesize=1&prefix="
url += node.Name url += node.Name
log.Println(url)
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("token", auth.GetToken()) req.Header.Add("token", auth.GetToken())
...@@ -74,13 +74,39 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) { ...@@ -74,13 +74,39 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
var jsonRes nodetree.DataRemot var jsonRes nodetree.DataRemot
json.Unmarshal(body, &jsonRes) json.Unmarshal(body, &jsonRes)
for _, f := range jsonRes.Data.List { url = "http://"
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/query_list?pages=1&pagesize="
url += strconv.Itoa(jsonRes.Data.Total)
url += "&prefix="
url += node.Name
log.Println(url)
req, _ = http.NewRequest("GET", url, nil)
req.Header.Add("token", auth.GetToken())
var respAll *http.Response
respAll, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
defer respAll.Body.Close()
bodyAll, _ := ioutil.ReadAll(respAll.Body)
var jsonResAll nodetree.DataRemot
json.Unmarshal(bodyAll, &jsonResAll)
for _, f := range jsonResAll.Data.List {
var child nodetree.NodeRemot var child nodetree.NodeRemot
child.Type = f.Type child.Type = f.Type
...@@ -108,6 +134,4 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) { ...@@ -108,6 +134,4 @@ func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
} }
} }
defer resp.Body.Close()
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!