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
|
package redis
import (
"errors"
"fmt"
"log"
"github.com/gomodule/redigo/redis"
"git.ofmax.li/iserv/internal/image"
)
const V1FilePathFmt = "v1imagepost:%s"
var ErrNotFound error = errors.New("Not Found")
// ImageRepo deps. for storage
type ImageRepo struct {
db *redis.Pool
}
// NewRedisImageRepo
func NewRedisImageRepo(conn *redis.Pool) *ImageRepo {
return &ImageRepo{
conn,
}
}
func fileKey(imageID, V1FilePathFmt string) string {
return fmt.Sprintf(V1FilePathFmt, imageID)
}
func (r *ImageRepo) AddNewFile(imageID string, meta *image.PostMeta, timeout int) error {
conn := r.db.Get()
defer conn.Close()
key := fileKey(imageID, V1FilePathFmt)
_, err := conn.Do("HMSET", redis.Args{}.Add(key).AddFlat(meta)...)
if err != nil {
log.Fatal(err)
}
return err
}
func (r *ImageRepo) GetFile(fileUrl string) (*image.PostMeta, error) {
conn := r.db.Get()
defer conn.Close()
imageMeta := &image.PostMeta{}
key := fileKey(fileUrl, V1FilePathFmt)
res, err := redis.Values(conn.Do("HGETALL", key))
if err != nil {
return &image.PostMeta{}, err
}
err = redis.ScanStruct(res, imageMeta)
if imageMeta.FilePath == "" {
return &image.PostMeta{}, ErrNotFound
}
return imageMeta, err
}
|