diff --git a/fileutil/file.go b/fileutil/file.go index ee8c3ac..bd89967 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -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 + +} diff --git a/fileutil/file_example_test.go b/fileutil/file_example_test.go index d945e6f..f42fbf8 100644 --- a/fileutil/file_example_test.go +++ b/fileutil/file_example_test.go @@ -235,3 +235,42 @@ func ExampleIsZipFile() { // false // true } + +func ExampleFileSize() { + size, err := FileSize("./testdata/test.txt") + + fmt.Println(size) + fmt.Println(err) + + // Output: + // 20 + // +} + +func ExampleMTime() { + mtime, err := MTime("./testdata/test.txt") + + fmt.Println(mtime) + fmt.Println(err) + + // Output: + // 1682391110 + // +} + +func ExampleSha() { + sha1, err := Sha("./testdata/test.txt", 1) + sha256, _ := Sha("./testdata/test.txt", 256) + sha512, _ := Sha("./testdata/test.txt", 512) + + fmt.Println(sha1) + fmt.Println(sha256) + fmt.Println(sha512) + fmt.Println(err) + + // Output: + // dda3cf10c5a6ff6c6659a497bf7261b287af2bc7 + // aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35 + // d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870 + // +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index 74a795e..5b5316b 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -253,3 +253,34 @@ func TestIsZipFile(t *testing.T) { assert.Equal(false, IsZipFile("./file.go")) assert.Equal(true, IsZipFile("./testdata/file.go.zip")) } + +func TestFileSize(t *testing.T) { + assert := internal.NewAssert(t, "TestFileSize") + + size, err := FileSize("./testdata/test.txt") + + assert.IsNil(err) + assert.Equal(int64(20), size) +} + +func TestMTime(t *testing.T) { + assert := internal.NewAssert(t, "TestMTime") + + mtime, err := MTime("./testdata/test.txt") + + assert.IsNil(err) + assert.Equal(int64(1682391110), mtime) +} + +func TestSha(t *testing.T) { + assert := internal.NewAssert(t, "TestMTime") + + sha1, err := Sha("./testdata/test.txt", 1) + sha256, err := Sha("./testdata/test.txt", 256) + sha512, err := Sha("./testdata/test.txt", 512) + + assert.IsNil(err) + assert.Equal("dda3cf10c5a6ff6c6659a497bf7261b287af2bc7", sha1) + assert.Equal("aa6d0a3fbc3442c228d606da09e0c1dc98c69a1cac3da1909199e0266171df35", sha256) + assert.Equal("d22aba2a1b7a2e2f512756255cc1c3708905646920cb1eb95e45b531ba74774dbbb89baebf1f716220eb9cf4908f1cfc5b2a01267704d9a59f59d77cab609870", sha512) +} diff --git a/fileutil/testdata/test.txt b/fileutil/testdata/test.txt new file mode 100644 index 0000000..c1cac2d --- /dev/null +++ b/fileutil/testdata/test.txt @@ -0,0 +1 @@ +this is a test file. \ No newline at end of file