From 01e9a34952bd6ddd383680b0ca2312e476ad07a6 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Sun, 19 Apr 2015 22:09:52 +0200 Subject: Initial commit. --- uploadedfile/uploadedfile.go | 155 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 uploadedfile/uploadedfile.go (limited to 'uploadedfile/uploadedfile.go') diff --git a/uploadedfile/uploadedfile.go b/uploadedfile/uploadedfile.go new file mode 100644 index 0000000..a3c8345 --- /dev/null +++ b/uploadedfile/uploadedfile.go @@ -0,0 +1,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" +} -- cgit v1.2.3