This repository has been archived by the owner on May 6, 2024. It is now read-only.
/
image.go
121 lines (107 loc) · 3.31 KB
/
image.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package images
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
"github.com/rs/zerolog"
"github.com/Skarlso/kube-cluster-sample/receiver/models"
)
// Config db configs here
type Config struct {
Port string
Dbname string
UsernamePassword string
Hostname string
Logger zerolog.Logger
}
type imageProvider struct {
config Config
}
// NewImageProvider creates a new image provider using the db.
func NewImageProvider(cfg Config) *imageProvider {
return &imageProvider{config: cfg}
}
func (i *imageProvider) openConnection() (*sql.DB, error) {
connectionString := fmt.Sprintf("%s@tcp(%s:%s)/%s",
i.config.UsernamePassword,
i.config.Hostname,
i.config.Port,
i.config.Dbname)
return sql.Open("mysql", connectionString)
}
// SaveImage takes an image model and saves it into the database.
func (i *imageProvider) SaveImage(image *models.Image) (*models.Image, error) {
i.config.Logger.Debug().Str("path", string(image.Path)).Msg("Saving image path...")
if ok, _ := i.SearchPath(string(image.Path)); ok {
return nil, fmt.Errorf("image with path '%s' already exists", string(image.Path))
}
conn, err := i.openConnection()
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to open connection")
return nil, err
}
defer i.closeWithLog(conn)
result, err := conn.Exec("insert into images (path, person, status) values (?, ?, ?)", image.Path, image.PersonID, image.Status)
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to run insert")
return nil, err
}
id, _ := result.LastInsertId()
image.ID = id
return image, nil
}
// LoadImage takes an id and looks for that id in the database.
func (i *imageProvider) LoadImage(id int64) (*models.Image, error) {
log.Println("Loading image with ID: ", id)
conn, err := i.openConnection()
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to open connection")
return &models.Image{}, err
}
defer i.closeWithLog(conn)
var (
imageID int
path string
person int
status int
)
err = conn.QueryRow("select id, path, person, status from images where id = ?", id).Scan(&imageID, &path, &person, status)
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to query images")
return &models.Image{}, err
}
ret := &models.Image{
ID: int64(imageID),
Path: []byte(path),
PersonID: person,
Status: models.Status(status),
}
return ret, nil
}
// SearchPath takes an image path and checks if there is an image in the database with that path.
func (i *imageProvider) SearchPath(path string) (bool, error) {
conn, err := i.openConnection()
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to open connection")
return false, err
}
defer i.closeWithLog(conn)
row, err := conn.Query("select 1 from images where path = ?", path)
if err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to select from images")
return false, err
}
defer i.closeWithLog(row)
return row.Next(), nil
}
// Closer defines an entity which can close a connection.
type Closer interface {
Close() error
}
// closeWithLog closes a connection and logs if it errors but does not interrupt normal flow.
func (i *imageProvider) closeWithLog(closer Closer) {
if err := closer.Close(); err != nil {
i.config.Logger.Debug().Err(err).Msg("failed to close connection")
}
}