summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
Diffstat (limited to 'model')
-rw-r--r--model/client.go2
-rw-r--r--model/config.go44
-rw-r--r--model/file_info.go72
-rw-r--r--model/file_info_test.go76
4 files changed, 178 insertions, 16 deletions
diff --git a/model/client.go b/model/client.go
index d3f76817d..b9b97dedc 100644
--- a/model/client.go
+++ b/model/client.go
@@ -717,7 +717,7 @@ func (c *Client) GetFileInfo(url string) (*Result, *AppError) {
return nil, AppErrorFromJson(rp.Body)
} else {
return &Result{rp.Header.Get(HEADER_REQUEST_ID),
- rp.Header.Get(HEADER_ETAG_SERVER), MapFromJson(rp.Body)}, nil
+ rp.Header.Get(HEADER_ETAG_SERVER), FileInfoFromJson(rp.Body)}, nil
}
}
diff --git a/model/config.go b/model/config.go
index 38ef81a85..a4792ff9e 100644
--- a/model/config.go
+++ b/model/config.go
@@ -68,21 +68,25 @@ type LogSettings struct {
}
type FileSettings struct {
- DriverName string
- Directory string
- EnablePublicLink bool
- PublicLinkSalt string
- ThumbnailWidth int
- ThumbnailHeight int
- PreviewWidth int
- PreviewHeight int
- ProfileWidth int
- ProfileHeight int
- InitialFont string
- AmazonS3AccessKeyId string
- AmazonS3SecretAccessKey string
- AmazonS3Bucket string
- AmazonS3Region string
+ DriverName string
+ Directory string
+ EnablePublicLink bool
+ PublicLinkSalt string
+ ThumbnailWidth int
+ ThumbnailHeight int
+ PreviewWidth int
+ PreviewHeight int
+ ProfileWidth int
+ ProfileHeight int
+ InitialFont string
+ AmazonS3AccessKeyId string
+ AmazonS3SecretAccessKey string
+ AmazonS3Bucket string
+ AmazonS3Region string
+ AmazonS3Endpoint string
+ AmazonS3BucketEndpoint string
+ AmazonS3LocationConstraint *bool
+ AmazonS3LowercaseBucket *bool
}
type EmailSettings struct {
@@ -210,6 +214,16 @@ func (o *Config) SetDefaults() {
o.FileSettings.PublicLinkSalt = NewRandomString(32)
}
+ if o.FileSettings.AmazonS3LocationConstraint == nil {
+ o.FileSettings.AmazonS3LocationConstraint = new(bool)
+ *o.FileSettings.AmazonS3LocationConstraint = false
+ }
+
+ if o.FileSettings.AmazonS3LowercaseBucket == nil {
+ o.FileSettings.AmazonS3LowercaseBucket = new(bool)
+ *o.FileSettings.AmazonS3LowercaseBucket = false
+ }
+
if len(o.EmailSettings.InviteSalt) == 0 {
o.EmailSettings.InviteSalt = NewRandomString(32)
}
diff --git a/model/file_info.go b/model/file_info.go
new file mode 100644
index 000000000..741b4e55d
--- /dev/null
+++ b/model/file_info.go
@@ -0,0 +1,72 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "bytes"
+ "encoding/json"
+ "image/gif"
+ "io"
+ "mime"
+ "path/filepath"
+)
+
+type FileInfo struct {
+ Filename string `json:"filename"`
+ Size int `json:"size"`
+ Extension string `json:"extension"`
+ MimeType string `json:"mime_type"`
+ HasPreviewImage bool `json:"has_preview_image"`
+}
+
+func GetInfoForBytes(filename string, data []byte) (*FileInfo, *AppError) {
+ size := len(data)
+
+ var mimeType string
+ extension := filepath.Ext(filename)
+ isImage := IsFileExtImage(extension)
+ if isImage {
+ mimeType = GetImageMimeType(extension)
+ } else {
+ mimeType = mime.TypeByExtension(extension)
+ }
+
+ hasPreviewImage := isImage
+ if mimeType == "image/gif" {
+ // just show the gif itself instead of a preview image for animated gifs
+ if gifImage, err := gif.DecodeAll(bytes.NewReader(data)); err != nil {
+ return nil, NewAppError("GetInfoForBytes", "Could not decode gif.", "filename="+filename)
+ } else {
+ hasPreviewImage = len(gifImage.Image) == 1
+ }
+ }
+
+ return &FileInfo{
+ Filename: filename,
+ Size: size,
+ Extension: extension[1:],
+ MimeType: mimeType,
+ HasPreviewImage: hasPreviewImage,
+ }, nil
+}
+
+func (info *FileInfo) ToJson() string {
+ b, err := json.Marshal(info)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func FileInfoFromJson(data io.Reader) *FileInfo {
+ decoder := json.NewDecoder(data)
+
+ var info FileInfo
+ if err := decoder.Decode(&info); err != nil {
+ return nil
+ } else {
+ return &info
+ }
+}
diff --git a/model/file_info_test.go b/model/file_info_test.go
new file mode 100644
index 000000000..ecf0d509c
--- /dev/null
+++ b/model/file_info_test.go
@@ -0,0 +1,76 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "encoding/base64"
+ "io/ioutil"
+ "testing"
+)
+
+func TestGetInfoForBytes(t *testing.T) {
+ fakeFile := make([]byte, 1000)
+
+ if info, err := GetInfoForBytes("file.txt", fakeFile); err != nil {
+ t.Fatal(err)
+ } else if info.Filename != "file.txt" {
+ t.Fatalf("Got incorrect filename: %v", info.Filename)
+ } else if info.Size != 1000 {
+ t.Fatalf("Got incorrect size: %v", info.Size)
+ } else if info.Extension != "txt" {
+ t.Fatalf("Git incorrect file extension: %v", info.Extension)
+ } else if info.MimeType != "text/plain; charset=utf-8" {
+ t.Fatalf("Got incorrect mime type: %v", info.MimeType)
+ } else if info.HasPreviewImage {
+ t.Fatalf("Got HasPreviewImage = true for non-image file")
+ }
+
+ if info, err := GetInfoForBytes("file.png", fakeFile); err != nil {
+ t.Fatal(err)
+ } else if info.Filename != "file.png" {
+ t.Fatalf("Got incorrect filename: %v", info.Filename)
+ } else if info.Size != 1000 {
+ t.Fatalf("Got incorrect size: %v", info.Size)
+ } else if info.Extension != "png" {
+ t.Fatalf("Git incorrect file extension: %v", info.Extension)
+ } else if info.MimeType != "image/png" {
+ t.Fatalf("Got incorrect mime type: %v", info.MimeType)
+ } else if !info.HasPreviewImage {
+ t.Fatalf("Got HasPreviewImage = false for image")
+ }
+
+ // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
+ gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=")
+ if info, err := GetInfoForBytes("handtinywhite.gif", gifFile); err != nil {
+ t.Fatal(err)
+ } else if info.Filename != "handtinywhite.gif" {
+ t.Fatalf("Got incorrect filename: %v", info.Filename)
+ } else if info.Size != 35 {
+ t.Fatalf("Got incorrect size: %v", info.Size)
+ } else if info.Extension != "gif" {
+ t.Fatalf("Git incorrect file extension: %v", info.Extension)
+ } else if info.MimeType != "image/gif" {
+ t.Fatalf("Got incorrect mime type: %v", info.MimeType)
+ } else if !info.HasPreviewImage {
+ t.Fatalf("Got HasPreviewImage = false for static gif")
+ }
+
+ animatedGifFile, err := ioutil.ReadFile("../web/static/images/testgif.gif")
+ if err != nil {
+ t.Fatalf("Failed to load testgif.gif: %v", err.Error())
+ }
+ if info, err := GetInfoForBytes("testgif.gif", animatedGifFile); err != nil {
+ t.Fatal(err)
+ } else if info.Filename != "testgif.gif" {
+ t.Fatalf("Got incorrect filename: %v", info.Filename)
+ } else if info.Size != 38689 {
+ t.Fatalf("Got incorrect size: %v", info.Size)
+ } else if info.Extension != "gif" {
+ t.Fatalf("Git incorrect file extension: %v", info.Extension)
+ } else if info.MimeType != "image/gif" {
+ t.Fatalf("Got incorrect mime type: %v", info.MimeType)
+ } else if info.HasPreviewImage {
+ t.Fatalf("Got HasPreviewImage = true for animated gif")
+ }
+}