This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
/
util.go
71 lines (60 loc) · 1.49 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package internal
import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"io"
"math/rand"
"net/http"
"os"
"path"
"strconv"
"time"
"github.com/gabriel-vasile/mimetype"
"github.com/google/uuid"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Salt returns a random salt
func Salt() string {
return strconv.FormatInt(rand.Int63(), 36)
}
// Hash returns the SHA1 hash of a string
func Hash(s string) string {
algorithm := sha1.New()
algorithm.Write([]byte(s))
return hex.EncodeToString(algorithm.Sum(nil))
}
// FilenameToUUID returns an UUID from a filename
func FilenameToUUID(filename string) string {
algorithm := md5.New()
algorithm.Write([]byte(path.Clean(filename)))
bytes := algorithm.Sum(nil)
bytes[6] = (bytes[6] & 0x0f) | 0x40 // Version 4
bytes[8] = (bytes[8] & 0x3f) | 0x80 // Variant is 10
return uuid.Must(uuid.FromBytes(bytes)).String()
}
// IsFolder returns whether a relative path is a folder
func IsFolder(root, relPath string) bool {
fi, err := os.Stat(path.Join(root, relPath))
if err != nil {
return false
}
return fi.IsDir()
}
// DetectContentType determines the MIME type of a given file
func DetectContentType(r io.ReadSeeker) (string, error) {
r.Seek(0, io.SeekStart)
mime, err := mimetype.DetectReader(r)
if err != nil {
return mime.String(), err
}
if mime.String() == "application/octet-stream" {
var header [512]byte
r.Seek(0, io.SeekStart)
_, _ = r.Read(header[:])
return http.DetectContentType(header[:]), nil
}
return mime.String(), nil
}