Commit 18bd56ba by shenyizhong

+ files

0 parents
src/minsync
src/minsync.log
package auth
import (
"fmt"
"os"
"log"
)
func init() {
logFile, err := os.OpenFile("./minsync.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
fmt.Println("open log file failed, err:", err)
return
}
log.SetOutput(logFile)
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
log.Println("auth.init() called")
}
func Login(user string, password string) (string, error) {
var token string
token = ""
return token, nil
}
func GetToken() string {
var token_op string
token_op = "96eb895c9a07435ea6fb6ba61543c4b2"
return token_op
}
package download
import (
"os"
"log"
"github.com/sirupsen/logrus"
"minsync/nodetree"
)
func init() {
log.Println("minsync.download.init() called")
var log01 = logrus.New()
log01.Formatter = new(logrus.JSONFormatter)
log01.Formatter = new(logrus.TextFormatter) //default
log01.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
log01.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
log01.Level = logrus.TraceLevel
log01.Out = os.Stdout
log01.Debug("minsync.download.init() called, from logrus")
}
func Download(src string, dst string) (nodetree.Task, error) {
var task nodetree.Task
return task, nil
}
func QueryProgress(task_id string) (nodetree.Task, error) {
var task nodetree.Task
return task, nil
}
package encrypt
import (
_"fmt"
"log"
)
func init() {
log.Println("minsync.encrypt.init() called")
}
module minsync
go 1.13
require github.com/sirupsen/logrus v1.8.1
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
package httpcli
import (
"fmt"
"io/ioutil"
"net/http"
"log"
)
func init() {
log.Println("minsync.httpcli.init() called")
}
func Get(url string) ([]byte, error) {
fmt.Println(url)
resp, err := http.Get(url)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body, err
}
func Post(postdata string) ([]byte, error) {
return nil, nil
}
func Delete(postdata string) ([]byte, error) {
return nil, nil
}
func Update(postdata string) ([]byte, error) {
return nil, nil
}
package main
import (
"bytes"
"os"
"encoding/json"
_ "fmt"
_ "io/ioutil"
"log"
_ "minsync/auth"
_ "minsync/download"
_ "minsync/encrypt"
_ "minsync/httpcli"
"minsync/nodetree"
_ "minsync/travloc"
"minsync/travremot"
_ "minsync/upload"
_ "net/http"
"runtime"
)
func main() {
var nodeloc nodetree.NodeLoc
nodeloc.Name = "look"
log.Printf("main() called, os %v, %v", runtime.GOOS, nodeloc.Name)
var option_remot nodetree.OptionRemot
option_remot.RootPath = []string{"data/"}
resp, _ := travremot.Explorer(option_remot)
b, err := json.Marshal(resp)
if err != nil {
log.Println(err)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
out.WriteTo(os.Stdout)
}
No preview for this file type
package nodetree
import (
_"fmt"
"log"
)
type NodeLoc struct {
Name string `json:"name"`
Path string `json:"path"`
Children []*NodeLoc `json:"children"`
IsDir bool `json:"isDir"`
}
type OptionLoc struct {
RootPath []string `yaml:"rootPath"`
SubFlag bool `yaml:"subFlag"` // if traverse sub dir
IgnorePath []string `yaml:"ignorePath"`
IgnoreFile []string `yaml:"ignoreFile"`
}
type NodeRemot struct {
Name string `json:"name"`
Type string `json:"type"`
UpdtDate string `json:"update_date"`
Creator string `json:"creator"`
Size int `json:"size"`
Children []*NodeRemot `json:"children"`
}
type ListRemot struct {
List []*NodeRemot `json:"list"`
Total int `json:"total"`
PageSize int `json:"pagesize"`
Pages int `json:"pages"`
}
type DataRemot struct {
Data ListRemot `json:"data"`
Msg string `json:"msg"`
}
type OptionRemot struct {
RootPath []string `yaml:"rootPath"`
SubFlag bool `yaml:"subFlag"` // if traverse sub dir
PageSize int `yaml:"pagesize"`
Pages int `yaml:"pages"`
Token string `yaml:"token"`
}
type Task struct {
Id string `json:"id"`
Progress int `json:"progress"`
State int `json:"state"`
}
func init() {
log.Println("minsync.nodetree init() called")
}
package travloc
import (
_"fmt"
"strings"
"os"
"io/ioutil"
"path"
"log"
"minsync/nodetree"
)
func init() {
log.Println("minsync.travloc.init() called")
}
func Explorer(option nodetree.OptionLoc) (nodetree.NodeLoc, error) {
var root nodetree.NodeLoc
for _, p := range option.RootPath {
if strings.TrimSpace(p) == "" {
continue
}
var child nodetree.NodeLoc
child.Path = p
exploreRecursive(&child, &option)
root.Children = append(root.Children, &child)
}
return root, nil
}
func exploreRecursive(node *nodetree.NodeLoc, option *nodetree.OptionLoc) {
p, err := os.Stat(node.Path)
if err != nil {
log.Println(err)
return
}
node.Name = p.Name()
node.IsDir = p.IsDir()
if !p.IsDir() {
return
}
sub, err := ioutil.ReadDir(node.Path)
if err != nil {
info := "dir is not exist, or open failed"
log.Printf("%v: %v", info, err)
return
}
for _, f := range sub {
tmp := path.Join(node.Path, f.Name())
var child nodetree.NodeLoc
child.Path = tmp
child.IsDir = f.IsDir()
if f.IsDir() {
if option.SubFlag {
if !IsInSlice(option.IgnorePath, f.Name()) {
node.Children = append(node.Children, &child)
exploreRecursive(&child, option)
}
} else {
if !IsInSlice(option.IgnoreFile, f.Name()) {
child.Name = f.Name()
node.Children = append(node.Children, &child)
}
}
} else {
if !IsInSlice(option.IgnoreFile, f.Name()) {
child.Name = f.Name()
node.Children = append(node.Children, &child)
}
}
}
}
func IsInSlice(slice []string, s string) bool {
if len(slice) == 0 {
return false;
}
isIn := false
for _, f := range slice {
if f == s {
isIn = true
break
}
}
return isIn
}
package travloc
import (
"fmt"
"bytes"
"encoding/json"
"log"
"os"
"runtime"
"testing"
"minsync/nodetree"
)
func TestExplorer(t *testing.T) {
var option nodetree.Option
fmt.Println(runtime.GOOS)
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
option.RootPath = []string{
`/home/syz/workshop/projects/minsync/src`,
}
}
if runtime.GOOS == "windows" {
option.RootPath = []string{`C:\`}
}
option.SubFlag = true
option.IgnorePath = []string{`.git`,`.svn`}
option.IgnoreFile = []string{`.DS_Store`, `.gitignore`}
result, err := Explorer(option)
if err != nil {
panic(err)
}
OutTerminal(result)
}
func OutTerminal(obj interface{}) {
b, err := json.Marshal(obj)
if err != nil {
log.Println(err)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
out.WriteTo(os.Stdout)
}
No preview for this file type
package travremot
import (
"strings"
"log"
"encoding/json"
"io/ioutil"
"net/http"
"minsync/auth"
"minsync/nodetree"
)
func init() {
log.Println("minsync.travremot.init() called")
}
func Explorer(option nodetree.OptionRemot) (nodetree.NodeRemot, error) {
var root nodetree.NodeRemot
for _, p := range option.RootPath {
if strings.TrimSpace(p) == "" {
continue
}
var child nodetree.NodeRemot
child.Name = p
exploreRecursive(&child, &option)
root.Children = append(root.Children, &child)
}
return root, nil
}
func exploreRecursive(node *nodetree.NodeRemot, option *nodetree.OptionRemot) {
log.Printf("travremot.exploreRecursive() node.Name: %s", node.Name)
url := "http://122.112.207.59:8989/front/data/query_list?pages=1&pagesize=20&prefix="
url += node.Name
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.DataRemot
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
if f.Type == "folder" {
child.Name += "/"
node.Children = append(node.Children, &child)
exploreRecursive(&child, option)
} else if f.Type == "file" {
node.Children = append(node.Children, &child)
}
}
defer resp.Body.Close()
}
package travremot
import (
"bytes"
"encoding/json"
"log"
"minsync/nodetree"
"os"
"testing"
)
func TestExplorer(t *testing.T) {
var option nodetree.OptionRemot
option.RootPath = []string{
`data/`,
}
option.SubFlag = true
result, err := Explorer(option)
if err != nil {
panic(err)
}
OutTerminal(result)
}
func OutTerminal(obj interface{}) {
b, err := json.Marshal(obj)
if err != nil {
log.Println(err)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
out.WriteTo(os.Stdout)
}
package upload
import (
_"fmt"
"log"
"minsync/nodetree"
)
func init() {
log.Println("minsync.upload.init() called")
}
func Upload(src string, dst string) (nodetree.Task, error) {
var task nodetree.Task
return task, nil
}
func QueryProgress(task_id string) (nodetree.Task, error) {
var task nodetree.Task
return task, nil
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!