2017-08-16 21:37:37 +00:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2020-08-09 22:29:54 +00:00
|
|
|
// See LICENSE.txt for license information.
|
2016-04-10 21:39:38 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-11-12 21:00:53 +00:00
|
|
|
"image"
|
2016-04-10 21:39:38 +00:00
|
|
|
"image/gif"
|
|
|
|
"io"
|
|
|
|
"mime"
|
2018-02-08 23:11:04 +00:00
|
|
|
"net/http"
|
2016-04-10 21:39:38 +00:00
|
|
|
"path/filepath"
|
2016-11-12 21:00:53 +00:00
|
|
|
"strings"
|
2016-04-10 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
const (
|
|
|
|
FILEINFO_SORT_BY_CREATED = "CreateAt"
|
|
|
|
FILEINFO_SORT_BY_SIZE = "Size"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetFileInfosOptions contains options for getting FileInfos
|
|
|
|
type GetFileInfosOptions struct {
|
|
|
|
// UserIds optionally limits the FileInfos to those created by the given users.
|
|
|
|
UserIds []string `json:"user_ids"`
|
|
|
|
// ChannelIds optionally limits the FileInfos to those created in the given channels.
|
|
|
|
ChannelIds []string `json:"channel_ids"`
|
|
|
|
// Since optionally limits FileInfos to those created at or after the given time, specified as Unix time in milliseconds.
|
|
|
|
Since int64 `json:"since"`
|
|
|
|
// IncludeDeleted if set includes deleted FileInfos.
|
|
|
|
IncludeDeleted bool `json:"include_deleted"`
|
|
|
|
// SortBy sorts the FileInfos by this field. The default is to sort by date created.
|
|
|
|
SortBy string `json:"sort_by"`
|
|
|
|
// SortDescending changes the sort direction to descending order when true.
|
|
|
|
SortDescending bool `json:"sort_descending"`
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:39:38 +00:00
|
|
|
type FileInfo struct {
|
2016-11-12 21:00:53 +00:00
|
|
|
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"`
|
2016-04-10 21:39:38 +00:00
|
|
|
Extension string `json:"extension"`
|
2016-11-12 21:00:53 +00:00
|
|
|
Size int64 `json:"size"`
|
2016-04-10 21:39:38 +00:00
|
|
|
MimeType string `json:"mime_type"`
|
2016-11-12 21:00:53 +00:00
|
|
|
Width int `json:"width,omitempty"`
|
|
|
|
Height int `json:"height,omitempty"`
|
|
|
|
HasPreviewImage bool `json:"has_preview_image,omitempty"`
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
func (fi *FileInfo) ToJson() string {
|
|
|
|
b, _ := json.Marshal(fi)
|
2018-11-18 17:55:05 +00:00
|
|
|
return string(b)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
2016-04-10 21:39:38 +00:00
|
|
|
|
2016-11-12 21:00:53 +00:00
|
|
|
func FileInfoFromJson(data io.Reader) *FileInfo {
|
|
|
|
decoder := json.NewDecoder(data)
|
2016-04-10 21:39:38 +00:00
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
var fi FileInfo
|
|
|
|
if err := decoder.Decode(&fi); err != nil {
|
2016-11-12 21:00:53 +00:00
|
|
|
return nil
|
|
|
|
} else {
|
2020-08-09 22:29:54 +00:00
|
|
|
return &fi
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 21:00:53 +00:00
|
|
|
func FileInfosToJson(infos []*FileInfo) string {
|
2018-11-18 17:55:05 +00:00
|
|
|
b, _ := json.Marshal(infos)
|
|
|
|
return string(b)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 21:00:53 +00:00
|
|
|
func FileInfosFromJson(data io.Reader) []*FileInfo {
|
2016-04-10 21:39:38 +00:00
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
|
2016-11-12 21:00:53 +00:00
|
|
|
var infos []*FileInfo
|
|
|
|
if err := decoder.Decode(&infos); err != nil {
|
2016-04-10 21:39:38 +00:00
|
|
|
return nil
|
|
|
|
} else {
|
2016-11-12 21:00:53 +00:00
|
|
|
return infos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
func (fi *FileInfo) PreSave() {
|
|
|
|
if fi.Id == "" {
|
|
|
|
fi.Id = NewId()
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if fi.CreateAt == 0 {
|
|
|
|
fi.CreateAt = GetMillis()
|
2018-02-08 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if fi.UpdateAt < fi.CreateAt {
|
|
|
|
fi.UpdateAt = fi.CreateAt
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
func (fi *FileInfo) IsValid() *AppError {
|
|
|
|
if !IsValidId(fi.Id) {
|
2018-02-08 23:11:04 +00:00
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.id.app_error", nil, "", http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if !IsValidId(fi.CreatorId) && fi.CreatorId != "nouser" {
|
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.user_id.app_error", nil, "id="+fi.Id, http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if len(fi.PostId) != 0 && !IsValidId(fi.PostId) {
|
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.post_id.app_error", nil, "id="+fi.Id, http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if fi.CreateAt == 0 {
|
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.create_at.app_error", nil, "id="+fi.Id, http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if fi.UpdateAt == 0 {
|
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.update_at.app_error", nil, "id="+fi.Id, http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
if fi.Path == "" {
|
|
|
|
return NewAppError("FileInfo.IsValid", "model.file_info.is_valid.path.app_error", nil, "id="+fi.Id, http.StatusBadRequest)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|
2016-11-12 21:00:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:29:54 +00:00
|
|
|
func (fi *FileInfo) IsImage() bool {
|
|
|
|
return strings.HasPrefix(fi.MimeType, "image")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInfo(name string) *FileInfo {
|
|
|
|
info := &FileInfo{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 21:40:00 +00:00
|
|
|
func GetInfoForBytes(name string, data io.ReadSeeker, size int) (*FileInfo, *AppError) {
|
2016-11-12 21:00:53 +00:00
|
|
|
info := &FileInfo{
|
|
|
|
Name: name,
|
2020-10-19 21:40:00 +00:00
|
|
|
Size: int64(size),
|
2016-11-12 21:00:53 +00:00
|
|
|
}
|
|
|
|
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
|
2020-10-19 21:40:00 +00:00
|
|
|
if config, _, err := image.DecodeConfig(data); err == nil {
|
2016-11-12 21:00:53 +00:00
|
|
|
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
|
2020-10-19 21:40:00 +00:00
|
|
|
data.Seek(0, io.SeekStart)
|
|
|
|
if gifConfig, err := gif.DecodeAll(data); err != nil {
|
2016-11-12 21:00:53 +00:00
|
|
|
// Still return the rest of the info even though it doesn't appear to be an actual gif
|
|
|
|
info.HasPreviewImage = true
|
2020-10-19 21:40:00 +00:00
|
|
|
return info, NewAppError("GetInfoForBytes", "model.file_info.get.gif.app_error", nil, err.Error(), http.StatusBadRequest)
|
2016-11-12 21:00:53 +00:00
|
|
|
} 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)
|
2016-04-10 21:39:38 +00:00
|
|
|
}
|