This commit is contained in:
Henrique Dias committed 2017-06-27 15:44:20 +01:00
1 parent 230ecdea95
commit 4b3ebca48c
9 files changed
+582 -596

No files matched your search

+43
View File
@@ -1,6 +1,14 @@
package filemanager
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"hash"
"io"
"io/ioutil"
"mime"
"net/http"
@@ -13,6 +21,10 @@ import (
humanize "github.com/dustin/go-humanize"
)
var (
errInvalidOption = errors.New("Invalid option")
)
// fileInfo contains the information about a particular file or directory.
type fileInfo struct {
// Used to store the file's content temporarily.
@@ -149,6 +161,37 @@ func (i *fileInfo) Read() error {
return nil
}
func (i fileInfo) Checksum(kind string) (string, error) {
file, err := os.Open(i.Path)
if err != nil {
return "", err
}
defer file.Close()
var h hash.Hash
switch kind {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
default:
return "", errInvalidOption
}
_, err = io.Copy(h, file)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// StringifyContent returns a string with the file content.
func (i fileInfo) StringifyContent() string {
return string(i.content)