background/utils/base.go

53 lines
1.1 KiB
Go

package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
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)
}
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
rd, err := ioutil.ReadDir(pathname)
for _, fi := range rd {
if fi.IsDir() {
childdir := new (FileDesc)
childdir.Types = 1
childdir.Name = fi.Name()
GetPathFileName(pathname + fi.Name() + "\\",childdir)
desc.Child = append(desc.Child,*childdir)
} else {
childdir := new (FileDesc)
childdir.Types = 0
childdir.Name = fi.Name()
desc.Child = append(desc.Child,*childdir)
}
}
return err
}