This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstorage.go
179 lines (142 loc) · 2.97 KB
/
storage.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
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
package photoshare
import (
"code.google.com/p/graphics-go/graphics"
"errors"
"github.com/dchest/uniuri"
"github.com/disintegration/gift"
"github.com/juju/errgo"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"path"
)
const (
thumbnailHeight = 300
thumbnailWidth = 300
)
type readable interface {
io.Reader
io.Seeker
}
var allowedContentTypes = []string{
"image/png",
"image/jpeg",
"image/gif"}
func isAllowedContentType(contentType string) bool {
for _, value := range allowedContentTypes {
if contentType == value {
return true
}
}
return false
}
func generateRandomFilename(contentType string) string {
var ext string
switch contentType {
case "image/jpeg":
ext = ".jpg"
case "image/png":
ext = ".png"
case "image/gif":
ext = ".gif"
}
return uniuri.New() + ext
}
type fileStorage interface {
clean(string) error
store(readable, string, string) error
}
func newFileStorage(cfg *config) fileStorage {
return &defaultFileStorage{
cfg.UploadsDir,
cfg.ThumbnailsDir,
}
}
type defaultFileStorage struct {
uploadsDir, thumbnailsDir string
}
func (f *defaultFileStorage) clean(name string) error {
imagePath := path.Join(f.uploadsDir, name)
thumbnailPath := path.Join(f.thumbnailsDir, name)
if err := os.Remove(imagePath); err != nil {
return errgo.Mask(err)
}
if err := os.Remove(thumbnailPath); err != nil {
return errgo.Mask(err)
}
return nil
}
func (f *defaultFileStorage) store(src readable, filename, contentType string) error {
if err := os.MkdirAll(f.uploadsDir, 0777); err != nil && !os.IsExist(err) {
return errgo.Mask(err)
}
if err := os.MkdirAll(f.thumbnailsDir, 0777); err != nil && !os.IsExist(err) {
return errgo.Mask(err)
}
// make thumbnail
var (
img image.Image
err error
)
switch contentType {
case "image/png":
img, err = png.Decode(src)
break
case "image/jpeg":
img, err = jpeg.Decode(src)
break
case "image/jpg":
img, err = jpeg.Decode(src)
break
case "image/gif":
img, err = gif.Decode(src)
break
default:
return errors.New("invalid content type:" + contentType)
}
if err != nil {
return errgo.Mask(err)
}
thumb := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
graphics.Thumbnail(thumb, img)
dst, err := os.Create(path.Join(f.thumbnailsDir, filename))
if err != nil {
return errgo.Mask(err)
}
g := gift.New(gift.Contrast(-30))
g.Draw(thumb, thumb)
if err != nil {
return errgo.Mask(err)
}
defer dst.Close()
switch contentType {
case "image/png":
err = png.Encode(dst, thumb)
break
case "image/jpeg":
err = jpeg.Encode(dst, thumb, nil)
break
case "image/jpg":
err = jpeg.Encode(dst, thumb, nil)
break
case "image/gif":
err = gif.Encode(dst, thumb, nil)
}
if err != nil {
return errgo.Mask(err)
}
src.Seek(0, 0)
dst, err = os.Create(path.Join(f.uploadsDir, filename))
if err != nil {
return errgo.Mask(err)
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
return errgo.Mask(err)
}
return nil
}