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 (
"io/ioutil"
"log"
"minsync/auth"
"minsync/def"
"minsync/nodetree"
"net/http"
"os"
......@@ -20,16 +21,31 @@ import (
var host string
var port int
type SvrResp struct {
Data interface{}
Msg string
}
func init() {
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()
decoder := json.NewDecoder(file)
conf := nodetree.Configuration{}
err := decoder.Decode(&conf)
err = decoder.Decode(&conf)
if err != nil {
log.Println("Error:", err)
log.Println(err)
return
}
host = conf.Host
port = conf.Port
......@@ -38,7 +54,10 @@ func init() {
func Get(url string) ([]byte, error) {
resp, err := http.Get(url)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
......@@ -54,11 +73,45 @@ func Update(postdata string) ([]byte, error) {
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)
if len(tmpStrs) < 2 {
return
return def.ERR_INVALID_PATH, nil
}
// "/front/data/createfolder?name=data/folder3/"
......@@ -72,27 +125,34 @@ func MkdirRemot(path string) {
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())
var resp *http.Response
resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
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))
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)
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 += host
url += ":"
......@@ -102,17 +162,25 @@ func Upload(node *nodetree.NodeLoc) {
url += tmpStrs[1]
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())
var resp *http.Response
resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
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
json.Unmarshal(body, &jsonRes)
......@@ -146,18 +214,22 @@ func Upload(node *nodetree.NodeLoc) {
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
return def.ERR_UNKNOWN, err
}
defer stdout.Close()
if err := cmd.Start(); err != nil {
log.Fatal(err)
return def.ERR_UNKNOWN, err
}
if opBytes, err := ioutil.ReadAll(stdout); err != nil {
log.Fatal(err)
return def.ERR_UNKNOWN, err
} else {
log.Println(string(opBytes))
}
return def.SUCCESS, nil
}
func PathExists(path string) (bool, error) {
......@@ -171,7 +243,7 @@ func PathExists(path string) (bool, error) {
return false, err
}
func MkdirLoc(path string) {
func MkdirLoc(path string) (def.ErrCode, error) {
tmpStrs := strings.Split(path, nodetree.StrSrc)
if len(tmpStrs) > 1 {
var strDir string
......@@ -184,20 +256,25 @@ func MkdirLoc(path string) {
strDir += tmpStrs[1]
exist, err := PathExists(strDir)
if err != nil {
return def.ERR_PATH_HAS_EXISTED, err
}
if !exist {
err = os.Mkdir(strDir, 0775)
if err != nil {
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)
if len(tmpStrs) < 2 {
return nil
return def.ERR_INVALID_PATH, nil
}
// "/front/data/download?name=data/folder3/ddd.txt"
......@@ -210,17 +287,24 @@ func Download(node *nodetree.NodeRemot) error {
url += tmpStrs[1]
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())
var resp *http.Response
resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
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
json.Unmarshal(body, &jsonRes)
......@@ -239,8 +323,8 @@ func Download(node *nodetree.NodeRemot) error {
res, err := http.Get(jsonRes.Data.Url)
if err != nil {
fmt.Println("error occurred!")
return err
fmt.Println(err)
return def.ERR_HTTP_REQUEST, err
}
defer res.Body.Close()
reader := bufio.NewReaderSize(res.Body, 32*1024)
......@@ -248,11 +332,48 @@ func Download(node *nodetree.NodeRemot) error {
file, err := os.Create(strDstFile)
if err != nil {
panic(err)
return def.ERR_SYSTEM, err
}
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)
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 (
"io/ioutil"
"log"
_ "minsync/auth"
"minsync/def"
_ "minsync/download"
_ "minsync/encrypt"
_ "minsync/httpcli"
"minsync/httpcli"
"minsync/nodetree"
"minsync/travloc"
"minsync/travremot"
......@@ -24,11 +25,14 @@ func printUsage() {
fmt.Println("Usage: seetadm --token ef0c26da821c [upload|download] src dst")
}
func isValidToken(token string) (bool, error) {
return httpcli.IsValidToken(token)
}
func main() {
fmt.Println("seetadm v0.0.3")
// seetadm --token ef0c26da821c upload /home/syz/tmp/abc /part1/box2
if len(os.Args) < 6 {
printUsage()
return
......@@ -40,17 +44,18 @@ func main() {
}
if os.Args[1] == "--token" {
// seetadm --token ef0c26da821c upload /home/syz/tmp/abc/ /part1/box2/
nodetree.StrToken = os.Args[2]
nodetree.StrOp = os.Args[3]
nodetree.StrSrc = os.Args[4]
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.StrOp = os.Args[1]
nodetree.StrSrc = os.Args[2]
nodetree.StrDst = os.Args[3]
}
fmt.Printf("%v %v %v %v\n", nodetree.StrToken, nodetree.StrOp, nodetree.StrSrc, nodetree.StrDst)
if runtime.GOOS == "windows" {
nodetree.StrSrc = strings.Replace(nodetree.StrSrc, "\\", "/", -1)
......@@ -96,19 +101,41 @@ func main() {
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()
decoder := json.NewDecoder(file)
conf := nodetree.Configuration{}
err = decoder.Decode(&conf)
if err != nil {
log.Println("Error:", err)
fmt.Println(err)
return
}
if nodetree.StrOp == "upload" {
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
option_loc.RootPath = []string{nodetree.StrSrc}
......@@ -118,11 +145,14 @@ func main() {
b_loc, err := json.Marshal(resp_loc)
if err != nil {
log.Println(err)
fmt.Println(err)
}
var out_loc bytes.Buffer
err = json.Indent(&out_loc, b_loc, "", " ")
if err != nil {
log.Println(err)
}
// TODO, debug in config
//out_loc.WriteTo(os.Stdout)
......@@ -133,7 +163,10 @@ func main() {
var option_remot nodetree.OptionRemot
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)
if err != nil {
......@@ -144,6 +177,5 @@ func main() {
err = json.Indent(&out_remot, b_remot, "", " ")
// TODO, debug in config
//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!