background/utils/base.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-04-07 04:25:07 +00:00
package utils
import (
"fmt"
2020-05-11 03:35:45 +00:00
"io/ioutil"
"os"
"path/filepath"
"strings"
)
2019-04-07 04:25:07 +00:00
func ByteSliceToString(b []byte) string {
var ret string
for i := 0; i < len(b); i++ {
s := fmt.Sprintf("%02x", b[i])
ret += string(s)
}
return ret
}
func GetCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return "./"
}
return strings.Replace(dir, "\\", "/", -1)
}
2020-05-11 03:35:45 +00:00
2020-05-11 07:14:22 +00:00
type FileDesc struct {
Child []FileDesc `json:"child"`
Types int32 `json:"types"`
Name string `json:"name"`
}
func GetPathFileName(pathname string,desc *FileDesc) (error) {
desc.Name = pathname
desc.Child = make([]FileDesc,0)
desc.Types = 1 // 1 means fileholder
2020-05-11 03:35:45 +00:00
rd, err := ioutil.ReadDir(pathname)
for _, fi := range rd {
if fi.IsDir() {
2020-05-11 07:14:22 +00:00
childdir := new (FileDesc)
childdir.Types = 1
childdir.Name = fi.Name()
2020-05-11 08:28:25 +00:00
GetPathFileName(pathname + fi.Name() + "\\",childdir)
2020-05-11 07:14:22 +00:00
desc.Child = append(desc.Child,*childdir)
2020-05-11 03:35:45 +00:00
} else {
2020-05-11 07:14:22 +00:00
childdir := new (FileDesc)
childdir.Types = 0
childdir.Name = fi.Name()
desc.Child = append(desc.Child,*childdir)
2020-05-11 03:35:45 +00:00
}
}
return err
}