Compare commits
No commits in common. "1a2732df7a759745424f8103b91a33bc72dab6c9" and "4cf354733ca692ab16b8a9d086d6bdeaa3975ad9" have entirely different histories.
1a2732df7a
...
4cf354733c
@ -1,9 +1,6 @@
|
||||
package osutils
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@ -50,6 +47,7 @@ func RecursiceWalk(pathToWalk string) ([]string, error) {
|
||||
func ListDir(path string) ([]fs.FileInfo, error) {
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return []fs.FileInfo{}, err
|
||||
}
|
||||
return files, nil
|
||||
@ -61,56 +59,3 @@ func GetParentDir(filePath string) (string, error) {
|
||||
finalPath := path.Join("/", path.Join(cuttedPath...))
|
||||
return finalPath, nil
|
||||
}
|
||||
|
||||
type FileValidationInfo struct {
|
||||
Path string
|
||||
fs.FileInfo
|
||||
SHA1 string
|
||||
}
|
||||
|
||||
func ValidateFile(filePath string) (FileValidationInfo, error) {
|
||||
info := FileValidationInfo{
|
||||
Path: filePath,
|
||||
FileInfo: nil,
|
||||
SHA1: "",
|
||||
}
|
||||
info.Path = filePath
|
||||
|
||||
hash, err := GetFileHash(filePath)
|
||||
if err != nil {
|
||||
return info, err
|
||||
}
|
||||
info.SHA1 = hash
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func GetFileHash(filePath string) (string, error) {
|
||||
//Initialize variable returnMD5String now in case an error has to be returned
|
||||
var returnSHA1String string
|
||||
|
||||
//Open the filepath passed by the argument and check for any error
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Tell the program to call the following function when the current function returns
|
||||
defer file.Close()
|
||||
|
||||
//Open a new SHA1 hash interface to write to
|
||||
hash := sha1.New()
|
||||
|
||||
//Copy the file in the hash interface and check for any error
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return returnSHA1String, err
|
||||
}
|
||||
|
||||
//Get the 20 bytes hash
|
||||
hashInBytes := hash.Sum(nil)[:20]
|
||||
|
||||
//Convert the bytes to a string
|
||||
returnSHA1String = hex.EncodeToString(hashInBytes)
|
||||
|
||||
return returnSHA1String, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user