aboutsummaryrefslogtreecommitdiff
path: root/models/image.go
blob: 6dc8e32de91b2f94a5483489cb6e74a1a0f4a667 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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:"-"`
	SizeType config.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"`
	ContentType    config.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         config.Rating           `gorm:"not null;default:'safe';size:15" 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;joinForeignKey:image_id;joinReferences:tag_id;constraint:OnDelete:CASCADE" 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.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 config.ImageSizeType) string {
	for _, size := range i.Sizes {
		if size.SizeType == sizeType {
			return size.GetURL()
		}
	}

	return ""
}

func (i *Image) GetSize(sizeType config.ImageSizeType) *ImageSize {
	for _, size := range i.Sizes {
		if size.SizeType == sizeType {
			return &size
		}
	}
	return nil
}

func (i *Image) GetSizeByString(sizeType string) *ImageSize {
	for _, size := range i.Sizes {
		if string(size.SizeType) == sizeType {
			return &size
		}
	}
	return nil
}

func (i *Image) GetOriginalDimensions() string {
	if fullSize := i.GetSize(config.ImageSizeTypeOriginal); fullSize != nil {
		return fullSize.GetDimensions()
	}
	return "Unknown"
}

func (i *Image) GetOriginalSize() *ImageSize {
	return i.GetSize(config.ImageSizeTypeOriginal)
}

func (i *Image) GetSmallSize() *ImageSize {
	return i.GetSize(config.ImageSizeTypeSmall)
}

func (i *Image) GetMediumSize() *ImageSize {
	return i.GetSize(config.ImageSizeTypeMedium)
}

func (i *Image) GetLargeSize() *ImageSize {
	return i.GetSize(config.ImageSizeTypeLarge)
}

func (i *Image) GetThumbnailSize() *ImageSize {
	return i.GetSize(config.ImageSizeTypeThumbnail)
}

func (i *Image) GetAspectRatio() string {
	if fullSize := i.GetSize(config.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) 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
}

func (i *Image) ToggleFavourite(tx *gorm.DB, user *User) error {
	if i.IsDeleted {
		return fmt.Errorf("cannot favourite deleted image")
	}

	var count int64
	if err := tx.Table("user_favorites").Where("user_id = ? AND image_id = ?", user.ID, i.ID).Count(&count).Error; err != nil {
		return err
	}

	if count > 0 {
		if err := tx.Model(user).Association("FavoritedImages").Delete(i); err != nil {
			return err
		}
		return tx.Model(i).UpdateColumn("favourite_count", gorm.Expr("GREATEST(favourite_count - ?, 0)", 1)).Error
	} else {
		if err := tx.Model(user).Association("FavoritedImages").Append(i); err != nil {
			return err
		}
		return tx.Model(i).UpdateColumn("favourite_count", gorm.Expr("favourite_count + ?", 1)).Error
	}
}

func (i *Image) IsUserFavourited(tx *gorm.DB, user *User) bool {
	if user == nil {
		return false
	}
	var count int64
	tx.Table("user_favorites").Where("user_id = ? AND image_id = ?", user.ID, i.ID).Count(&count)
	return count > 0
}

// ImageTag represents the many-to-many relationship between images and tags
// with a unique constraint to prevent duplicate associations
type ImageTag struct {
	ImageID uint  `gorm:"not null;index;uniqueIndex:idx_image_tag_unique" json:"image_id"`
	TagID   uint  `gorm:"not null;index;uniqueIndex:idx_image_tag_unique" json:"tag_id"`
	Image   Image `gorm:"foreignKey:ImageID;constraint:OnDelete:CASCADE" json:"-"`
	Tag     Tag   `gorm:"foreignKey:TagID;constraint:OnDelete:CASCADE" json:"-"`
}

// TableName specifies the table name for the ImageTag model
func (ImageTag) TableName() string {
	return "image_tags"
}