Commit 62264379 by shenyizhong

* code review

1 parent b66203ce
package def
type ErrCode int32
const (
SUCCESS ErrCode = 0
ERR_SYSTEM ErrCode = -10001
ERR_UNKNOWN ErrCode = -10002
ERR_NOT_LOGIN ErrCode = -10003
ERR_PATH_NOT_EXISTED ErrCode = -10004
ERR_PATH_HAS_EXISTED ErrCode = -10005
ERR_JSON_PARSE ErrCode = -10006
ERR_HTTP_REQUEST ErrCode = -10007
ERR_INVALID_PATH ErrCode = -10008
)
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"minsync/auth" "minsync/auth"
"minsync/def"
"minsync/nodetree" "minsync/nodetree"
"net/http" "net/http"
"os" "os"
...@@ -20,16 +21,31 @@ import ( ...@@ -20,16 +21,31 @@ import (
var host string var host string
var port int var port int
type SvrResp struct {
Data interface{}
Msg string
}
func init() { func init() {
log.Println("minsync.httpcli.init() called") log.Println("minsync.httpcli.init() called")
file, _ := os.Open("./config.json") pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
file, err := os.Open(pwd + "/config.json")
if err != nil {
fmt.Println(err)
return
}
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(err)
return
} }
host = conf.Host host = conf.Host
port = conf.Port port = conf.Port
...@@ -38,7 +54,10 @@ func init() { ...@@ -38,7 +54,10 @@ func init() {
func Get(url string) ([]byte, error) { func Get(url string) ([]byte, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
defer resp.Body.Close() defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err return body, err
} }
...@@ -54,11 +73,45 @@ func Update(postdata string) ([]byte, error) { ...@@ -54,11 +73,45 @@ func Update(postdata string) ([]byte, error) {
return nil, nil return nil, nil
} }
func MkdirRemot(path string) { func MkdirRemotRaw(path string) (def.ErrCode, error) {
// "/front/data/createfolder?name=data/folder3/"
url := "http://"
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/createfolder?name="
url += path
log.Println("httpcli.MkdirRemotRaw() 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 {
fmt.Println(err)
return def.ERR_HTTP_REQUEST, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return def.ERR_UNKNOWN, err
} else {
var dl SvrResp
json.Unmarshal(body, &dl)
if dl.Msg == "user do not login" {
return def.ERR_NOT_LOGIN, nil
}
}
return def.SUCCESS, nil
}
func MkdirRemot(path string) (def.ErrCode, error) {
var tmpStrs = strings.Split(path, nodetree.StrSrc) var tmpStrs = strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) < 2 { if len(tmpStrs) < 2 {
return return def.ERR_INVALID_PATH, nil
} }
// "/front/data/createfolder?name=data/folder3/" // "/front/data/createfolder?name=data/folder3/"
...@@ -72,27 +125,34 @@ func MkdirRemot(path string) { ...@@ -72,27 +125,34 @@ func MkdirRemot(path string) {
url += "/" url += "/"
log.Println("httpcli.Mkdir() called, GET request:", url) log.Println("httpcli.Mkdir() called, GET request:", url)
req, _ := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil {
return def.ERR_HTTP_REQUEST, err
}
req.Header.Add("token", auth.GetToken()) req.Header.Add("token", auth.GetToken())
var resp *http.Response var resp *http.Response
resp, err := http.DefaultClient.Do(req) resp, err = http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return def.ERR_UNKNOWN, err
} }
body, _ := ioutil.ReadAll(resp.Body)
log.Println(string(body)) log.Println(string(body))
return def.SUCCESS, nil
} }
func Upload(node *nodetree.NodeLoc) { func Upload(node *nodetree.NodeLoc) (def.ErrCode, error) {
var tmpStrs = strings.Split(node.Path, nodetree.StrSrc) var tmpStrs = strings.Split(node.Path, nodetree.StrSrc)
if len(tmpStrs) < 2 { if len(tmpStrs) < 2 {
return return def.ERR_INVALID_PATH, nil
} }
// "/front/data/upload?name=data/folder3/ddd.txt" // "/front/data/upload?name=data/folder3/"
url := "http://" url := "http://"
url += host url += host
url += ":" url += ":"
...@@ -102,17 +162,25 @@ func Upload(node *nodetree.NodeLoc) { ...@@ -102,17 +162,25 @@ func Upload(node *nodetree.NodeLoc) {
url += tmpStrs[1] url += tmpStrs[1]
log.Println("httpcli.Upload() called, GET request:", url) log.Println("httpcli.Upload() called, GET request:", url)
req, _ := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
return def.ERR_HTTP_REQUEST, err
}
req.Header.Add("token", auth.GetToken()) req.Header.Add("token", auth.GetToken())
var resp *http.Response var resp *http.Response
resp, err = http.DefaultClient.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_HTTP_REQUEST, err
} }
body, _ := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return def.ERR_UNKNOWN, err
}
var jsonRes nodetree.UploadFile var jsonRes nodetree.UploadFile
json.Unmarshal(body, &jsonRes) json.Unmarshal(body, &jsonRes)
...@@ -146,18 +214,22 @@ func Upload(node *nodetree.NodeLoc) { ...@@ -146,18 +214,22 @@ func Upload(node *nodetree.NodeLoc) {
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return def.ERR_UNKNOWN, err
} }
defer stdout.Close() defer stdout.Close()
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Fatal(err) log.Fatal(err)
return def.ERR_UNKNOWN, err
} }
if opBytes, err := ioutil.ReadAll(stdout); err != nil { if opBytes, err := ioutil.ReadAll(stdout); err != nil {
log.Fatal(err) log.Fatal(err)
return def.ERR_UNKNOWN, err
} else { } else {
log.Println(string(opBytes)) log.Println(string(opBytes))
} }
return def.SUCCESS, nil
} }
func PathExists(path string) (bool, error) { func PathExists(path string) (bool, error) {
...@@ -171,7 +243,7 @@ func PathExists(path string) (bool, error) { ...@@ -171,7 +243,7 @@ func PathExists(path string) (bool, error) {
return false, err return false, err
} }
func MkdirLoc(path string) { func MkdirLoc(path string) (def.ErrCode, error) {
tmpStrs := strings.Split(path, nodetree.StrSrc) tmpStrs := strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) > 1 { if len(tmpStrs) > 1 {
var strDir string var strDir string
...@@ -184,20 +256,25 @@ func MkdirLoc(path string) { ...@@ -184,20 +256,25 @@ func MkdirLoc(path string) {
strDir += tmpStrs[1] strDir += tmpStrs[1]
exist, err := PathExists(strDir) exist, err := PathExists(strDir)
if err != nil {
return def.ERR_PATH_HAS_EXISTED, err
}
if !exist { if !exist {
err = os.Mkdir(strDir, 0775) err = os.Mkdir(strDir, 0775)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_SYSTEM, err
} }
} }
} }
return 0, nil
} }
func Download(node *nodetree.NodeRemot) error { func Download(node *nodetree.NodeRemot) (def.ErrCode, error) {
tmpStrs := strings.Split(node.Name, nodetree.StrSrc) tmpStrs := strings.Split(node.Name, nodetree.StrSrc)
if len(tmpStrs) < 2 { if len(tmpStrs) < 2 {
return nil return def.ERR_INVALID_PATH, nil
} }
// "/front/data/download?name=data/folder3/ddd.txt" // "/front/data/download?name=data/folder3/ddd.txt"
...@@ -210,17 +287,24 @@ func Download(node *nodetree.NodeRemot) error { ...@@ -210,17 +287,24 @@ func Download(node *nodetree.NodeRemot) error {
url += tmpStrs[1] url += tmpStrs[1]
log.Println("httpcli.Download() called, GET request:", url) log.Println("httpcli.Download() called, GET request:", url)
req, _ := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil {
return def.ERR_HTTP_REQUEST, err
}
req.Header.Add("token", auth.GetToken()) req.Header.Add("token", auth.GetToken())
var resp *http.Response var resp *http.Response
resp, err := http.DefaultClient.Do(req) resp, err = http.DefaultClient.Do(req)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return def.ERR_HTTP_REQUEST, err
} }
body, _ := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return def.ERR_UNKNOWN, err
}
var jsonRes nodetree.DownloadFile var jsonRes nodetree.DownloadFile
json.Unmarshal(body, &jsonRes) json.Unmarshal(body, &jsonRes)
...@@ -239,8 +323,8 @@ func Download(node *nodetree.NodeRemot) error { ...@@ -239,8 +323,8 @@ func Download(node *nodetree.NodeRemot) error {
res, err := http.Get(jsonRes.Data.Url) res, err := http.Get(jsonRes.Data.Url)
if err != nil { if err != nil {
fmt.Println("error occurred!") fmt.Println(err)
return err return def.ERR_HTTP_REQUEST, err
} }
defer res.Body.Close() defer res.Body.Close()
reader := bufio.NewReaderSize(res.Body, 32*1024) reader := bufio.NewReaderSize(res.Body, 32*1024)
...@@ -248,11 +332,48 @@ func Download(node *nodetree.NodeRemot) error { ...@@ -248,11 +332,48 @@ func Download(node *nodetree.NodeRemot) error {
file, err := os.Create(strDstFile) file, err := os.Create(strDstFile)
if err != nil { if err != nil {
panic(err) panic(err)
return def.ERR_SYSTEM, err
} }
writer := bufio.NewWriter(file) writer := bufio.NewWriter(file)
written, _ := io.Copy(writer, reader) written, err := io.Copy(writer, reader)
if err != nil {
return def.ERR_SYSTEM, err
}
fmt.Printf("%v bytes\n", written) fmt.Printf("%v bytes\n", written)
return nil return def.SUCCESS, nil
}
func IsValidToken(token string) (bool, error) {
url := "http://"
url += host
url += ":"
url += strconv.Itoa(port)
url += "/front/data/query_list?pages=0&pagesize=20&prefix=data/"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false, err
}
req.Header.Add("token", token)
var resp *http.Response
resp, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return false, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
var dl SvrResp
json.Unmarshal(body, &dl)
if dl.Msg == "user do not login" {
return false, nil
}
return true, nil
} }
...@@ -7,9 +7,10 @@ import ( ...@@ -7,9 +7,10 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
_ "minsync/auth" _ "minsync/auth"
"minsync/def"
_ "minsync/download" _ "minsync/download"
_ "minsync/encrypt" _ "minsync/encrypt"
_ "minsync/httpcli" "minsync/httpcli"
"minsync/nodetree" "minsync/nodetree"
"minsync/travloc" "minsync/travloc"
"minsync/travremot" "minsync/travremot"
...@@ -24,11 +25,14 @@ func printUsage() { ...@@ -24,11 +25,14 @@ func printUsage() {
fmt.Println("Usage: seetadm --token ef0c26da821c [upload|download] src dst") fmt.Println("Usage: seetadm --token ef0c26da821c [upload|download] src dst")
} }
func isValidToken(token string) (bool, error) {
return httpcli.IsValidToken(token)
}
func main() { func main() {
fmt.Println("seetadm v0.0.3") fmt.Println("seetadm v0.0.3")
// seetadm --token ef0c26da821c upload /home/syz/tmp/abc /part1/box2
if len(os.Args) < 6 { if len(os.Args) < 6 {
printUsage() printUsage()
return return
...@@ -40,17 +44,18 @@ func main() { ...@@ -40,17 +44,18 @@ func main() {
} }
if os.Args[1] == "--token" { if os.Args[1] == "--token" {
// seetadm --token ef0c26da821c upload /home/syz/tmp/abc/ /part1/box2/
nodetree.StrToken = os.Args[2] nodetree.StrToken = os.Args[2]
nodetree.StrOp = os.Args[3] nodetree.StrOp = os.Args[3]
nodetree.StrSrc = os.Args[4] nodetree.StrSrc = os.Args[4]
nodetree.StrDst = os.Args[5] nodetree.StrDst = os.Args[5]
} else if os.Args[4] == "" { } else if os.Args[4] == "--token" {
// seetadm upload /home/syz/tmp/abc/ /part1/box2/ --token ef0c26da821c
nodetree.StrToken = os.Args[5] nodetree.StrToken = os.Args[5]
nodetree.StrOp = os.Args[1] nodetree.StrOp = os.Args[1]
nodetree.StrSrc = os.Args[2] nodetree.StrSrc = os.Args[2]
nodetree.StrDst = os.Args[3] nodetree.StrDst = os.Args[3]
} }
fmt.Printf("%v %v %v %v\n", nodetree.StrToken, nodetree.StrOp, nodetree.StrSrc, nodetree.StrDst)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1) nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1)
...@@ -96,19 +101,41 @@ func main() { ...@@ -96,19 +101,41 @@ func main() {
nodetree.StrToken = string(data) nodetree.StrToken = string(data)
} }
file, _ := os.Open("./config.json") pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
file, err := os.Open(pwd + "/config.json")
if err != nil {
fmt.Println(err)
return
}
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) fmt.Println(err)
return
} }
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 tmpstr := nodetree.StrSrc[:len(nodetree.StrSrc)-1]
idx := strings.LastIndex(tmpstr, "/")
nodetree.StrDst = nodetree.StrDst + nodetree.StrSrc[idx+1:]
mkdir_res, err := httpcli.MkdirRemotRaw(nodetree.StrDst)
if err != nil {
fmt.Println(err)
return
}
if mkdir_res == def.ERR_NOT_LOGIN {
fmt.Println("invalid token")
return
}
var option_loc nodetree.OptionLoc var option_loc nodetree.OptionLoc
option_loc.RootPath = []string{nodetree.StrSrc} option_loc.RootPath = []string{nodetree.StrSrc}
...@@ -118,11 +145,14 @@ func main() { ...@@ -118,11 +145,14 @@ func main() {
b_loc, err := json.Marshal(resp_loc) b_loc, err := json.Marshal(resp_loc)
if err != nil { if err != nil {
log.Println(err) fmt.Println(err)
} }
var out_loc bytes.Buffer var out_loc bytes.Buffer
err = json.Indent(&out_loc, b_loc, "", " ") err = json.Indent(&out_loc, b_loc, "", " ")
if err != nil {
log.Println(err)
}
// TODO, debug in config // TODO, debug in config
//out_loc.WriteTo(os.Stdout) //out_loc.WriteTo(os.Stdout)
...@@ -133,7 +163,10 @@ func main() { ...@@ -133,7 +163,10 @@ func main() {
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, err := travremot.Explorer(option_remot)
if err != nil {
log.Println(err)
}
b_remot, err := json.Marshal(resp_remot) b_remot, err := json.Marshal(resp_remot)
if err != nil { if err != nil {
...@@ -144,6 +177,5 @@ func main() { ...@@ -144,6 +177,5 @@ func main() {
err = json.Indent(&out_remot, b_remot, "", " ") err = json.Indent(&out_remot, b_remot, "", " ")
// TODO, debug in config // TODO, debug in config
//out_remot.WriteTo(os.Stdout) //out_remot.WriteTo(os.Stdout)
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!