-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-upload.go
139 lines (117 loc) · 4.06 KB
/
image-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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package fileupload
import (
"bytes"
"io"
"math"
"mime/multipart"
"os"
"gopkg.in/h2non/bimg.v1" // external dependencies
"github.com/google/uuid"
"github.com/pkg/errors"
)
/*
Uses the Bimg library to save a thumbnail of a large image
*/
func saveThumbnail(buffer []byte, path, name string, width, height int) error {
widthRatio := float64(width) / float64(height)
thumbHeight := 75
thumbWidth := int(math.Floor((float64(thumbHeight) * widthRatio) + 0.5)) // round the float
options := bimg.Options{Quality: 90, Type: bimg.JPEG, Width: thumbWidth, Height: thumbHeight}
newImage, err := bimg.NewImage(buffer).Process(options) // do image parsing
if err != nil {
return errors.Wrap(err, "saveThumbnail()")
}
err = bimg.Write(path+string(os.PathSeparator)+name, newImage) // save image to a file
if err != nil {
return errors.Wrap(err, "saveThumbnail()")
}
return nil
}
/*
Uses the Bimg library to save a copy of an image
Returns data about the image (filesize, width, height, name, ...)
*/
func saveImage(buffer []byte, oldName, newName, directory string) (*FileInfo, error) {
options := bimg.Options{Quality: 90, Type: bimg.JPEG}
img := bimg.NewImage(buffer)
imgSize, err := img.Size()
if err != nil {
return nil, errors.Wrap(err, "saveImage()")
}
newImage, err := img.Process(options) // do image parsing
if err != nil {
return nil, errors.Wrap(err, "saveImage()")
}
err = bimg.Write(directory+string(os.PathSeparator)+newName, newImage) // save image to a file
if err != nil {
return nil, errors.Wrap(err, "saveImage()")
}
var size int64
if fi, err := os.Stat(directory + string(os.PathSeparator) + newName); err != nil {
return nil, errors.Wrap(err, "saveImage()")
} else {
size = fi.Size()
}
return &FileInfo{Name: newName, OriginalName: oldName, Size: size, MimeType: "image/jpeg", IsImage: true, Directory: directory, Width: imgSize.Width, Height: imgSize.Height}, nil
}
func UploadImageWithThumbnail(header *multipart.FileHeader, imageDir string, thumbnailDir string) (*FileInfo, error) {
// check if the directories exist
if _, err := os.Stat(imageDir); os.IsNotExist(err) {
return nil, ErrDirectoryDoesNotExist
}
if _, err := os.Stat(thumbnailDir); os.IsNotExist(err) {
return nil, ErrDirectoryDoesNotExist
}
file, err := header.Open()
if err != nil {
return nil, errors.Wrap(err, "UploadImageWithThumbnail()")
}
defer file.Close()
// is it an image?
mimetype, err := getMimeType(file)
if err != nil {
return nil, errors.Wrap(err, "UploadImageWithThumbnail()")
}
if isFileImage(mimetype) == false {
return nil, ErrNotImageType
}
// copy file to a buffer
buffer := &bytes.Buffer{}
if _, err := io.Copy(buffer, file); err != nil {
return nil, errors.Wrap(err, "UploadImageWithThumbnail()")
}
// UUIDv4 is used to avoid name conflicts (filename already exists errors)
uuidStr := uuid.New().String() + ".jpg"
fi, err := saveImage(buffer.Bytes(), header.Filename, uuidStr, imageDir) // re-save the uploaded image
if err != nil {
return nil, errors.Wrap(err, "UploadImageWithThumbnail()")
}
if err := saveThumbnail(buffer.Bytes(), thumbnailDir, uuidStr, fi.Width, fi.Height); err != nil { // create a thumbnail
return nil, errors.Wrap(err, "UploadImageWithThumbnail()")
}
return fi, nil
}
/*
Upload and resave fullsize images and create thumbnails. Fullsize images and thumbnails are saved to two separate directories.
Return data about each file, useful for Javascript upload tools.
*/
func UploadAllImages(files map[string][]*multipart.FileHeader, imageDir, thumbnailDir string) ([]FileInfo, error) {
// check if the directories exist
if _, err := os.Stat(imageDir); os.IsNotExist(err) {
return nil, ErrDirectoryDoesNotExist
}
if _, err := os.Stat(thumbnailDir); os.IsNotExist(err) {
return nil, ErrDirectoryDoesNotExist
}
var slice []FileInfo
for _, fieldSlice := range files {
for _, header := range fieldSlice {
fi, err := UploadImageWithThumbnail(header, imageDir, thumbnailDir)
if err != nil {
return nil, errors.Wrap(err, "UploadAllImages()")
}
slice = append(slice, *fi)
}
}
return slice, nil
}