mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Added additional information to getFileInfo api call
This commit is contained in:
60
model/file_info.go
Normal file
60
model/file_info.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
76
model/file_info_test.go
Normal file
76
model/file_info_test.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user