summaryrefslogtreecommitdiff
path: root/uploadedfile/uploadedfile.go
diff options
context:
space:
mode:
authorhorus_arch2015-04-19 22:09:52 +0200
committerhorus_arch2015-04-19 22:09:52 +0200
commit01e9a34952bd6ddd383680b0ca2312e476ad07a6 (patch)
tree00902575e5c271cc5d35ea65aa8795b8caeb97bc /uploadedfile/uploadedfile.go
downloadmandible-01e9a34952bd6ddd383680b0ca2312e476ad07a6.tar.gz
Initial commit.
Diffstat (limited to 'uploadedfile/uploadedfile.go')
-rw-r--r--uploadedfile/uploadedfile.go155
1 files changed, 155 insertions, 0 deletions
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"
+}