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
|
package uploadedfile
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"net/http"
"os"
)
type UploadedFile struct {
filename string
path string
mime string
hash string
thumbs []*ThumbFile
}
var supportedTypes = map[string]bool{
"image/jpeg": true,
"image/jpg": true,
"image/gif": true,
"image/png": true,
}
func NewUploadedFile(filename, path string, thumbs []*ThumbFile) (*UploadedFile, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
buff := make([]byte, 512) // http://golang.org/pkg/net/http/#DetectContentType
_, err = file.Read(buff)
if err != nil {
return nil, err
}
filetype := http.DetectContentType(buff)
if _, ok := supportedTypes[filetype]; !ok {
return nil, errors.New("Unsupported file type!")
}
return &UploadedFile{
filename,
path,
filetype,
"",
thumbs,
}, nil
}
func (this *UploadedFile) GetFilename() string {
return this.filename
}
func (this *UploadedFile) SetFilename(filename string) {
this.filename = filename
}
func (this *UploadedFile) GetHash() string {
return this.hash
}
func (this *UploadedFile) SetHash(hash string) {
this.hash = hash
}
func (this *UploadedFile) SetPath(path string) {
// TODO: find a better location for this
os.Remove(this.path)
this.path = path
}
func (this *UploadedFile) GetPath() string {
return this.path
}
func (this *UploadedFile) GetMime() string {
return this.mime
}
func (this *UploadedFile) SetMime(mime string) {
this.mime = mime
}
func (this *UploadedFile) GetThumbs() []*ThumbFile {
return this.thumbs
}
func (this *UploadedFile) FileSize() (int64, error) {
f, err := os.Open(this.path)
if err != nil {
return 0, err
}
stats, err := f.Stat()
if err != nil {
return 0, err
}
size := stats.Size()
return size, nil
}
func (this *UploadedFile) Clean() {
os.Remove(this.path)
for _, thumb := range this.thumbs {
os.Remove(thumb.path)
}
}
func (this *UploadedFile) Dimensions() (int, int, error) {
f, err := os.Open(this.path)
if err != nil {
return 0, 0, err
}
var cfg image.Config
switch true {
case this.IsGif():
cfg, err = gif.DecodeConfig(f)
case this.IsPng():
cfg, err = png.DecodeConfig(f)
case this.IsJpeg():
cfg, err = jpeg.DecodeConfig(f)
default:
return 0, 0, errors.New("Invalid mime type!")
}
if err != nil {
return 0, 0, err
}
return cfg.Width, cfg.Height, nil
}
func (this *UploadedFile) IsJpeg() bool {
return (this.GetMime() == "image/jpeg" || this.GetMime() == "image/jpg")
}
func (this *UploadedFile) IsPng() bool {
return this.GetMime() == "image/png"
}
func (this *UploadedFile) IsGif() bool {
return this.GetMime() == "image/gif"
}
|