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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
package models
import (
"fmt"
"imageboard/config"
"imageboard/utils/format"
"imageboard/utils/math"
"strings"
"gorm.io/gorm"
)
type ImageSize struct {
gorm.Model
ImageID uint `gorm:"not null;index" json:"-"`
Image Image `gorm:"foreignKey:ImageID" json:"image"`
SizeType ImageSizeType `gorm:"not null;size:50" json:"size_type"`
Width int `gorm:"not null" json:"width"`
Height int `gorm:"not null" json:"height"`
FileSize int64 `gorm:"not null" json:"file_size"`
}
func (s *ImageSize) BeforeCreate(tx *gorm.DB) error {
if s.Width <= 0 || s.Height <= 0 {
return fmt.Errorf("image dimensions must be greater than zero")
}
if s.FileSize <= 0 {
return fmt.Errorf("file size must be greater than zero")
}
return nil
}
func (s *ImageSize) GetURL() string {
if config.S3.PublicURL == "" {
return ""
}
return fmt.Sprintf("%s/%s/%s/%s/%s", config.S3.PublicURL, config.S3.BucketName, config.S3.FolderPath, s.SizeType, s.Image.FileName)
}
func (s *ImageSize) GetAspectRatio() float64 {
if s.Height == 0 {
return 0
}
return float64(s.Width) / float64(s.Height)
}
func (s *ImageSize) GetDimensions() string {
return fmt.Sprintf("%dx%d", s.Width, s.Height)
}
func (s *ImageSize) GetFileSizeFormatted() string {
return format.FileSize(s.FileSize)
}
type Image struct {
gorm.Model
FileName string `gorm:"not null;size:255" json:"file_name"`
OriginalName string `gorm:"not null;size:255" json:"original_name"`
ContentType ImageContentType `gorm:"not null;size:100" json:"content_type"`
MD5Hash string `gorm:"not null;size:32" json:"md5_hash"`
Title string `gorm:"default:'';size:255" json:"title"`
Description string `gorm:"default:'';type:text" json:"description"`
SourceURL string `gorm:"default:'';size:500" json:"source_url"`
Rating Rating `gorm:"not null;default:'safe';size:10" json:"rating"`
IsApproved bool `gorm:"not null;default:true" json:"is_approved"`
IsDeleted bool `gorm:"not null;default:false" json:"is_deleted"`
ThreadLocked bool `gorm:"not null;default:false" json:"thread_locked"`
UploaderID uint `gorm:"not null;index" json:"-"`
Uploader User `gorm:"foreignKey:UploaderID" json:"uploader"`
ApproverID *uint `gorm:"index" json:"-"`
Approver *User `gorm:"foreignKey:ApproverID" json:"approver,omitempty"`
RelatedImages []Image `gorm:"many2many:image_relationships;joinForeignKey:image_id;joinReferences:related_image_id" json:"related_images,omitempty"`
ViewCount int64 `gorm:"not null;default:0" json:"view_count"`
FavouriteCount int64 `gorm:"not null;default:0" json:"favorite_count"`
CommentCount int64 `gorm:"not null;default:0" json:"comment_count"`
Sizes []ImageSize `gorm:"foreignKey:ImageID" json:"sizes,omitempty"`
Tags []Tag `gorm:"many2many:image_tags" json:"tags,omitempty"`
FavoritedBy []User `gorm:"many2many:user_favorites" json:"favorited_by,omitempty"`
Comments []Comment `gorm:"foreignKey:ImageID" json:"comments,omitempty"`
}
func (i *Image) BeforeCreate(tx *gorm.DB) error {
i.FileName = strings.TrimSpace(i.FileName)
i.OriginalName = strings.TrimSpace(i.OriginalName)
i.Title = strings.TrimSpace(i.Title)
i.Description = strings.TrimSpace(i.Description)
if i.FileName == "" {
return fmt.Errorf("file name cannot be empty")
}
if len(i.MD5Hash) != 32 {
return fmt.Errorf("MD5 hash must be exactly 32 characters long")
}
return nil
}
func (i *Image) BeforeDelete(tx *gorm.DB) error {
return tx.Exec(`UPDATE tags SET count = count - 1 WHERE id IN (
SELECT tag_id FROM image_tags WHERE image_id = ?
) AND count > 0`, i.ID).Error
}
func (i *Image) GetURL(sizeType ImageSizeType) string {
for _, size := range i.Sizes {
if size.SizeType == sizeType {
return size.GetURL()
}
}
return ""
}
func (i *Image) GetSize(sizeType ImageSizeType) *ImageSize {
for _, size := range i.Sizes {
if size.SizeType == sizeType {
return &size
}
}
return nil
}
func (i *Image) GetOriginalDimensions() string {
if fullSize := i.GetSize(ImageSizeTypeOriginal); fullSize != nil {
return fullSize.GetDimensions()
}
return "Unknown"
}
func (i *Image) GetAspectRatio() string {
if fullSize := i.GetSize(ImageSizeTypeOriginal); fullSize != nil {
if fullSize.Height == 0 {
return "Unknown"
}
width := fullSize.Width
height := fullSize.Height
divisor := math.GCD(width, height)
simplifiedWidth := width / divisor
simplifiedHeight := height / divisor
return fmt.Sprintf("%d:%d", simplifiedWidth, simplifiedHeight)
}
return "Unknown"
}
func (i *Image) AddSize(tx *gorm.DB, sizeType ImageSizeType, width, height int, fileSize int64) (*ImageSize, error) {
if width <= 0 || height <= 0 {
return nil, fmt.Errorf("image dimensions must be greater than zero")
}
if fileSize <= 0 {
return nil, fmt.Errorf("file size must be greater than zero")
}
size := &ImageSize{
ImageID: i.ID,
SizeType: sizeType,
Width: width,
Height: height,
FileSize: fileSize,
}
if err := tx.Create(size).Error; err != nil {
return nil, fmt.Errorf("failed to create image size: %v", err)
}
i.Sizes = append(i.Sizes, *size)
return size, nil
}
func (i *Image) AddRelatedImage(tx *gorm.DB, relatedImage *Image) error {
if relatedImage.ID == 0 {
return fmt.Errorf("related image must be saved before adding relationship")
}
if relatedImage.IsDeleted {
return fmt.Errorf("cannot add deleted image as related image")
}
if i.ID == relatedImage.ID {
return fmt.Errorf("cannot relate an image to itself")
}
// If the relationship already exists, do nothing
var count int64
if err := tx.Table("image_relationships").Where("image_id = ? AND related_image_id = ?", i.ID, relatedImage.ID).Count(&count).Error; err != nil {
return fmt.Errorf("failed to check existing relationship: %v", err)
}
if count > 0 {
return nil
}
// Create bi-directional relationship
if err := tx.Model(&i).Association("RelatedImages").Append(relatedImage); err != nil {
return fmt.Errorf("failed to add related image: %v", err)
}
if err := tx.Model(&relatedImage).Association("RelatedImages").Append(i); err != nil {
return fmt.Errorf("failed to add related image: %v", err)
}
return nil
}
func (i *Image) RemoveRelatedImage(tx *gorm.DB, relatedImage *Image) error {
if relatedImage.ID == 0 {
return fmt.Errorf("related image must be saved before removing relationship")
}
if i.ID == relatedImage.ID {
return fmt.Errorf("cannot remove self from related images")
}
// Remove bi-directional relationship
if err := tx.Model(&i).Association("RelatedImages").Delete(relatedImage); err != nil {
return fmt.Errorf("failed to remove related image: %v", err)
}
if err := tx.Model(&relatedImage).Association("RelatedImages").Delete(i); err != nil {
return fmt.Errorf("failed to remove related image: %v", err)
}
return nil
}
func (i *Image) GetRelatedImages(tx *gorm.DB) ([]Image, error) {
var relatedImages []Image
if err := tx.Model(&i).Association("RelatedImages").Find(&relatedImages); err != nil {
return nil, fmt.Errorf("failed to get related images: %v", err)
}
return relatedImages, nil
}
func (i *Image) AddTag(tx *gorm.DB, tag *Tag) error {
if i.IsDeleted || tag.IsDeleted {
return fmt.Errorf("cannot add tag to deleted image or add deleted tag")
}
// Check if already associated
var count int64
if err := tx.Table("image_tags").Where("image_id = ? AND tag_id = ?", i.ID, tag.ID).Count(&count).Error; err != nil {
return err
}
if count > 0 {
return nil
}
// Add association
if err := tx.Model(i).Association("Tags").Append(tag); err != nil {
return err
}
// Update tag count
return tx.Model(tag).UpdateColumn("count", gorm.Expr("count + ?", 1)).Error
}
func (i *Image) RemoveTag(tx *gorm.DB, tag *Tag) error {
// Check if associated
var count int64
if err := tx.Table("image_tags").Where("image_id = ? AND tag_id = ?", i.ID, tag.ID).Count(&count).Error; err != nil {
return err
}
if count == 0 {
return nil // Not associated
}
// Remove association
if err := tx.Model(i).Association("Tags").Delete(tag); err != nil {
return err
}
// Update tag count
return tx.Model(tag).UpdateColumn("count", gorm.Expr("GREATEST(count - ?, 0)", 1)).Error
}
func (i *Image) GetTags(tx *gorm.DB) ([]Tag, error) {
var tags []Tag
if err := tx.Model(i).Association("Tags").Find(&tags); err != nil {
return nil, fmt.Errorf("failed to get image tags: %v", err)
}
return tags, nil
}
func (i *Image) DeleteImage(tx *gorm.DB) error {
if i.IsDeleted {
return fmt.Errorf("image is already deleted")
}
i.IsDeleted = true
return tx.Save(i).Error
}
|