PLT-3105 Files table migration (#4068)
* Implemented initial changes for files table * Removed *_benchmark_test.go files * Re-implemented GetPublicFile and added support for old path * Localization for files table * Moved file system code into utils package * Finished server-side changes and added initial upgrade script * Added getPostFiles api * Re-add Extension and HasPreviewImage fields to FileInfo * Removed unused translation * Fixed merge conflicts left over after permissions changes * Forced FileInfo.extension to be lower case * Changed FileUploadResponse to contain the FileInfos instead of FileIds * Fixed permissions on getFile* calls * Fixed notifications for file uploads * Added initial version of client code for files changes * Permanently added FileIds field to Post object and removed Post.HasFiles * Updated PostStore.Update to be usable in more circumstances * Re-added Filenames field and switched file migration to be entirely lazy-loaded * Increased max listener count for FileStore * Removed unused fileInfoCache * Moved file system code back into api * Removed duplicate test case * Fixed unit test running on ports other than 8065 * Renamed HasPermissionToPostContext to HasPermissionToChannelByPostContext * Refactored handleImages to make it more easily understandable * Renamed getPostFiles to getFileInfosForPost * Re-added pre-FileIds posts to analytics * Changed files to be saved as their ids as opposed to id/filename.ext * Renamed FileInfo.UserId to FileInfo.CreatorId * Fixed detection of language in CodePreview * Fixed switching between threads in the RHS not loading new files * Add serverside protection against a rare bug where the client sends the same file twice for a single post * Refactored the important parts of uploadFile api call into a function that can be called without a web context
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a2deeed597
Коммит
8a0e649f98
119
model/client.go
119
model/client.go
@@ -124,6 +124,10 @@ func (c *Client) GetGeneralRoute() string {
|
||||
return "/general"
|
||||
}
|
||||
|
||||
func (c *Client) GetFileRoute(fileId string) string {
|
||||
return fmt.Sprintf("/files/%v", fileId)
|
||||
}
|
||||
|
||||
func (c *Client) DoPost(url, data, contentType string) (*http.Response, *AppError) {
|
||||
rq, _ := http.NewRequest("POST", c.Url+url, strings.NewReader(data))
|
||||
rq.Header.Set("Content-Type", contentType)
|
||||
@@ -1289,8 +1293,33 @@ func (c *Client) UploadProfileFile(data []byte, contentType string) (*Result, *A
|
||||
return c.uploadFile(c.ApiUrl+"/users/newimage", data, contentType)
|
||||
}
|
||||
|
||||
func (c *Client) UploadPostAttachment(data []byte, contentType string) (*Result, *AppError) {
|
||||
return c.uploadFile(c.ApiUrl+c.GetTeamRoute()+"/files/upload", data, contentType)
|
||||
func (c *Client) UploadPostAttachment(data []byte, channelId string, filename string) (*FileUploadResponse, *AppError) {
|
||||
c.clearExtraProperties()
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
if part, err := writer.CreateFormFile("files", filename); err != nil {
|
||||
return nil, NewLocAppError("UploadPostAttachment", "model.client.upload_post_attachment.file.app_error", nil, err.Error())
|
||||
} else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil {
|
||||
return nil, NewLocAppError("UploadPostAttachment", "model.client.upload_post_attachment.file.app_error", nil, err.Error())
|
||||
}
|
||||
|
||||
if part, err := writer.CreateFormField("channel_id"); err != nil {
|
||||
return nil, NewLocAppError("UploadPostAttachment", "model.client.upload_post_attachment.channel_id.app_error", nil, err.Error())
|
||||
} else if _, err = io.Copy(part, strings.NewReader(channelId)); err != nil {
|
||||
return nil, NewLocAppError("UploadPostAttachment", "model.client.upload_post_attachment.channel_id.app_error", nil, err.Error())
|
||||
}
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, NewLocAppError("UploadPostAttachment", "model.client.upload_post_attachment.writer.app_error", nil, err.Error())
|
||||
}
|
||||
|
||||
if result, err := c.uploadFile(c.ApiUrl+c.GetTeamRoute()+"/files/upload", body.Bytes(), writer.FormDataContentType()); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return result.Data.(*FileUploadResponse), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) uploadFile(url string, data []byte, contentType string) (*Result, *AppError) {
|
||||
@@ -1312,55 +1341,51 @@ func (c *Client) uploadFile(url string, data []byte, contentType string) (*Resul
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetFile(url string, isFullUrl bool) (*Result, *AppError) {
|
||||
var rq *http.Request
|
||||
if isFullUrl {
|
||||
rq, _ = http.NewRequest("GET", url, nil)
|
||||
func (c *Client) GetFile(fileId string) (io.ReadCloser, *AppError) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/get", "", ""); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
rq, _ = http.NewRequest("GET", c.ApiUrl+c.GetTeamRoute()+"/files/get"+url, nil)
|
||||
}
|
||||
|
||||
if len(c.AuthToken) > 0 {
|
||||
rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
|
||||
}
|
||||
|
||||
if rp, err := c.HttpClient.Do(rq); err != nil {
|
||||
return nil, NewLocAppError(url, "model.client.connecting.app_error", nil, err.Error())
|
||||
} else if rp.StatusCode >= 300 {
|
||||
return nil, AppErrorFromJson(rp.Body)
|
||||
} else {
|
||||
defer closeBody(rp)
|
||||
return &Result{rp.Header.Get(HEADER_REQUEST_ID),
|
||||
rp.Header.Get(HEADER_ETAG_SERVER), rp.Body}, nil
|
||||
c.fillInExtraProperties(r)
|
||||
return r.Body, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetFileInfo(url string) (*Result, *AppError) {
|
||||
var rq *http.Request
|
||||
rq, _ = http.NewRequest("GET", c.ApiUrl+c.GetTeamRoute()+"/files/get_info"+url, nil)
|
||||
|
||||
if len(c.AuthToken) > 0 {
|
||||
rq.Header.Set(HEADER_AUTH, "BEARER "+c.AuthToken)
|
||||
}
|
||||
|
||||
if rp, err := c.HttpClient.Do(rq); err != nil {
|
||||
return nil, NewLocAppError(url, "model.client.connecting.app_error", nil, err.Error())
|
||||
} else if rp.StatusCode >= 300 {
|
||||
return nil, AppErrorFromJson(rp.Body)
|
||||
func (c *Client) GetFileThumbnail(fileId string) (io.ReadCloser, *AppError) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/get_thumbnail", "", ""); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer closeBody(rp)
|
||||
return &Result{rp.Header.Get(HEADER_REQUEST_ID),
|
||||
rp.Header.Get(HEADER_ETAG_SERVER), FileInfoFromJson(rp.Body)}, nil
|
||||
c.fillInExtraProperties(r)
|
||||
return r.Body, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetPublicLink(filename string) (*Result, *AppError) {
|
||||
if r, err := c.DoApiPost(c.GetTeamRoute()+"/files/get_public_link", MapToJson(map[string]string{"filename": filename})); err != nil {
|
||||
func (c *Client) GetFilePreview(fileId string) (io.ReadCloser, *AppError) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/get_preview", "", ""); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return &Result{r.Header.Get(HEADER_REQUEST_ID),
|
||||
r.Header.Get(HEADER_ETAG_SERVER), StringFromJson(r.Body)}, nil
|
||||
c.fillInExtraProperties(r)
|
||||
return r.Body, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetFileInfo(fileId string) (*FileInfo, *AppError) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/get_info", "", ""); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
c.fillInExtraProperties(r)
|
||||
return FileInfoFromJson(r.Body), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetPublicLink(fileId string) (string, *AppError) {
|
||||
if r, err := c.DoApiGet(c.GetFileRoute(fileId)+"/get_public_link", "", ""); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
c.fillInExtraProperties(r)
|
||||
return StringFromJson(r.Body), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1930,3 +1955,17 @@ func (c *Client) GetWebrtcToken() (map[string]string, *AppError) {
|
||||
return MapFromJson(r.Body), nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileInfosForPost returns a list of FileInfo objects for a given post id, if successful.
|
||||
// Otherwise, it returns an error.
|
||||
func (c *Client) GetFileInfosForPost(channelId string, postId string, etag string) ([]*FileInfo, *AppError) {
|
||||
c.clearExtraProperties()
|
||||
|
||||
if r, err := c.DoApiGet(c.GetChannelRoute(channelId)+fmt.Sprintf("/posts/%v/get_file_infos", postId), "", etag); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
c.fillInExtraProperties(r)
|
||||
return FileInfosFromJson(r.Body), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ type CompliancePost struct {
|
||||
PostType string
|
||||
PostProps string
|
||||
PostHashtags string
|
||||
PostFilenames string
|
||||
PostFileIds string
|
||||
}
|
||||
|
||||
func CompliancePostHeader() []string {
|
||||
@@ -60,7 +60,7 @@ func CompliancePostHeader() []string {
|
||||
"PostType",
|
||||
"PostProps",
|
||||
"PostHashtags",
|
||||
"PostFilenames",
|
||||
"PostFileIds",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,6 @@ func (me *CompliancePost) Row() []string {
|
||||
me.PostType,
|
||||
me.PostProps,
|
||||
me.PostHashtags,
|
||||
me.PostFilenames,
|
||||
me.PostFileIds,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestCompliancePostHeader(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCompliancePost(t *testing.T) {
|
||||
o := CompliancePost{TeamName: "test", PostFilenames: "files", PostCreateAt: GetMillis()}
|
||||
o := CompliancePost{TeamName: "test", PostFileIds: "files", PostCreateAt: GetMillis()}
|
||||
r := o.Row()
|
||||
|
||||
if r[0] != "test" {
|
||||
|
||||
@@ -14,8 +14,8 @@ var (
|
||||
)
|
||||
|
||||
type FileUploadResponse struct {
|
||||
Filenames []string `json:"filenames"`
|
||||
ClientIds []string `json:"client_ids"`
|
||||
FileInfos []*FileInfo `json:"file_infos"`
|
||||
ClientIds []string `json:"client_ids"`
|
||||
}
|
||||
|
||||
func FileUploadResponseFromJson(data io.Reader) *FileUploadResponse {
|
||||
|
||||
@@ -6,54 +6,31 @@ package model
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/gif"
|
||||
"io"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
Filename string `json:"filename"`
|
||||
Size int `json:"size"`
|
||||
Id string `json:"id"`
|
||||
CreatorId string `json:"user_id"`
|
||||
PostId string `json:"post_id,omitempty"`
|
||||
CreateAt int64 `json:"create_at"`
|
||||
UpdateAt int64 `json:"update_at"`
|
||||
DeleteAt int64 `json:"delete_at"`
|
||||
Path string `json:"-"` // not sent back to the client
|
||||
ThumbnailPath string `json:"-"` // not sent back to the client
|
||||
PreviewPath string `json:"-"` // not sent back to the client
|
||||
Name string `json:"name"`
|
||||
Extension string `json:"extension"`
|
||||
Size int64 `json:"size"`
|
||||
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)
|
||||
}
|
||||
|
||||
if extension != "" && extension[0] == '.' {
|
||||
// the client expects a file extension without the leading period
|
||||
extension = extension[1:]
|
||||
}
|
||||
|
||||
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, NewLocAppError("GetInfoForBytes", "model.file_info.get.gif.app_error", nil, "filename="+filename)
|
||||
} else {
|
||||
hasPreviewImage = len(gifImage.Image) == 1
|
||||
}
|
||||
}
|
||||
|
||||
return &FileInfo{
|
||||
Filename: filename,
|
||||
Size: size,
|
||||
Extension: extension,
|
||||
MimeType: mimeType,
|
||||
HasPreviewImage: hasPreviewImage,
|
||||
}, nil
|
||||
Width int `json:"width,omitempty"`
|
||||
Height int `json:"height,omitempty"`
|
||||
HasPreviewImage bool `json:"has_preview_image,omitempty"`
|
||||
}
|
||||
|
||||
func (info *FileInfo) ToJson() string {
|
||||
@@ -75,3 +52,123 @@ func FileInfoFromJson(data io.Reader) *FileInfo {
|
||||
return &info
|
||||
}
|
||||
}
|
||||
|
||||
func FileInfosToJson(infos []*FileInfo) string {
|
||||
b, err := json.Marshal(infos)
|
||||
if err != nil {
|
||||
return ""
|
||||
} else {
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
func FileInfosFromJson(data io.Reader) []*FileInfo {
|
||||
decoder := json.NewDecoder(data)
|
||||
|
||||
var infos []*FileInfo
|
||||
if err := decoder.Decode(&infos); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
return infos
|
||||
}
|
||||
}
|
||||
|
||||
func (o *FileInfo) PreSave() {
|
||||
if o.Id == "" {
|
||||
o.Id = NewId()
|
||||
}
|
||||
|
||||
if o.CreateAt == 0 {
|
||||
o.CreateAt = GetMillis()
|
||||
o.UpdateAt = o.CreateAt
|
||||
}
|
||||
}
|
||||
|
||||
func (o *FileInfo) IsValid() *AppError {
|
||||
if len(o.Id) != 26 {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.id.app_error", nil, "")
|
||||
}
|
||||
|
||||
if len(o.CreatorId) != 26 {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.user_id.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if len(o.PostId) != 0 && len(o.PostId) != 26 {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.post_id.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if o.CreateAt == 0 {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.create_at.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if o.UpdateAt == 0 {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.update_at.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if o.Path == "" {
|
||||
return NewLocAppError("FileInfo.IsValid", "model.file_info.is_valid.path.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *FileInfo) IsImage() bool {
|
||||
return strings.HasPrefix(o.MimeType, "image")
|
||||
}
|
||||
|
||||
func GetInfoForBytes(name string, data []byte) (*FileInfo, *AppError) {
|
||||
info := &FileInfo{
|
||||
Name: name,
|
||||
Size: int64(len(data)),
|
||||
}
|
||||
var err *AppError
|
||||
|
||||
extension := strings.ToLower(filepath.Ext(name))
|
||||
info.MimeType = mime.TypeByExtension(extension)
|
||||
|
||||
if extension != "" && extension[0] == '.' {
|
||||
// The client expects a file extension without the leading period
|
||||
info.Extension = extension[1:]
|
||||
} else {
|
||||
info.Extension = extension
|
||||
}
|
||||
|
||||
if info.IsImage() {
|
||||
// Only set the width and height if it's actually an image that we can understand
|
||||
if config, _, err := image.DecodeConfig(bytes.NewReader(data)); err == nil {
|
||||
info.Width = config.Width
|
||||
info.Height = config.Height
|
||||
|
||||
if info.MimeType == "image/gif" {
|
||||
// Just show the gif itself instead of a preview image for animated gifs
|
||||
if gifConfig, err := gif.DecodeAll(bytes.NewReader(data)); err != nil {
|
||||
// Still return the rest of the info even though it doesn't appear to be an actual gif
|
||||
info.HasPreviewImage = true
|
||||
err = NewLocAppError("GetInfoForBytes", "model.file_info.get.gif.app_error", nil, "name="+name)
|
||||
} else {
|
||||
info.HasPreviewImage = len(gifConfig.Image) == 1
|
||||
}
|
||||
} else {
|
||||
info.HasPreviewImage = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return info, err
|
||||
}
|
||||
|
||||
func GetEtagForFileInfos(infos []*FileInfo) string {
|
||||
if len(infos) == 0 {
|
||||
return Etag()
|
||||
}
|
||||
|
||||
var maxUpdateAt int64
|
||||
|
||||
for _, info := range infos {
|
||||
if info.UpdateAt > maxUpdateAt {
|
||||
maxUpdateAt = info.UpdateAt
|
||||
}
|
||||
}
|
||||
|
||||
return Etag(infos[0].PostId, maxUpdateAt)
|
||||
}
|
||||
|
||||
@@ -5,56 +5,137 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
_ "image/gif"
|
||||
_ "image/png"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetInfoForBytes(t *testing.T) {
|
||||
func TestFileInfoIsValid(t *testing.T) {
|
||||
info := &FileInfo{
|
||||
Id: NewId(),
|
||||
CreatorId: NewId(),
|
||||
CreateAt: 1234,
|
||||
UpdateAt: 1234,
|
||||
PostId: "",
|
||||
Path: "fake/path.png",
|
||||
}
|
||||
|
||||
if err := info.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
info.Id = ""
|
||||
if err := info.IsValid(); err == nil {
|
||||
t.Fatal("empty Id isn't valid")
|
||||
}
|
||||
|
||||
info.Id = NewId()
|
||||
info.CreateAt = 0
|
||||
if err := info.IsValid(); err == nil {
|
||||
t.Fatal("empty CreateAt isn't valid")
|
||||
}
|
||||
|
||||
info.CreateAt = 1234
|
||||
info.UpdateAt = 0
|
||||
if err := info.IsValid(); err == nil {
|
||||
t.Fatal("empty UpdateAt isn't valid")
|
||||
}
|
||||
|
||||
info.UpdateAt = 1234
|
||||
info.PostId = NewId()
|
||||
if err := info.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
info.Path = ""
|
||||
if err := info.IsValid(); err == nil {
|
||||
t.Fatal("empty Path isn't valid")
|
||||
}
|
||||
|
||||
info.Path = "fake/path.png"
|
||||
if err := info.IsValid(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileInfoIsImage(t *testing.T) {
|
||||
info := &FileInfo{
|
||||
MimeType: "image/png",
|
||||
}
|
||||
|
||||
if !info.IsImage() {
|
||||
t.Fatal("file is an image")
|
||||
}
|
||||
|
||||
info.MimeType = "text/plain"
|
||||
if info.IsImage() {
|
||||
t.Fatal("file is not an image")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetInfoForFile(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.Name != "file.txt" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "txt" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.Size != 1000 {
|
||||
t.Fatalf("Got incorrect size: %v", info.Size)
|
||||
} else if info.Extension != "txt" {
|
||||
t.Fatalf("Got incorrect file extension: %v", info.Extension)
|
||||
} else if !strings.HasPrefix(info.MimeType, "text/plain") {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
} else if info.Width != 0 {
|
||||
t.Fatalf("Got incorrect width: %v", info.Width)
|
||||
} else if info.Height != 0 {
|
||||
t.Fatalf("Got incorrect height: %v", info.Height)
|
||||
} else if info.HasPreviewImage {
|
||||
t.Fatalf("Got HasPreviewImage = true for non-image file")
|
||||
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
|
||||
}
|
||||
|
||||
if info, err := GetInfoForBytes("file.png", fakeFile); err != nil {
|
||||
pngFile, err := ioutil.ReadFile("../tests/test.png")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load test.png: %v", err.Error())
|
||||
}
|
||||
if info, err := GetInfoForBytes("test.png", pngFile); 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.Name != "test.png" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "png" {
|
||||
t.Fatalf("Got incorrect file extension: %v", info.Extension)
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.Size != 279591 {
|
||||
t.Fatalf("Got incorrect size: %v", info.Size)
|
||||
} else if info.MimeType != "image/png" {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
} else if info.Width != 408 {
|
||||
t.Fatalf("Got incorrect width: %v", info.Width)
|
||||
} else if info.Height != 336 {
|
||||
t.Fatalf("Got incorrect height: %v", info.Height)
|
||||
} else if !info.HasPreviewImage {
|
||||
t.Fatalf("Got HasPreviewImage = false for image")
|
||||
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
|
||||
}
|
||||
|
||||
// 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.Name != "handtinywhite.gif" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "gif" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.Size != 35 {
|
||||
t.Fatalf("Got incorrect size: %v", info.Size)
|
||||
} else if info.Extension != "gif" {
|
||||
t.Fatalf("Got incorrect file extension: %v", info.Extension)
|
||||
} else if info.MimeType != "image/gif" {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
} else if info.Width != 1 {
|
||||
t.Fatalf("Got incorrect width: %v", info.Width)
|
||||
} else if info.Height != 1 {
|
||||
t.Fatalf("Got incorrect height: %v", info.Height)
|
||||
} else if !info.HasPreviewImage {
|
||||
t.Fatalf("Got HasPreviewImage = false for static gif")
|
||||
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
|
||||
}
|
||||
|
||||
animatedGifFile, err := ioutil.ReadFile("../tests/testgif.gif")
|
||||
@@ -63,29 +144,57 @@ func TestGetInfoForBytes(t *testing.T) {
|
||||
}
|
||||
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.Name != "testgif.gif" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "gif" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.Size != 38689 {
|
||||
t.Fatalf("Got incorrect size: %v", info.Size)
|
||||
} else if info.Extension != "gif" {
|
||||
t.Fatalf("Got incorrect file extension: %v", info.Extension)
|
||||
} else if info.MimeType != "image/gif" {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
} else if info.Width != 118 {
|
||||
t.Fatalf("Got incorrect width: %v", info.Width)
|
||||
} else if info.Height != 118 {
|
||||
t.Fatalf("Got incorrect height: %v", info.Height)
|
||||
} else if info.HasPreviewImage {
|
||||
t.Fatalf("Got HasPreviewImage = true for animated gif")
|
||||
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
|
||||
}
|
||||
|
||||
if info, err := GetInfoForBytes("filewithoutextension", fakeFile); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if info.Filename != "filewithoutextension" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Filename)
|
||||
} else if info.Name != "filewithoutextension" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.Size != 1000 {
|
||||
t.Fatalf("Got incorrect size: %v", info.Size)
|
||||
} else if info.Extension != "" {
|
||||
t.Fatalf("Got incorrect file extension: %v", info.Extension)
|
||||
} else if info.MimeType != "" {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
} else if info.Width != 0 {
|
||||
t.Fatalf("Got incorrect width: %v", info.Width)
|
||||
} else if info.Height != 0 {
|
||||
t.Fatalf("Got incorrect height: %v", info.Height)
|
||||
} else if info.HasPreviewImage {
|
||||
t.Fatalf("Got HasPreviewImage = true for non-image file")
|
||||
t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage)
|
||||
}
|
||||
|
||||
// Always make the extension lower case to make it easier to use in other places
|
||||
if info, err := GetInfoForBytes("file.TXT", fakeFile); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if info.Name != "file.TXT" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "txt" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
}
|
||||
|
||||
// Don't error out for image formats we don't support
|
||||
if info, err := GetInfoForBytes("file.tif", fakeFile); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if info.Name != "file.tif" {
|
||||
t.Fatalf("Got incorrect filename: %v", info.Name)
|
||||
} else if info.Extension != "tif" {
|
||||
t.Fatalf("Got incorrect extension: %v", info.Extension)
|
||||
} else if info.MimeType != "image/tiff" && info.MimeType != "image/x-tiff" {
|
||||
t.Fatalf("Got incorrect mime type: %v", info.MimeType)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ type Post struct {
|
||||
Type string `json:"type"`
|
||||
Props StringInterface `json:"props"`
|
||||
Hashtags string `json:"hashtags"`
|
||||
Filenames StringArray `json:"filenames"`
|
||||
Filenames StringArray `json:"filenames,omitempty"` // Deprecated, do not use this field any more
|
||||
FileIds StringArray `json:"file_ids,omitempty"`
|
||||
PendingPostId string `json:"pending_post_id" db:"-"`
|
||||
}
|
||||
|
||||
@@ -118,6 +119,10 @@ func (o *Post) IsValid() *AppError {
|
||||
return NewLocAppError("Post.IsValid", "model.post.is_valid.filenames.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(ArrayToJson(o.FileIds)) > 150 {
|
||||
return NewLocAppError("Post.IsValid", "model.post.is_valid.file_ids.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(StringInterfaceToJson(o.Props)) > 8000 {
|
||||
return NewLocAppError("Post.IsValid", "model.post.is_valid.props.app_error", nil, "id="+o.Id)
|
||||
}
|
||||
@@ -145,15 +150,16 @@ func (o *Post) PreSave() {
|
||||
if o.Filenames == nil {
|
||||
o.Filenames = []string{}
|
||||
}
|
||||
|
||||
if o.FileIds == nil {
|
||||
o.FileIds = []string{}
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Post) MakeNonNil() {
|
||||
if o.Props == nil {
|
||||
o.Props = make(map[string]interface{})
|
||||
}
|
||||
if o.Filenames == nil {
|
||||
o.Filenames = []string{}
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Post) AddProp(key string, value interface{}) {
|
||||
|
||||
Ссылка в новой задаче
Block a user