/
avatar.go
76 lines (64 loc) · 1.58 KB
/
avatar.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
72
73
74
75
76
package avatar
import (
"database/sql"
_ "embed"
"time"
"github.com/allocamelus/allocamelus/internal/data"
"github.com/allocamelus/allocamelus/internal/pkg/fileutil"
)
var (
//go:embed sql/hasAvatar.sql
qHasAvatar string
preHasAvatar *sql.Stmt
//go:embed sql/getAvatarUrl.sql
qGetAvatarUrl string
preGetAvatarUrl *sql.Stmt
//go:embed sql/insertAvatar.sql
qInsertAvatar string
preInsertAvatar *sql.Stmt
//go:embed sql/remove.sql
qRemove string
preRemove *sql.Stmt
)
func init() {
data.PrepareQueuer.Add(&preHasAvatar, qHasAvatar)
data.PrepareQueuer.Add(&preGetAvatarUrl, qGetAvatarUrl)
data.PrepareQueuer.Add(&preInsertAvatar, qInsertAvatar)
data.PrepareQueuer.Add(&preRemove, qRemove)
}
const (
MaxHightWidth int = 500
SubPath = "users/avatars"
)
func HasAvatar(userID int64) (hasAvatar bool, err error) {
err = preHasAvatar.QueryRow(userID).Scan(&hasAvatar)
return
}
func GetUrl(userID int64) (url string, err error) {
var (
b58hash string
)
err = preGetAvatarUrl.QueryRow(userID).Scan(&b58hash)
if err != nil {
return
}
url = fileutil.PublicPath(selectorPath(b58hash, true))
return
}
func InsertAvatar(userID int64, fileType fileutil.Format, b58hash string) error {
_, err := preInsertAvatar.Exec(userID, time.Now().Unix(), fileType, b58hash)
if err != nil {
return err
}
return nil
}
func Remove(userID int64) error {
_, err := preRemove.Exec(userID)
if err != nil {
return err
}
return CleanupOld(userID)
}
func selectorPath(b58hash string, includeFile bool) string {
return fileutil.RelativePath(SubPath, b58hash, includeFile)
}