feat: add locally syncing and watchdog
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
)
|
||||
|
||||
func HashReader(r io.Reader) (string, error) {
|
||||
h := sha512.New()
|
||||
if _, err := io.Copy(h, r); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// return the sha checksum in hex
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
// alternatively return in base64
|
||||
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
func HashUpload(fileHeader *multipart.FileHeader) (string, error) {
|
||||
stream, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
return HashReader(stream)
|
||||
}
|
||||
|
||||
func HashFile(path string) (string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return HashReader(file)
|
||||
}
|
||||
Reference in New Issue
Block a user