Former-commit-id: 8220f5660a75af69ee6358af3782416dae9aa185 [formerly 098749492c954879b5a01324076804e74947c780] [formerly 34f6dda3fafc72730b4e44d0c23ade6940bb90d4 [formerly 4b3ebca48c]]
Former-commit-id: 722e71cd19b9ecceb627cc1e65572839bd2a55c8 [formerly 23d5ac81b9ff1f39b51cc042eb2ac12d2e5da6fc]
Former-commit-id: c4e5ea0d6db7edcfdc3b551e0c43d1c77ef587c3
This commit is contained in:
Henrique Dias
2017-06-27 15:44:20 +01:00
parent 69a96e56b7
commit 1e99d3d7c1
9 changed files with 582 additions and 596 deletions

43
file.go
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)