-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
model_image.go
83 lines (70 loc) · 1.58 KB
/
model_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
package models
import (
"database/sql"
"sort"
"github.com/gofrs/uuid"
)
type Image struct {
ID uuid.UUID `db:"id" json:"id"`
RemoteURL sql.NullString `db:"url" json:"url"`
Checksum string `db:"checksum" json:"checksum"`
Width int64 `db:"width" json:"width"`
Height int64 `db:"height" json:"height"`
}
func (p Image) GetID() uuid.UUID {
return p.ID
}
type Images []*Image
func (p Images) Each(fn func(interface{})) {
for _, v := range p {
fn(v)
}
}
func (p Images) ToURLSlice() []string {
urls := make([]string, len(p))
for i, v := range p {
urls[i] = v.RemoteURL.String
}
return urls
}
func (p Images) OrderLandscape() {
sort.Slice(p, func(a, b int) bool {
if p[a].Height == 0 || p[b].Height == 0 {
return false
}
aspectA := p[a].Width / p[a].Height
aspectB := p[b].Width / p[b].Height
if aspectA > aspectB {
return true
} else if aspectA < aspectB {
return false
}
return p[a].Width > p[b].Width
})
}
func (p Images) OrderPortrait() {
sort.Slice(p, func(a, b int) bool {
if p[a].Width == 0 || p[b].Width == 0 {
return false
}
aspectA := p[a].Height / p[a].Width
aspectB := p[b].Height / p[b].Width
if aspectA > aspectB {
return true
} else if aspectA < aspectB {
return false
}
return p[a].Height > p[b].Height
})
}
func (p *Images) Add(o interface{}) {
*p = append(*p, o.(*Image))
}
func (p *Image) IsEditTarget() {
}
func (p *Image) CopyFromCreateInput(input ImageCreateInput) {
CopyFull(p, input)
}
func (p *Image) CopyFromUpdateInput(input ImageUpdateInput) {
CopyFull(p, input)
}