mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 03:32:56 +00:00

Former-commit-id: 31f37b7a4487e7d0b5763f07f52b8f2b6157a4cd [formerly 8143cd383b7cd35339a21e93ec4d24ba91039708] [formerly 35ea25e79b4bea4d58edfb1745b844fcb12d91d9 [formerly 06c4e4d58bc50b245c78ca3f15d7512fcc16e92b]] Former-commit-id: e7f57997c2640adcfff36b605a99da9786aed3cb [formerly 2d63af9ef7a090b597239d5c0aeaf056e6366efc] Former-commit-id: cf7d129de97b0f289a6c35fa1ce9b070d9d6eac9
51 lines
909 B
Go
51 lines
909 B
Go
package filemanager
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha1"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
e "errors"
|
|
"hash"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// serveChecksum calculates the hash of a file. Supports MD5, SHA1, SHA256 and SHA512.
|
|
func serveChecksum(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
query := r.URL.Query().Get("checksum")
|
|
|
|
file, err := os.Open(c.fi.Path)
|
|
if err != nil {
|
|
return errorToHTTP(err, true), err
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
var h hash.Hash
|
|
|
|
switch query {
|
|
case "md5":
|
|
h = md5.New()
|
|
case "sha1":
|
|
h = sha1.New()
|
|
case "sha256":
|
|
h = sha256.New()
|
|
case "sha512":
|
|
h = sha512.New()
|
|
default:
|
|
return http.StatusBadRequest, e.New("Unknown HASH type")
|
|
}
|
|
|
|
_, err = io.Copy(h, file)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
val := hex.EncodeToString(h.Sum(nil))
|
|
w.Write([]byte(val))
|
|
return http.StatusOK, nil
|
|
}
|