mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-05 00:15:51 +00:00
hash: docs and argument
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
b3f5e06c4d
commit
416e4e85ab
2 changed files with 10 additions and 9 deletions
13
hash/hash.go
13
hash/hash.go
|
@ -14,11 +14,12 @@ func init() {
|
|||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
// Rand64 is an int64 random number
|
||||
func Rand64() int64 {
|
||||
return rand.Int63()
|
||||
}
|
||||
|
||||
/* Convinience method for getting md5 sum of a string */
|
||||
// GetMd5FromString is a convinience method for getting md5 sum of a string
|
||||
func GetMd5FromString(blob string) (sum []byte) {
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
|
@ -26,7 +27,7 @@ func GetMd5FromString(blob string) (sum []byte) {
|
|||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
/* Convinience method for getting md5 sum of some bytes */
|
||||
// GetMd5FromBytes is a convinience method for getting md5 sum of some bytes
|
||||
func GetMd5FromBytes(blob []byte) (sum []byte) {
|
||||
h := md5.New()
|
||||
defer h.Reset()
|
||||
|
@ -34,9 +35,9 @@ func GetMd5FromBytes(blob []byte) (sum []byte) {
|
|||
return h.Sum(nil)
|
||||
}
|
||||
|
||||
/* get a small, decently unique hash */
|
||||
func GetSmallHash() (small_hash string) {
|
||||
// GetSmallHash get a small, decently unique hash
|
||||
func GetSmallHash(num uint) string {
|
||||
h := sha256.New()
|
||||
io.WriteString(h, fmt.Sprintf("%d%d", Rand64()))
|
||||
return strings.ToLower(fmt.Sprintf("%X", h.Sum(nil)[0:4]))
|
||||
io.WriteString(h, fmt.Sprintf("%d%d", Rand64(), Rand64()))
|
||||
return strings.ToLower(fmt.Sprintf("%X", h.Sum(nil)[0:num]))
|
||||
}
|
||||
|
|
|
@ -274,7 +274,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
if len(filename) == 0 {
|
||||
str := hash.GetSmallHash()
|
||||
str := hash.GetSmallHash(5)
|
||||
if len(p_ext) == 0 {
|
||||
filename = fmt.Sprintf("%s.jpg", str)
|
||||
} else {
|
||||
|
@ -673,7 +673,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
if exists || useRandName {
|
||||
ext := filepath.Ext(local_filename)
|
||||
str := hash.GetSmallHash()
|
||||
str := hash.GetSmallHash(5)
|
||||
stored_filename = fmt.Sprintf("%s%s", str, ext)
|
||||
} else {
|
||||
stored_filename = filepath.Base(local_filename)
|
||||
|
@ -765,7 +765,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if exists || useRandName {
|
||||
ext := filepath.Ext(filename)
|
||||
str := hash.GetSmallHash()
|
||||
str := hash.GetSmallHash(5)
|
||||
filename = strings.ToLower(fmt.Sprintf("%s%s", str, ext))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue