summaryrefslogtreecommitdiffstats
path: root/model/file_info.go
diff options
context:
space:
mode:
authorhmhealey <harrisonmhealey@gmail.com>2015-12-16 16:48:34 -0500
committerhmhealey <harrisonmhealey@gmail.com>2015-12-18 12:24:52 -0500
commitd4a139c09afa5fc0bcdcd1276cf0d2121dbdd226 (patch)
tree65f77e593fbd64551fc64fc9f5eeeb821d8511dc /model/file_info.go
parent58358ddd7cd0152bf16a7326e1d595524fb51246 (diff)
downloadchat-d4a139c09afa5fc0bcdcd1276cf0d2121dbdd226.tar.gz
chat-d4a139c09afa5fc0bcdcd1276cf0d2121dbdd226.tar.bz2
chat-d4a139c09afa5fc0bcdcd1276cf0d2121dbdd226.zip
Added additional information to getFileInfo api call
Diffstat (limited to 'model/file_info.go')
-rw-r--r--model/file_info.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/model/file_info.go b/model/file_info.go
new file mode 100644
index 000000000..d96723f32
--- /dev/null
+++ b/model/file_info.go
@@ -0,0 +1,60 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "bytes"
+ "encoding/json"
+ "image/gif"
+ "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)
+ }
+}