forked from getfider/fider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
116 lines (97 loc) · 3.02 KB
/
upload.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
package validate
import (
"fmt"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/img"
)
// MaxDimensionSize is the max width/height of an image. If image is bigger than this, it'll be resized.
const MaxDimensionSize = 1500
// MultiImageUploadOpts arguments to validate mulitple image upload process
type MultiImageUploadOpts struct {
MaxUploads int
IsRequired bool
MinWidth int
MinHeight int
ExactRatio bool
MaxKilobytes int
}
// ImageUploadOpts arguments to validate given upload
type ImageUploadOpts struct {
IsRequired bool
MinWidth int
MinHeight int
ExactRatio bool
MaxKilobytes int
}
//MultiImageUpload validates multiple image uploads
func MultiImageUpload(currentAttachments []string, uploads []*models.ImageUpload, opts MultiImageUploadOpts) ([]string, error) {
if currentAttachments == nil {
currentAttachments = []string{}
}
totalCount := len(currentAttachments)
for _, upload := range uploads {
if upload.Remove {
for _, attachment := range currentAttachments {
if attachment == upload.BlobKey {
totalCount--
}
}
} else if upload.Upload != nil {
totalCount++
}
messages, err := ImageUpload(upload, ImageUploadOpts{
IsRequired: opts.IsRequired,
MinWidth: opts.MinWidth,
MinHeight: opts.MinHeight,
ExactRatio: opts.ExactRatio,
MaxKilobytes: opts.MaxKilobytes,
})
if err != nil {
return nil, err
}
if len(messages) > 0 {
return messages, nil
}
}
if totalCount > opts.MaxUploads {
return []string{fmt.Sprintf("A maximum of %d attachments are allowed per post.", opts.MaxUploads)}, nil
}
return []string{}, nil
}
//ImageUpload validates given image upload
func ImageUpload(upload *models.ImageUpload, opts ImageUploadOpts) ([]string, error) {
messages := []string{}
if opts.IsRequired {
if upload == nil || (upload.BlobKey == "" && upload.Upload == nil) || upload.Remove {
messages = append(messages, "An image is required.")
}
}
if upload != nil && upload.Upload != nil && len(upload.Upload.Content) > 0 {
logo, err := img.Parse(upload.Upload.Content)
if err != nil {
if err == img.ErrNotSupported {
messages = append(messages, "This file format not supported.")
} else {
return nil, err
}
} else {
if logo.Width < opts.MinWidth || logo.Height < opts.MinHeight {
messages = append(messages, fmt.Sprintf("The image must have minimum dimensions of %dx%d pixels.", opts.MinWidth, opts.MinHeight))
}
if opts.ExactRatio && logo.Width != logo.Height {
messages = append(messages, "The image must have an aspect ratio of 1:1.")
}
if logo.Size > (opts.MaxKilobytes * 1024) {
messages = append(messages, fmt.Sprintf("The image size must be smaller than %dKB.", opts.MaxKilobytes))
}
if logo.Height > MaxDimensionSize && logo.Width > MaxDimensionSize {
newImageBytes, err := img.Apply(upload.Upload.Content, img.Resize(MaxDimensionSize))
if err != nil {
return nil, err
}
upload.Upload.Content = newImageBytes
}
}
}
return messages, nil
}