summaryrefslogtreecommitdiff
path: root/uploadedfile
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
downloadmandible-01e9a34952bd6ddd383680b0ca2312e476ad07a6.tar.gz
Initial commit.
Diffstat (limited to 'uploadedfile')
-rw-r--r--uploadedfile/thumbfile.go126
-rw-r--r--uploadedfile/uploadedfile.go155
2 files changed, 281 insertions, 0 deletions
diff --git a/uploadedfile/thumbfile.go b/uploadedfile/thumbfile.go
new file mode 100644
index 0000000..ab38f72
--- /dev/null
+++ b/uploadedfile/thumbfile.go
@@ -0,0 +1,126 @@
+package uploadedfile
+
+import (
+ "errors"
+ "fmt"
+ "os"
+
+ "mandible/imageprocessor/gm"
+)
+
+type ThumbFile struct {
+ name string
+ width int
+ height int
+ shape string
+ path string
+}
+
+func NewThumbFile(width, height int, name, shape, path string) *ThumbFile {
+ return &ThumbFile{
+ name,
+ width,
+ height,
+ shape,
+ path,
+ }
+}
+
+func (this *ThumbFile) GetName() string {
+ return this.name
+}
+
+func (this *ThumbFile) SetName(name string) {
+ this.name = name
+}
+
+func (this *ThumbFile) GetHeight() int {
+ return this.height
+}
+
+func (this *ThumbFile) SetHeight(h int) {
+ this.height = h
+}
+
+func (this *ThumbFile) GetWidth() int {
+ return this.width
+}
+
+func (this *ThumbFile) SetWidth(h int) {
+ this.width = h
+}
+
+func (this *ThumbFile) GetShape() string {
+ return this.shape
+}
+
+func (this *ThumbFile) SetShape(shape string) {
+ this.shape = shape
+}
+
+func (this *ThumbFile) GetPath() string {
+ return this.path
+}
+
+func (this *ThumbFile) SetPath(path string) error {
+ if _, err := os.Stat(path); os.IsNotExist(err) {
+ return errors.New(fmt.Sprintf("Error when creating thumbnail %s", this.GetName()))
+ }
+
+ this.path = path
+
+ return nil
+}
+
+func (this *ThumbFile) Process(original *UploadedFile) error {
+ switch this.shape {
+ case "circle":
+ return this.processCircle(original)
+ case "thumb":
+ return this.processThumb(original)
+ case "square":
+ return this.processSquare(original)
+
+ }
+
+ return errors.New("Invalid thumb shape " + this.shape)
+}
+
+func (this *ThumbFile) processSquare(original *UploadedFile) error {
+ filename, err := gm.SquareThumb(original.GetPath(), this.GetName(), this.GetWidth())
+ if err != nil {
+ return err
+ }
+
+ if err := this.SetPath(filename); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (this *ThumbFile) processCircle(original *UploadedFile) error {
+ filename, err := gm.CircleThumb(original.GetPath(), this.GetName(), this.GetWidth())
+ if err != nil {
+ return err
+ }
+
+ if err := this.SetPath(filename); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (this *ThumbFile) processThumb(original *UploadedFile) error {
+ filename, err := gm.Thumb(original.GetPath(), this.GetName(), this.GetWidth(), this.GetHeight())
+ if err != nil {
+ return err
+ }
+
+ if err := this.SetPath(filename); err != nil {
+ return err
+ }
+
+ return nil
+}
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"
+}