2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2016-08-15 17:38:55 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/base64"
|
2015-06-14 23:53:32 -08:00
|
|
|
"fmt"
|
|
|
|
|
"image"
|
2015-09-24 08:33:15 -07:00
|
|
|
"image/color"
|
|
|
|
|
"image/draw"
|
2015-06-14 23:53:32 -08:00
|
|
|
_ "image/gif"
|
|
|
|
|
"image/jpeg"
|
|
|
|
|
"io"
|
2015-07-16 08:54:09 -04:00
|
|
|
"io/ioutil"
|
2015-06-14 23:53:32 -08:00
|
|
|
"net/http"
|
2016-09-30 11:06:30 -04:00
|
|
|
"net/url"
|
2015-07-16 08:54:09 -04:00
|
|
|
"os"
|
2015-06-14 23:53:32 -08:00
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2016-10-06 16:44:41 -04:00
|
|
|
"sync"
|
2015-06-14 23:53:32 -08:00
|
|
|
"time"
|
2016-05-18 22:34:31 +02:00
|
|
|
|
|
|
|
|
l4g "github.com/alecthomas/log4go"
|
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
|
"github.com/goamz/goamz/aws"
|
|
|
|
|
"github.com/goamz/goamz/s3"
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/mattermost/platform/model"
|
|
|
|
|
"github.com/mattermost/platform/utils"
|
|
|
|
|
"github.com/mssola/user_agent"
|
|
|
|
|
"github.com/rwcarlsen/goexif/exif"
|
|
|
|
|
_ "golang.org/x/image/bmp"
|
2015-06-14 23:53:32 -08:00
|
|
|
)
|
|
|
|
|
|
2015-09-18 18:00:09 -04:00
|
|
|
const (
|
|
|
|
|
/*
|
|
|
|
|
EXIF Image Orientations
|
|
|
|
|
1 2 3 4 5 6 7 8
|
|
|
|
|
|
|
|
|
|
888888 888888 88 88 8888888888 88 88 8888888888
|
|
|
|
|
88 88 88 88 88 88 88 88 88 88 88 88
|
|
|
|
|
8888 8888 8888 8888 88 8888888888 8888888888 88
|
|
|
|
|
88 88 88 88
|
|
|
|
|
88 88 888888 888888
|
|
|
|
|
*/
|
|
|
|
|
Upright = 1
|
|
|
|
|
UprightMirrored = 2
|
|
|
|
|
UpsideDown = 3
|
|
|
|
|
UpsideDownMirrored = 4
|
|
|
|
|
RotatedCWMirrored = 5
|
|
|
|
|
RotatedCCW = 6
|
|
|
|
|
RotatedCCWMirrored = 7
|
|
|
|
|
RotatedCW = 8
|
2015-10-26 14:38:46 -04:00
|
|
|
|
2016-03-07 14:37:19 -05:00
|
|
|
MaxImageSize = 6048 * 4032 // 24 megapixels, roughly 36MB as a raw image
|
2015-09-18 18:00:09 -04:00
|
|
|
)
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
func InitFile() {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.file.init.debug"))
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
BaseRoutes.TeamFiles.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST")
|
|
|
|
|
|
|
|
|
|
BaseRoutes.NeedFile.Handle("/get", ApiUserRequiredTrustRequester(getFile)).Methods("GET")
|
|
|
|
|
BaseRoutes.NeedFile.Handle("/get_thumbnail", ApiUserRequiredTrustRequester(getFileThumbnail)).Methods("GET")
|
|
|
|
|
BaseRoutes.NeedFile.Handle("/get_preview", ApiUserRequiredTrustRequester(getFilePreview)).Methods("GET")
|
|
|
|
|
BaseRoutes.NeedFile.Handle("/get_info", ApiUserRequired(getFileInfo)).Methods("GET")
|
|
|
|
|
BaseRoutes.NeedFile.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("GET")
|
2016-05-05 16:35:03 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
BaseRoutes.Public.Handle("/files/{file_id:[A-Za-z0-9]+}/get", ApiAppHandlerTrustRequesterIndependent(getPublicFile)).Methods("GET")
|
|
|
|
|
BaseRoutes.Public.Handle("/files/get/{team_id:[A-Za-z0-9]+}/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiAppHandlerTrustRequesterIndependent(getPublicFileOld)).Methods("GET")
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
2015-09-23 13:47:10 -07:00
|
|
|
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
2016-01-22 01:37:11 -03:00
|
|
|
c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.storage.app_error", nil, "")
|
2015-06-14 23:53:32 -08:00
|
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 15:07:42 +02:00
|
|
|
if r.ContentLength > *utils.Cfg.FileSettings.MaxFileSize {
|
2016-01-22 01:37:11 -03:00
|
|
|
c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.too_large.app_error", nil, "")
|
2015-12-14 16:04:10 -05:00
|
|
|
c.Err.StatusCode = http.StatusRequestEntityTooLarge
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize); err != nil {
|
2015-06-14 23:53:32 -08:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := r.MultipartForm
|
|
|
|
|
|
|
|
|
|
props := m.Value
|
|
|
|
|
if len(props["channel_id"]) == 0 {
|
|
|
|
|
c.SetInvalidParam("uploadFile", "channel_id")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
channelId := props["channel_id"][0]
|
|
|
|
|
if len(channelId) == 0 {
|
|
|
|
|
c.SetInvalidParam("uploadFile", "channel_id")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_UPLOAD_FILE) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
resStruct := &model.FileUploadResponse{
|
2016-09-30 11:06:30 -04:00
|
|
|
FileInfos: []*model.FileInfo{},
|
2015-08-10 12:05:45 -04:00
|
|
|
ClientIds: []string{},
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
previewPathList := []string{}
|
|
|
|
|
thumbnailPathList := []string{}
|
2015-06-14 23:53:32 -08:00
|
|
|
imageDataList := [][]byte{}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
for i, fileHeader := range m.File["files"] {
|
|
|
|
|
file, fileErr := fileHeader.Open()
|
2015-06-14 23:53:32 -08:00
|
|
|
defer file.Close()
|
2016-09-30 11:06:30 -04:00
|
|
|
if fileErr != nil {
|
|
|
|
|
http.Error(w, fileErr.Error(), http.StatusInternalServerError)
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
io.Copy(buf, file)
|
2016-09-30 11:06:30 -04:00
|
|
|
data := buf.Bytes()
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
info, err := doUploadFile(c.TeamId, channelId, c.Session.UserId, fileHeader.Filename, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-07-21 15:18:17 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if info.PreviewPath != "" || info.ThumbnailPath != "" {
|
|
|
|
|
previewPathList = append(previewPathList, info.PreviewPath)
|
|
|
|
|
thumbnailPathList = append(thumbnailPathList, info.ThumbnailPath)
|
|
|
|
|
imageDataList = append(imageDataList, data)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
resStruct.FileInfos = append(resStruct.FileInfos, info)
|
2015-10-26 14:38:46 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if len(m.Value["client_ids"]) > 0 {
|
|
|
|
|
resStruct.ClientIds = append(resStruct.ClientIds, m.Value["client_ids"][i])
|
2015-10-26 14:38:46 -04:00
|
|
|
}
|
2016-09-30 11:06:30 -04:00
|
|
|
}
|
2015-10-26 14:38:46 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
handleImages(previewPathList, thumbnailPathList, imageDataList)
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
w.Write([]byte(resStruct.ToJson()))
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func doUploadFile(teamId string, channelId string, userId string, rawFilename string, data []byte) (*model.FileInfo, *model.AppError) {
|
|
|
|
|
filename := filepath.Base(rawFilename)
|
2015-07-17 15:55:06 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
info, err := model.GetInfoForBytes(filename, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return nil, err
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
info.Id = model.NewId()
|
|
|
|
|
info.CreatorId = userId
|
|
|
|
|
|
|
|
|
|
pathPrefix := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + info.Id + "/"
|
|
|
|
|
info.Path = pathPrefix + filename
|
|
|
|
|
|
|
|
|
|
if info.IsImage() {
|
|
|
|
|
// Check dimensions before loading the whole thing into memory later on
|
|
|
|
|
if info.Width*info.Height > MaxImageSize {
|
|
|
|
|
err := model.NewLocAppError("uploadFile", "api.file.upload_file.large_image.app_error", nil, "")
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nameWithoutExtension := filename[:strings.LastIndex(filename, ".")]
|
|
|
|
|
info.PreviewPath = pathPrefix + nameWithoutExtension + "_preview.jpg"
|
|
|
|
|
info.ThumbnailPath = pathPrefix + nameWithoutExtension + "_thumb.jpg"
|
2015-08-10 12:05:45 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if err := WriteFile(data, info.Path); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if result := <-Srv.Store.FileInfo().Save(info); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
return info, nil
|
|
|
|
|
}
|
2016-05-18 22:34:31 +02:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func handleImages(previewPathList []string, thumbnailPathList []string, fileData [][]byte) {
|
2016-10-20 09:13:33 -04:00
|
|
|
for i, data := range fileData {
|
|
|
|
|
go func(i int, data []byte) {
|
2016-05-18 22:34:31 +02:00
|
|
|
// Decode image bytes into Image object
|
|
|
|
|
img, imgType, err := image.Decode(bytes.NewReader(fileData[i]))
|
|
|
|
|
if err != nil {
|
2016-09-30 11:06:30 -04:00
|
|
|
l4g.Error(utils.T("api.file.handle_images_forget.decode.error"), err)
|
2016-05-18 22:34:31 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
width := img.Bounds().Dx()
|
|
|
|
|
height := img.Bounds().Dy()
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
// Fill in the background of a potentially-transparent png file as white
|
2016-05-18 22:34:31 +02:00
|
|
|
if imgType == "png" {
|
|
|
|
|
dst := image.NewRGBA(img.Bounds())
|
|
|
|
|
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
|
|
|
|
draw.Draw(dst, dst.Bounds(), img, img.Bounds().Min, draw.Over)
|
|
|
|
|
img = dst
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
// Flip the image to be upright
|
|
|
|
|
orientation, _ := getImageOrientation(fileData[i])
|
|
|
|
|
|
2016-05-18 22:34:31 +02:00
|
|
|
switch orientation {
|
|
|
|
|
case UprightMirrored:
|
|
|
|
|
img = imaging.FlipH(img)
|
|
|
|
|
case UpsideDown:
|
|
|
|
|
img = imaging.Rotate180(img)
|
|
|
|
|
case UpsideDownMirrored:
|
|
|
|
|
img = imaging.FlipV(img)
|
|
|
|
|
case RotatedCWMirrored:
|
|
|
|
|
img = imaging.Transpose(img)
|
|
|
|
|
case RotatedCCW:
|
|
|
|
|
img = imaging.Rotate270(img)
|
|
|
|
|
case RotatedCCWMirrored:
|
|
|
|
|
img = imaging.Transverse(img)
|
|
|
|
|
case RotatedCW:
|
|
|
|
|
img = imaging.Rotate90(img)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
go generateThumbnailImage(img, thumbnailPathList[i], width, height)
|
|
|
|
|
go generatePreviewImage(img, previewPathList[i], width)
|
2016-10-20 09:13:33 -04:00
|
|
|
}(i, data)
|
2016-05-18 22:34:31 +02:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-09-17 11:09:13 -04:00
|
|
|
func getImageOrientation(imageData []byte) (int, error) {
|
|
|
|
|
if exifData, err := exif.Decode(bytes.NewReader(imageData)); err != nil {
|
2015-09-18 18:00:09 -04:00
|
|
|
return Upright, err
|
2015-09-17 11:09:13 -04:00
|
|
|
} else {
|
|
|
|
|
if tag, err := exifData.Get("Orientation"); err != nil {
|
2015-09-18 18:00:09 -04:00
|
|
|
return Upright, err
|
2015-09-17 11:09:13 -04:00
|
|
|
} else {
|
|
|
|
|
orientation, err := tag.Int(0)
|
|
|
|
|
if err != nil {
|
2015-09-18 18:00:09 -04:00
|
|
|
return Upright, err
|
2015-09-17 11:09:13 -04:00
|
|
|
} else {
|
|
|
|
|
return orientation, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func generateThumbnailImage(img image.Image, thumbnailPath string, width int, height int) {
|
|
|
|
|
thumbWidth := float64(utils.Cfg.FileSettings.ThumbnailWidth)
|
|
|
|
|
thumbHeight := float64(utils.Cfg.FileSettings.ThumbnailHeight)
|
|
|
|
|
imgWidth := float64(width)
|
|
|
|
|
imgHeight := float64(height)
|
|
|
|
|
|
|
|
|
|
var thumbnail image.Image
|
|
|
|
|
if imgHeight < thumbHeight && imgWidth < thumbWidth {
|
|
|
|
|
thumbnail = img
|
|
|
|
|
} else if imgHeight/imgWidth < thumbHeight/thumbWidth {
|
|
|
|
|
thumbnail = imaging.Resize(img, 0, utils.Cfg.FileSettings.ThumbnailHeight, imaging.Lanczos)
|
|
|
|
|
} else {
|
|
|
|
|
thumbnail = imaging.Resize(img, utils.Cfg.FileSettings.ThumbnailWidth, 0, imaging.Lanczos)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
if err := jpeg.Encode(buf, thumbnail, &jpeg.Options{Quality: 90}); err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.handle_images_forget.encode_jpeg.error"), thumbnailPath, err)
|
2015-08-24 15:03:52 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if err := WriteFile(buf.Bytes(), thumbnailPath); err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), thumbnailPath, err)
|
2015-08-24 15:03:52 -07:00
|
|
|
return
|
|
|
|
|
}
|
2016-09-30 11:06:30 -04:00
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func generatePreviewImage(img image.Image, previewPath string, width int) {
|
|
|
|
|
var preview image.Image
|
|
|
|
|
if width > int(utils.Cfg.FileSettings.PreviewWidth) {
|
|
|
|
|
preview = imaging.Resize(img, utils.Cfg.FileSettings.PreviewWidth, utils.Cfg.FileSettings.PreviewHeight, imaging.Lanczos)
|
|
|
|
|
} else {
|
|
|
|
|
preview = img
|
2015-08-24 15:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
if err := jpeg.Encode(buf, preview, &jpeg.Options{Quality: 90}); err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.handle_images_forget.encode_preview.error"), previewPath, err)
|
2015-08-24 15:03:52 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if err := WriteFile(buf.Bytes(), previewPath); err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.handle_images_forget.upload_preview.error"), previewPath, err)
|
2016-09-13 12:42:48 -04:00
|
|
|
return
|
|
|
|
|
}
|
2016-09-30 11:06:30 -04:00
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
info, err := getFileInfoForRequest(c, r, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if data, err := ReadFile(info.Path); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
c.Err.StatusCode = http.StatusNotFound
|
|
|
|
|
} else if err := writeFileResponse(info.Name, data, w, r); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
info, err := getFileInfoForRequest(c, r, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
2015-08-24 15:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if info.ThumbnailPath == "" {
|
|
|
|
|
c.Err = model.NewLocAppError("getFileThumbnail", "api.file.get_file_thumbnail.no_thumbnail.app_error", nil, "file_id="+info.Id)
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if data, err := ReadFile(info.ThumbnailPath); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
c.Err.StatusCode = http.StatusNotFound
|
|
|
|
|
} else if err := writeFileResponse(info.Name, data, w, r); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-08-24 15:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
info, err := getFileInfoForRequest(c, r, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if info.PreviewPath == "" {
|
|
|
|
|
c.Err = model.NewLocAppError("getFilePreview", "api.file.get_file_preview.no_preview.app_error", nil, "file_id="+info.Id)
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if data, err := ReadFile(info.PreviewPath); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err = err
|
2016-09-30 11:06:30 -04:00
|
|
|
c.Err.StatusCode = http.StatusNotFound
|
|
|
|
|
} else if err := writeFileResponse(info.Name, data, w, r); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err = err
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getFileInfo(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
info, err := getFileInfoForRequest(c, r, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
w.Header().Set("Cache-Control", "max-age=2592000, public")
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
w.Write([]byte(info.ToJson()))
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
2016-05-05 16:35:03 -04:00
|
|
|
if !utils.Cfg.FileSettings.EnablePublicLink {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_disabled.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
|
|
|
return
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
info, err := getFileInfoForRequest(c, r, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hash := r.URL.Query().Get("h")
|
|
|
|
|
|
2016-08-15 17:38:55 -04:00
|
|
|
if len(hash) > 0 {
|
2016-09-30 11:06:30 -04:00
|
|
|
correctHash := generatePublicLinkHash(info.Id, *utils.Cfg.FileSettings.PublicLinkSalt)
|
2016-08-15 17:38:55 -04:00
|
|
|
|
|
|
|
|
if hash != correctHash {
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_invalid.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
} else {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_invalid.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if data, err := ReadFile(info.Path); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err = err
|
2016-09-30 11:06:30 -04:00
|
|
|
c.Err.StatusCode = http.StatusNotFound
|
|
|
|
|
} else if err := writeFileResponse(info.Name, data, w, r); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err = err
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getFileInfoForRequest(c *Context, r *http.Request, requireFileVisible bool) (*model.FileInfo, *model.AppError) {
|
2016-05-05 16:35:03 -04:00
|
|
|
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
2016-09-30 11:06:30 -04:00
|
|
|
err := model.NewLocAppError("getFileInfoForRequest", "api.file.get_file_info_for_request.storage.app_error", nil, "")
|
2016-05-05 16:35:03 -04:00
|
|
|
err.StatusCode = http.StatusNotImplemented
|
2016-09-30 11:06:30 -04:00
|
|
|
return nil, err
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
params := mux.Vars(r)
|
|
|
|
|
|
|
|
|
|
fileId := params["file_id"]
|
|
|
|
|
if len(fileId) != 26 {
|
|
|
|
|
return nil, NewInvalidParamError("getFileInfoForRequest", "file_id")
|
2016-06-30 14:53:36 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
var info *model.FileInfo
|
|
|
|
|
if result := <-Srv.Store.FileInfo().Get(fileId); result.Err != nil {
|
|
|
|
|
return nil, result.Err
|
|
|
|
|
} else {
|
|
|
|
|
info = result.Data.(*model.FileInfo)
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
// only let users access files visible in a channel, unless they're the one who uploaded the file
|
|
|
|
|
if info.CreatorId != c.Session.UserId {
|
|
|
|
|
if len(info.PostId) == 0 {
|
|
|
|
|
err := model.NewLocAppError("getFileInfoForRequest", "api.file.get_file_info_for_request.no_post.app_error", nil, "file_id="+fileId)
|
|
|
|
|
err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if requireFileVisible {
|
|
|
|
|
if !HasPermissionToChannelByPostContext(c, info.PostId, model.PERMISSION_READ_CHANNEL) {
|
|
|
|
|
return nil, c.Err
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
return info, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPublicFileOld(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if len(utils.Cfg.FileSettings.DriverName) == 0 {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_public_file_old.storage.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
|
|
|
return
|
|
|
|
|
} else if !utils.Cfg.FileSettings.EnablePublicLink {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_disabled.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
|
|
|
return
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
params := mux.Vars(r)
|
2016-05-05 16:35:03 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
teamId := params["team_id"]
|
|
|
|
|
channelId := params["channel_id"]
|
|
|
|
|
userId := params["user_id"]
|
|
|
|
|
filename := params["filename"]
|
2016-05-05 16:35:03 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
hash := r.URL.Query().Get("h")
|
|
|
|
|
|
|
|
|
|
if len(hash) > 0 {
|
|
|
|
|
correctHash := generatePublicLinkHash(filename, *utils.Cfg.FileSettings.PublicLinkSalt)
|
|
|
|
|
|
|
|
|
|
if hash != correctHash {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_invalid.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-06-30 14:53:36 -04:00
|
|
|
} else {
|
2016-09-30 11:06:30 -04:00
|
|
|
c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_invalid.app_error", nil, "")
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
|
|
|
|
|
|
|
|
|
var info *model.FileInfo
|
|
|
|
|
if result := <-Srv.Store.FileInfo().GetByPath(path); result.Err != nil {
|
|
|
|
|
c.Err = result.Err
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
info = result.Data.(*model.FileInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(info.PostId) == 0 {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicFileOld", "api.file.get_public_file_old.no_post.app_error", nil, "file_id="+info.Id)
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if data, err := ReadFile(info.Path); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
c.Err.StatusCode = http.StatusNotFound
|
|
|
|
|
} else if err := writeFileResponse(info.Name, data, w, r); err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
2016-05-05 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeFileResponse(filename string, bytes []byte, w http.ResponseWriter, r *http.Request) *model.AppError {
|
2015-06-14 23:53:32 -08:00
|
|
|
w.Header().Set("Cache-Control", "max-age=2592000, public")
|
2016-05-05 16:35:03 -04:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
|
2015-10-06 09:09:52 -04:00
|
|
|
w.Header().Del("Content-Type") // Content-Type will be set automatically by the http writer
|
2015-09-25 11:46:43 -04:00
|
|
|
|
2016-04-08 18:06:35 -04:00
|
|
|
// attach extra headers to trigger a download on IE, Edge, and Safari
|
|
|
|
|
ua := user_agent.New(r.UserAgent())
|
|
|
|
|
bname, _ := ua.Browser()
|
2015-09-25 11:46:43 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename=\""+filename+"\"")
|
2016-02-23 07:54:51 -08:00
|
|
|
|
2016-04-08 18:06:35 -04:00
|
|
|
if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" {
|
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
2015-09-25 11:46:43 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-08 18:06:35 -04:00
|
|
|
// prevent file links from being embedded in iframes
|
|
|
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
|
|
|
w.Header().Set("Content-Security-Policy", "Frame-ancestors 'none'")
|
|
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
w.Write(bytes)
|
|
|
|
|
|
|
|
|
|
return nil
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getPublicLink(c *Context, w http.ResponseWriter, r *http.Request) {
|
2015-09-23 13:47:10 -07:00
|
|
|
if !utils.Cfg.FileSettings.EnablePublicLink {
|
2016-01-22 01:37:11 -03:00
|
|
|
c.Err = model.NewLocAppError("getPublicLink", "api.file.get_public_link.disabled.app_error", nil, "")
|
2016-05-05 16:35:03 -04:00
|
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
2016-04-21 22:37:01 -07:00
|
|
|
return
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
info, err := getFileInfoForRequest(c, r, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
if len(info.PostId) == 0 {
|
|
|
|
|
c.Err = model.NewLocAppError("getPublicLink", "api.file.get_public_link.no_post.app_error", nil, "file_id="+info.Id)
|
|
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
w.Write([]byte(model.StringToJson(generatePublicLink(c.GetSiteURL(), info))))
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func generatePublicLink(siteURL string, info *model.FileInfo) string {
|
|
|
|
|
hash := generatePublicLinkHash(info.Id, *utils.Cfg.FileSettings.PublicLinkSalt)
|
|
|
|
|
return fmt.Sprintf("%s%s/public/files/%v/get?h=%s", siteURL, model.API_URL_SUFFIX, info.Id, hash)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generatePublicLinkHash(fileId, salt string) string {
|
|
|
|
|
hash := sha256.New()
|
|
|
|
|
hash.Write([]byte(salt))
|
|
|
|
|
hash.Write([]byte(fileId))
|
|
|
|
|
|
|
|
|
|
return base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 16:44:41 -04:00
|
|
|
var fileMigrationLock sync.Mutex
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
// Creates and stores FileInfos for a post created before the FileInfos table existed.
|
|
|
|
|
func migrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
|
|
|
|
|
if len(post.Filenames) == 0 {
|
|
|
|
|
l4g.Warn(utils.T("api.file.migrate_filenames_to_file_infos.no_filenames.warn"), post.Id)
|
|
|
|
|
return []*model.FileInfo{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cchan := Srv.Store.Channel().Get(post.ChannelId)
|
|
|
|
|
|
|
|
|
|
// There's a weird bug that rarely happens where a post ends up with duplicate Filenames so remove those
|
|
|
|
|
filenames := utils.RemoveDuplicatesFromStringArray(post.Filenames)
|
|
|
|
|
|
|
|
|
|
var channel *model.Channel
|
|
|
|
|
if result := <-cchan; result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.channel.app_error"), post.Id, post.ChannelId, result.Err)
|
|
|
|
|
return []*model.FileInfo{}
|
|
|
|
|
} else {
|
|
|
|
|
channel = result.Data.(*model.Channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the team that was used to make this post since its part of the file path that isn't saved in the Filename
|
|
|
|
|
var teamId string
|
|
|
|
|
if channel.TeamId == "" {
|
|
|
|
|
// This post was made in a cross-team DM channel so we need to find where its files were saved
|
|
|
|
|
teamId = findTeamIdForFilename(post, filenames[0])
|
|
|
|
|
} else {
|
|
|
|
|
teamId = channel.TeamId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create FileInfo objects for this post
|
|
|
|
|
infos := make([]*model.FileInfo, 0, len(filenames))
|
|
|
|
|
if teamId == "" {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.team_id.error"), post.Id, filenames)
|
|
|
|
|
} else {
|
|
|
|
|
for _, filename := range filenames {
|
|
|
|
|
info := getInfoForFilename(post, teamId, filename)
|
|
|
|
|
if info == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infos = append(infos, info)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-06 16:44:41 -04:00
|
|
|
// Lock to prevent only one migration thread from trying to update the post at once, preventing duplicate FileInfos from being created
|
|
|
|
|
fileMigrationLock.Lock()
|
|
|
|
|
defer fileMigrationLock.Unlock()
|
|
|
|
|
|
|
|
|
|
if result := <-Srv.Store.Post().Get(post.Id); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.get_post_again.app_error"), post.Id, result.Err)
|
|
|
|
|
return []*model.FileInfo{}
|
|
|
|
|
} else if newPost := result.Data.(*model.PostList).Posts[post.Id]; len(newPost.Filenames) != len(post.Filenames) {
|
|
|
|
|
// Another thread has already created FileInfos for this post, so just return those
|
|
|
|
|
if result := <-Srv.Store.FileInfo().GetForPost(post.Id); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.get_post_file_infos_again.app_error"), post.Id, result.Err)
|
|
|
|
|
return []*model.FileInfo{}
|
|
|
|
|
} else {
|
|
|
|
|
l4g.Debug(utils.T("api.file.migrate_filenames_to_file_infos.not_migrating_post.debug"), post.Id)
|
|
|
|
|
return result.Data.([]*model.FileInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l4g.Debug(utils.T("api.file.migrate_filenames_to_file_infos.migrating_post.debug"), post.Id)
|
|
|
|
|
|
|
|
|
|
savedInfos := make([]*model.FileInfo, 0, len(infos))
|
|
|
|
|
fileIds := make([]string, 0, len(filenames))
|
|
|
|
|
for _, info := range infos {
|
|
|
|
|
if result := <-Srv.Store.FileInfo().Save(info); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.save_file_info.app_error"), post.Id, info.Id, info.Path, result.Err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
savedInfos = append(savedInfos, info)
|
|
|
|
|
fileIds = append(fileIds, info.Id)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
// Copy and save the updated post
|
|
|
|
|
newPost := &model.Post{}
|
|
|
|
|
*newPost = *post
|
2016-09-13 12:42:48 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
newPost.Filenames = []string{}
|
|
|
|
|
newPost.FileIds = fileIds
|
|
|
|
|
|
|
|
|
|
// Update Posts to clear Filenames and set FileIds
|
|
|
|
|
if result := <-Srv.Store.Post().Update(newPost, post); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.save_post.app_error"), post.Id, newPost.FileIds, post.Filenames, result.Err)
|
|
|
|
|
return []*model.FileInfo{}
|
|
|
|
|
} else {
|
2016-10-06 16:44:41 -04:00
|
|
|
return savedInfos
|
2016-09-30 11:06:30 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-07-16 08:54:09 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func findTeamIdForFilename(post *model.Post, filename string) string {
|
|
|
|
|
split := strings.SplitN(filename, "/", 5)
|
|
|
|
|
id := split[3]
|
|
|
|
|
name, _ := url.QueryUnescape(split[4])
|
|
|
|
|
|
|
|
|
|
// This post is in a direct channel so we need to figure out what team the files are stored under.
|
|
|
|
|
if result := <-Srv.Store.Team().GetTeamsByUserId(post.UserId); result.Err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.teams.app_error"), post.Id, result.Err)
|
|
|
|
|
} else if teams := result.Data.([]*model.Team); len(teams) == 1 {
|
|
|
|
|
// The user has only one team so the post must've been sent from it
|
|
|
|
|
return teams[0].Id
|
|
|
|
|
} else {
|
|
|
|
|
for _, team := range teams {
|
|
|
|
|
path := fmt.Sprintf("teams/%s/channels/%s/users/%s/%s/%s", team.Id, post.ChannelId, post.UserId, id, name)
|
|
|
|
|
if _, err := ReadFile(path); err == nil {
|
|
|
|
|
// Found the team that this file was posted from
|
|
|
|
|
return team.Id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""
|
2016-08-15 17:38:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func getInfoForFilename(post *model.Post, teamId string, filename string) *model.FileInfo {
|
|
|
|
|
// Find the path from the Filename of the form /{channelId}/{userId}/{uid}/{nameWithExtension}
|
|
|
|
|
split := strings.SplitN(filename, "/", 5)
|
|
|
|
|
if len(split) < 5 {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.unexpected_filename.error"), post.Id, filename)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2016-08-15 17:38:55 -04:00
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
channelId := split[1]
|
|
|
|
|
userId := split[2]
|
|
|
|
|
oldId := split[3]
|
|
|
|
|
name, _ := url.QueryUnescape(split[4])
|
|
|
|
|
|
|
|
|
|
if split[0] != "" || split[1] != post.ChannelId || split[2] != post.UserId || strings.Contains(split[4], "/") {
|
|
|
|
|
l4g.Warn(utils.T("api.file.migrate_filenames_to_file_infos.mismatched_filename.warn"), post.Id, post.ChannelId, post.UserId, filename)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pathPrefix := fmt.Sprintf("teams/%s/channels/%s/users/%s/%s/", teamId, channelId, userId, oldId)
|
|
|
|
|
path := pathPrefix + name
|
|
|
|
|
|
|
|
|
|
// Open the file and populate the fields of the FileInfo
|
|
|
|
|
var info *model.FileInfo
|
|
|
|
|
if data, err := ReadFile(path); err != nil {
|
|
|
|
|
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.file_not_found.error"), post.Id, filename, path, err)
|
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
var err *model.AppError
|
|
|
|
|
info, err = model.GetInfoForBytes(name, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
l4g.Warn(utils.T("api.file.migrate_filenames_to_file_infos.info.app_error"), post.Id, filename, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate a new ID because with the old system, you could very rarely get multiple posts referencing the same file
|
|
|
|
|
info.Id = model.NewId()
|
|
|
|
|
info.CreatorId = post.UserId
|
|
|
|
|
info.PostId = post.Id
|
|
|
|
|
info.CreateAt = post.CreateAt
|
|
|
|
|
info.UpdateAt = post.UpdateAt
|
|
|
|
|
info.Path = path
|
|
|
|
|
|
|
|
|
|
if info.IsImage() {
|
|
|
|
|
nameWithoutExtension := name[:strings.LastIndex(name, ".")]
|
|
|
|
|
info.PreviewPath = pathPrefix + nameWithoutExtension + "_preview.jpg"
|
|
|
|
|
info.ThumbnailPath = pathPrefix + nameWithoutExtension + "_thumb.jpg"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info
|
2016-08-15 17:38:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-11 13:45:03 -04:00
|
|
|
func WriteFile(f []byte, path string) *model.AppError {
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2015-07-16 08:54:09 -04:00
|
|
|
var auth aws.Auth
|
2015-09-23 13:47:10 -07:00
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
2015-07-16 08:54:09 -04:00
|
|
|
|
2015-12-21 11:24:01 -08:00
|
|
|
s := s3.New(auth, awsRegion())
|
2015-09-23 13:47:10 -07:00
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
2015-07-16 08:54:09 -04:00
|
|
|
|
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
if model.IsFileExtImage(ext) {
|
|
|
|
|
options := s3.Options{}
|
|
|
|
|
err = bucket.Put(path, f, model.GetImageMimeType(ext), s3.Private, options)
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
options := s3.Options{}
|
|
|
|
|
err = bucket.Put(path, f, "binary/octet-stream", s3.Private, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2016-04-11 13:45:03 -04:00
|
|
|
return model.NewLocAppError("WriteFile", "api.file.write_file.s3.app_error", nil, err.Error())
|
2015-07-16 08:54:09 -04:00
|
|
|
}
|
2015-09-23 13:47:10 -07:00
|
|
|
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
|
2016-09-30 11:06:30 -04:00
|
|
|
if err := writeFileLocally(f, utils.Cfg.FileSettings.Directory+path); err != nil {
|
2016-01-04 12:44:22 -05:00
|
|
|
return err
|
2015-07-16 08:54:09 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-04-11 13:45:03 -04:00
|
|
|
return model.NewLocAppError("WriteFile", "api.file.write_file.configured.app_error", nil, "")
|
2015-07-16 08:54:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 06:53:30 -07:00
|
|
|
func MoveFile(oldPath, newPath string) *model.AppError {
|
2016-03-08 12:27:27 -05:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2016-09-30 11:06:30 -04:00
|
|
|
fileBytes, _ := ReadFile(oldPath)
|
2016-03-08 12:27:27 -05:00
|
|
|
|
|
|
|
|
if fileBytes == nil {
|
|
|
|
|
return model.NewLocAppError("moveFile", "api.file.move_file.get_from_s3.app_error", nil, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var auth aws.Auth
|
|
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
|
|
|
|
|
|
|
|
|
s := s3.New(auth, awsRegion())
|
|
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
|
|
|
|
|
|
|
|
|
if err := bucket.Del(oldPath); err != nil {
|
|
|
|
|
return model.NewLocAppError("moveFile", "api.file.move_file.delete_from_s3.app_error", nil, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 13:45:03 -04:00
|
|
|
if err := WriteFile(fileBytes, newPath); err != nil {
|
2016-03-08 12:27:27 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
|
2016-04-28 06:53:30 -07:00
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(utils.Cfg.FileSettings.Directory+newPath), 0774); err != nil {
|
|
|
|
|
return model.NewLocAppError("moveFile", "api.file.move_file.rename.app_error", nil, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 12:27:27 -05:00
|
|
|
if err := os.Rename(utils.Cfg.FileSettings.Directory+oldPath, utils.Cfg.FileSettings.Directory+newPath); err != nil {
|
|
|
|
|
return model.NewLocAppError("moveFile", "api.file.move_file.rename.app_error", nil, err.Error())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return model.NewLocAppError("moveFile", "api.file.move_file.configured.app_error", nil, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 11:06:30 -04:00
|
|
|
func writeFileLocally(f []byte, path string) *model.AppError {
|
2016-01-04 12:44:22 -05:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0774); err != nil {
|
2016-05-31 21:35:08 +07:00
|
|
|
directory, _ := filepath.Abs(filepath.Dir(path))
|
|
|
|
|
return model.NewLocAppError("WriteFile", "api.file.write_file_locally.create_dir.app_error", nil, "directory="+directory+", err="+err.Error())
|
2016-01-04 12:44:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(path, f, 0644); err != nil {
|
2016-04-11 13:45:03 -04:00
|
|
|
return model.NewLocAppError("WriteFile", "api.file.write_file_locally.writing.app_error", nil, err.Error())
|
2016-01-04 12:44:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 13:45:03 -04:00
|
|
|
func ReadFile(path string) ([]byte, *model.AppError) {
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2015-07-16 08:54:09 -04:00
|
|
|
var auth aws.Auth
|
2015-09-23 13:47:10 -07:00
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
2015-07-16 08:54:09 -04:00
|
|
|
|
2015-12-21 11:24:01 -08:00
|
|
|
s := s3.New(auth, awsRegion())
|
2015-09-23 13:47:10 -07:00
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
2015-07-16 08:54:09 -04:00
|
|
|
|
|
|
|
|
// try to get the file from S3 with some basic retry logic
|
|
|
|
|
tries := 0
|
|
|
|
|
for {
|
|
|
|
|
tries++
|
|
|
|
|
|
|
|
|
|
f, err := bucket.Get(path)
|
|
|
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
|
return f, nil
|
|
|
|
|
} else if tries >= 3 {
|
2016-04-11 13:45:03 -04:00
|
|
|
return nil, model.NewLocAppError("ReadFile", "api.file.read_file.get.app_error", nil, "path="+path+", err="+err.Error())
|
2015-07-16 08:54:09 -04:00
|
|
|
}
|
|
|
|
|
time.Sleep(3000 * time.Millisecond)
|
|
|
|
|
}
|
2015-09-23 13:47:10 -07:00
|
|
|
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
|
|
|
|
|
if f, err := ioutil.ReadFile(utils.Cfg.FileSettings.Directory + path); err != nil {
|
2016-04-11 13:45:03 -04:00
|
|
|
return nil, model.NewLocAppError("ReadFile", "api.file.read_file.reading_local.app_error", nil, err.Error())
|
2015-07-16 08:54:09 -04:00
|
|
|
} else {
|
|
|
|
|
return f, nil
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2016-04-11 13:45:03 -04:00
|
|
|
return nil, model.NewLocAppError("ReadFile", "api.file.read_file.configured.app_error", nil, "")
|
2015-07-16 08:54:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-26 12:49:07 -04:00
|
|
|
|
|
|
|
|
func openFileWriteStream(path string) (io.Writer, *model.AppError) {
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2016-01-22 01:37:11 -03:00
|
|
|
return nil, model.NewLocAppError("openFileWriteStream", "api.file.open_file_write_stream.s3.app_error", nil, "")
|
2015-09-23 13:47:10 -07:00
|
|
|
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(utils.Cfg.FileSettings.Directory+path), 0774); err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
return nil, model.NewLocAppError("openFileWriteStream", "api.file.open_file_write_stream.creating_dir.app_error", nil, err.Error())
|
2015-08-26 12:49:07 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-23 13:47:10 -07:00
|
|
|
if fileHandle, err := os.Create(utils.Cfg.FileSettings.Directory + path); err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
return nil, model.NewLocAppError("openFileWriteStream", "api.file.open_file_write_stream.local_server.app_error", nil, err.Error())
|
2015-08-26 12:49:07 -04:00
|
|
|
} else {
|
|
|
|
|
fileHandle.Chmod(0644)
|
|
|
|
|
return fileHandle, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 01:37:11 -03:00
|
|
|
return nil, model.NewLocAppError("openFileWriteStream", "api.file.open_file_write_stream.configured.app_error", nil, "")
|
2015-08-26 12:49:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func closeFileWriteStream(file io.Writer) {
|
|
|
|
|
file.(*os.File).Close()
|
|
|
|
|
}
|
2015-12-21 11:24:01 -08:00
|
|
|
|
|
|
|
|
func awsRegion() aws.Region {
|
|
|
|
|
if region, ok := aws.Regions[utils.Cfg.FileSettings.AmazonS3Region]; ok {
|
|
|
|
|
return region
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return aws.Region{
|
|
|
|
|
Name: utils.Cfg.FileSettings.AmazonS3Region,
|
|
|
|
|
S3Endpoint: utils.Cfg.FileSettings.AmazonS3Endpoint,
|
|
|
|
|
S3BucketEndpoint: utils.Cfg.FileSettings.AmazonS3BucketEndpoint,
|
|
|
|
|
S3LocationConstraint: *utils.Cfg.FileSettings.AmazonS3LocationConstraint,
|
|
|
|
|
S3LowercaseBucket: *utils.Cfg.FileSettings.AmazonS3LowercaseBucket,
|
|
|
|
|
}
|
|
|
|
|
}
|