add comment to BuildFromFilePath

This commit is contained in:
whr819987540 2023-04-27 15:57:23 +08:00
parent 874ac2c6b6
commit 5868ed4d31
1 changed files with 8 additions and 0 deletions

View File

@ -35,7 +35,9 @@ type Info struct {
const NoName = "-" const NoName = "-"
// This is a helper that sets Files and Pieces from a root path and its children. // This is a helper that sets Files and Pieces from a root path and its children.
// fullfill the info struct
func (info *Info) BuildFromFilePath(root string) (err error) { func (info *Info) BuildFromFilePath(root string) (err error) {
// name
info.Name = func() string { info.Name = func() string {
b := filepath.Base(root) b := filepath.Base(root)
switch b { switch b {
@ -45,6 +47,8 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
return b return b
} }
}() }()
// files
info.Files = nil info.Files = nil
err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error { err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -74,9 +78,13 @@ func (info *Info) BuildFromFilePath(root string) (err error) {
slices.Sort(info.Files, func(l, r FileInfo) bool { slices.Sort(info.Files, func(l, r FileInfo) bool {
return strings.Join(l.Path, "/") < strings.Join(r.Path, "/") return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
}) })
// piece length
if info.PieceLength == 0 { if info.PieceLength == 0 {
info.PieceLength = ChoosePieceLength(info.TotalLength()) info.PieceLength = ChoosePieceLength(info.TotalLength())
} }
// pieces
err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) { err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator)))) return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
}) })