-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
153 lines (131 loc) · 2.93 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package data
import (
"fmt"
"gorm.io/gorm"
"io"
"log"
"os"
"path"
)
// Image represents a movie image.
type Image struct {
Id int `gorm:"column:id;primary_key"`
Data []byte `gorm:"column:image;"`
}
const imageCache = "/home/per/.cache/"
// TableName returns the name of the table.
func (i *Image) TableName() string {
return "image"
}
// GetImage returns an image from the database.
func (d *Database) GetImage(id int) (*Image, error) {
image := Image{}
// Load from cache
cachePath := path.Join(imageCache, "softimdb", fmt.Sprintf("%d.png", id))
if d.existCachedImage(cachePath) {
image.Id = id
err := d.getCachedImage(&image, cachePath)
if err == nil {
return &image, nil
}
}
db, err := d.getDatabase()
if err != nil {
return nil, err
}
if result := db.Where("Id=?", id).First(&image); result.Error != nil {
return nil, result.Error
}
// Store in cache
if !d.existCachedImage(cachePath) {
d.storeCachedImage(&image, cachePath)
}
return &image, nil
}
func (d *Database) storeCachedImage(image *Image, cachePath string) {
if !d.existCachedImage(path.Join(imageCache, "softimdb")) {
err := os.Mkdir(path.Join(imageCache, "softimdb"), os.ModePerm)
if err != nil {
return
}
}
file, err := os.Create(cachePath)
if err != nil {
return
}
defer func() {
err = file.Close()
if err != nil {
log.Print(err)
}
}()
_, err = file.Write(image.Data)
if err != nil {
return
}
}
// existCachedImage returns true if an image exists in the cache.
func (d *Database) existCachedImage(cachePath string) bool {
if _, err := os.Stat(cachePath); os.IsNotExist(err) {
return false
}
return true
}
// getCachedImage returns an image from the cache.
func (d *Database) getCachedImage(image *Image, cachePath string) error {
file, err := os.Open(cachePath)
if err != nil {
return err
}
defer func() {
err = file.Close()
if err != nil {
log.Print(err)
}
}()
data, err := io.ReadAll(file)
if err != nil {
return err
}
image.Data = data
return nil
}
// InsertImage inserts an image into the database.
func (d *Database) InsertImage(image *Image) error {
db, err := d.getDatabase()
if err != nil {
return err
}
if result := db.Create(image); result.Error != nil {
return result.Error
}
return nil
}
// UpdateImage removes an image from the database.
func (d *Database) UpdateImage(movie *Movie, imageData []byte) error {
db, err := d.getDatabase()
if err != nil {
return err
}
err = db.Transaction(
func(tx *gorm.DB) error {
if result := db.Delete(&Image{}, movie.ImageId); result.Error != nil {
return result.Error
}
image := &Image{Data: imageData}
if result := db.Create(image); result.Error != nil {
return result.Error
}
if result := db.Model(&movie).Update("image_id", image.Id); result.Error != nil {
return result.Error
}
// TODO : Update cache
return nil
},
)
// Check transaction error
if err != nil {
return err
}
return nil
}