1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

feat: add FileSize, MTime, Sha

This commit is contained in:
dudaodong
2023-04-25 11:16:00 +08:00
parent fa81ee143e
commit 11217a11c7
4 changed files with 128 additions and 0 deletions

View File

@@ -8,6 +8,9 @@ import (
"archive/zip"
"bufio"
"bytes"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"io"
@@ -377,3 +380,57 @@ func CurrentPath() string {
return absPath
}
// FileSize returns file size in bytes.
// Play: todo
func FileSize(path string) (int64, error) {
f, err := os.Stat(path)
if err != nil {
return 0, err
}
return f.Size(), nil
}
// MTime returns file modified time.
// Play: todo
func MTime(filepath string) (int64, error) {
f, err := os.Stat(filepath)
if err != nil {
return 0, err
}
return f.ModTime().Unix(), nil
}
// MTime returns file modified time, param `shaType` should be 1, 256 or 512.
// Play: todo
func Sha(filepath string, shaType ...int) (string, error) {
file, err := os.Open(filepath)
if err != nil {
return "", err
}
defer file.Close()
h := sha1.New()
if len(shaType) > 0 {
if shaType[0] == 1 {
h = sha1.New()
} else if shaType[0] == 256 {
h = sha256.New()
} else if shaType[0] == 512 {
h = sha512.New()
} else {
return "", errors.New("param `shaType` should be 1, 256 or 512.")
}
}
_, err = io.Copy(h, file)
if err != nil {
return "", err
}
sha := fmt.Sprintf("%x", h.Sum(nil))
return sha, nil
}