diff --git a/api/api.go b/api/api.go index 492c3b0a95..eea70c9b56 100644 --- a/api/api.go +++ b/api/api.go @@ -33,7 +33,9 @@ type Routes struct { Commands *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/commands' Hooks *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/hooks' - Files *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/files' + TeamFiles *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/files' + Files *mux.Router // 'api/v3/files' + NeedFile *mux.Router // 'api/v3/files/{file_id:[A-Za-z0-9]+}' OAuth *mux.Router // 'api/v3/oauth' @@ -70,7 +72,9 @@ func InitApi() { BaseRoutes.Posts = BaseRoutes.NeedChannel.PathPrefix("/posts").Subrouter() BaseRoutes.NeedPost = BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() BaseRoutes.Commands = BaseRoutes.NeedTeam.PathPrefix("/commands").Subrouter() - BaseRoutes.Files = BaseRoutes.NeedTeam.PathPrefix("/files").Subrouter() + BaseRoutes.TeamFiles = BaseRoutes.NeedTeam.PathPrefix("/files").Subrouter() + BaseRoutes.Files = BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter() + BaseRoutes.NeedFile = BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter() BaseRoutes.Hooks = BaseRoutes.NeedTeam.PathPrefix("/hooks").Subrouter() BaseRoutes.OAuth = BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter() BaseRoutes.Admin = BaseRoutes.ApiRoot.PathPrefix("/admin").Subrouter() diff --git a/api/authorization.go b/api/authorization.go index fb04b069b1..5badf244ba 100644 --- a/api/authorization.go +++ b/api/authorization.go @@ -114,6 +114,42 @@ func HasPermissionToChannel(user *model.User, teamMember *model.TeamMember, chan return HasPermissionToTeam(user, teamMember, permission) } +func HasPermissionToChannelByPostContext(c *Context, postId string, permission *model.Permission) bool { + cmc := Srv.Store.Channel().GetMemberForPost(postId, c.Session.UserId) + + var channelRoles []string + if cmcresult := <-cmc; cmcresult.Err == nil { + channelMember := cmcresult.Data.(*model.ChannelMember) + channelRoles = channelMember.GetRoles() + + if CheckIfRolesGrantPermission(channelRoles, permission.Id) { + return true + } + } + + cc := Srv.Store.Channel().GetForPost(postId) + if ccresult := <-cc; ccresult.Err == nil { + channel := ccresult.Data.(*model.Channel) + + if teamMember := c.Session.GetTeamByTeamId(channel.TeamId); teamMember != nil { + roles := teamMember.GetRoles() + + if CheckIfRolesGrantPermission(roles, permission.Id) { + return true + } + } + + } + + if HasPermissionToContext(c, permission) { + return true + } + + c.Err = model.NewLocAppError("HasPermissionToChannelByPostContext", "api.context.permissions.app_error", nil, "userId="+c.Session.UserId+", "+"permission="+permission.Id+" channelRoles="+model.RoleIdsToString(channelRoles)) + c.Err.StatusCode = http.StatusForbidden + return false +} + func HasPermissionToUser(c *Context, userId string) bool { // You are the user (users autmaticly have permissions to themselves) if c.Session.UserId == userId { diff --git a/api/auto_posts.go b/api/auto_posts.go index 2e26e513bb..6b1207c10b 100644 --- a/api/auto_posts.go +++ b/api/auto_posts.go @@ -8,7 +8,6 @@ import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" "io" - "mime/multipart" "os" ) @@ -40,53 +39,31 @@ func NewAutoPostCreator(client *model.Client, channelid string) *AutoPostCreator } func (cfg *AutoPostCreator) UploadTestFile() ([]string, bool) { - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - filename := cfg.ImageFilenames[utils.RandIntFromRange(utils.Range{0, len(cfg.ImageFilenames) - 1})] - part, err := writer.CreateFormFile("files", filename) - if err != nil { - return nil, false - } - path := utils.FindDir("web/static/images") file, err := os.Open(path + "/" + filename) defer file.Close() - _, err = io.Copy(part, file) + data := &bytes.Buffer{} + _, err = io.Copy(data, file) if err != nil { return nil, false } - field, err := writer.CreateFormField("channel_id") - if err != nil { - return nil, false - } - - _, err = field.Write([]byte(cfg.channelid)) - if err != nil { - return nil, false - } - - err = writer.Close() - if err != nil { - return nil, false - } - - resp, appErr := cfg.client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType()) + resp, appErr := cfg.client.UploadPostAttachment(data.Bytes(), cfg.channelid, filename) if appErr != nil { return nil, false } - return resp.Data.(*model.FileUploadResponse).Filenames, true + return []string{resp.FileInfos[0].Id}, true } func (cfg *AutoPostCreator) CreateRandomPost() (*model.Post, bool) { - var filenames []string + var fileIds []string if cfg.HasImage { var err1 bool - filenames, err1 = cfg.UploadTestFile() + fileIds, err1 = cfg.UploadTestFile() if err1 == false { return nil, false } @@ -102,7 +79,7 @@ func (cfg *AutoPostCreator) CreateRandomPost() (*model.Post, bool) { post := &model.Post{ ChannelId: cfg.channelid, Message: postText, - Filenames: filenames} + FileIds: fileIds} result, err2 := cfg.client.CreatePost(post) if err2 != nil { return nil, false diff --git a/api/channel_benchmark_test.go b/api/channel_benchmark_test.go deleted file mode 100644 index 569c2dcc05..0000000000 --- a/api/channel_benchmark_test.go +++ /dev/null @@ -1,284 +0,0 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -package api - -import ( - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/store" - "github.com/mattermost/platform/utils" - "testing" -) - -const ( - NUM_CHANNELS = 140 - NUM_USERS = 40 -) - -func BenchmarkCreateChannel(b *testing.B) { - th := Setup().InitBasic() - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - - b.ResetTimer() - for i := 0; i < b.N; i++ { - channelCreator.CreateTestChannels(utils.Range{NUM_CHANNELS, NUM_CHANNELS}) - } -} - -func BenchmarkCreateDirectChannel(b *testing.B) { - th := Setup().InitBasic() - - userCreator := NewAutoUserCreator(th.BasicClient, th.BasicTeam) - users, err := userCreator.CreateTestUsers(utils.Range{NUM_USERS, NUM_USERS}) - if err == false { - b.Fatal("Could not create users") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := 0; j < NUM_USERS; j++ { - th.BasicClient.CreateDirectChannel(users[j].Id) - } - } -} - -func BenchmarkUpdateChannel(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - CHANNEL_HEADER_LEN = 50 - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - for i := range channels { - channels[i].Header = utils.RandString(CHANNEL_HEADER_LEN, utils.ALPHANUMERIC) - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range channels { - if _, err := th.BasicClient.UpdateChannel(channels[j]); err != nil { - b.Fatal(err) - } - } - } -} - -func BenchmarkGetChannels(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - _, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - th.BasicClient.Must(th.BasicClient.GetChannels("")) - } -} - -func BenchmarkGetMoreChannels(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - _, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - th.BasicClient.Must(th.BasicClient.GetMoreChannels("")) - } -} - -func BenchmarkJoinChannel(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - // Secondary test user to join channels created by primary test user - user := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "That Guy", Password: "pwd"} - user = th.BasicClient.Must(th.BasicClient.CreateUser(user, "")).Data.(*model.User) - LinkUserToTeam(user, th.BasicTeam) - store.Must(Srv.Store.User().VerifyEmail(user.Id)) - th.BasicClient.Login(user.Email, "pwd") - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range channels { - th.BasicClient.Must(th.BasicClient.JoinChannel(channels[j].Id)) - } - } -} - -func BenchmarkDeleteChannel(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range channels { - th.BasicClient.Must(th.BasicClient.DeleteChannel(channels[j].Id)) - } - } -} - -func BenchmarkGetChannelExtraInfo(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range channels { - th.BasicClient.Must(th.BasicClient.GetChannelExtraInfo(channels[j].Id, -1, "")) - } - } -} - -func BenchmarkAddChannelMember(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_USERS = 100 - NUM_USERS_RANGE = utils.Range{NUM_USERS, NUM_USERS} - ) - - channel := &model.Channel{DisplayName: "Test Channel", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id} - channel = th.BasicClient.Must(th.BasicClient.CreateChannel(channel)).Data.(*model.Channel) - - userCreator := NewAutoUserCreator(th.BasicClient, th.BasicTeam) - users, valid := userCreator.CreateTestUsers(NUM_USERS_RANGE) - if valid == false { - b.Fatal("Unable to create test users") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range users { - if _, err := th.BasicClient.AddChannelMember(channel.Id, users[j].Id); err != nil { - b.Fatal(err) - } - } - } -} - -// Is this benchmark failing? Raise your file ulimit! 2048 worked for me. -func BenchmarkRemoveChannelMember(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_USERS = 140 - NUM_USERS_RANGE = utils.Range{NUM_USERS, NUM_USERS} - ) - - channel := &model.Channel{DisplayName: "Test Channel", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id} - channel = th.BasicClient.Must(th.BasicClient.CreateChannel(channel)).Data.(*model.Channel) - - userCreator := NewAutoUserCreator(th.BasicClient, th.BasicTeam) - users, valid := userCreator.CreateTestUsers(NUM_USERS_RANGE) - if valid == false { - b.Fatal("Unable to create test users") - } - - for i := range users { - if _, err := th.BasicClient.AddChannelMember(channel.Id, users[i].Id); err != nil { - b.Fatal(err) - } - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range users { - if _, err := th.BasicClient.RemoveChannelMember(channel.Id, users[j].Id); err != nil { - b.Fatal(err) - } - } - } -} - -func BenchmarkUpdateNotifyProps(b *testing.B) { - th := Setup().InitBasic() - - var ( - NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} - ) - - channelCreator := NewAutoChannelCreator(th.BasicClient, th.BasicTeam) - channels, valid := channelCreator.CreateTestChannels(NUM_CHANNELS_RANGE) - if valid == false { - b.Fatal("Unable to create test channels") - } - - data := make([]map[string]string, len(channels)) - - for i := range data { - newmap := map[string]string{ - "channel_id": channels[i].Id, - "user_id": th.BasicUser.Id, - "desktop": model.CHANNEL_NOTIFY_MENTION, - "mark_unread": model.CHANNEL_MARK_UNREAD_MENTION, - } - data[i] = newmap - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for j := range channels { - th.BasicClient.Must(th.BasicClient.UpdateNotifyProps(data[j])) - } - } -} diff --git a/api/file.go b/api/file.go index dd99a8caf2..9cf513ebf2 100644 --- a/api/file.go +++ b/api/file.go @@ -16,6 +16,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -57,17 +58,19 @@ const ( MaxImageSize = 6048 * 4032 // 24 megapixels, roughly 36MB as a raw image ) -var fileInfoCache *utils.Cache = utils.NewLru(1000) - func InitFile() { l4g.Debug(utils.T("api.file.init.debug")) - BaseRoutes.Files.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST") - BaseRoutes.Files.Handle("/get/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiUserRequiredTrustRequester(getFile)).Methods("GET") - BaseRoutes.Files.Handle("/get_info/{channel_id:[A-Za-z0-9]+}/{user_id:[A-Za-z0-9]+}/{filename:([A-Za-z0-9]+/)?.+(\\.[A-Za-z0-9]{3,})?}", ApiUserRequired(getFileInfo)).Methods("GET") - BaseRoutes.Files.Handle("/get_public_link", ApiUserRequired(getPublicLink)).Methods("POST") + BaseRoutes.TeamFiles.Handle("/upload", ApiUserRequired(uploadFile)).Methods("POST") - 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(getPublicFile)).Methods("GET") + 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") + + 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") } func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { @@ -83,8 +86,7 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { return } - err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize) - if err != nil { + if err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -92,7 +94,6 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { m := r.MultipartForm props := m.Value - if len(props["channel_id"]) == 0 { c.SetInvalidParam("uploadFile", "channel_id") return @@ -103,91 +104,108 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) { return } - files := m.File["files"] - - resStruct := &model.FileUploadResponse{ - Filenames: []string{}, - ClientIds: []string{}, - } - - imageNameList := []string{} - imageDataList := [][]byte{} - if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_UPLOAD_FILE) { return } - for i := range files { - file, err := files[i].Open() + resStruct := &model.FileUploadResponse{ + FileInfos: []*model.FileInfo{}, + ClientIds: []string{}, + } + + previewPathList := []string{} + thumbnailPathList := []string{} + imageDataList := [][]byte{} + + for i, fileHeader := range m.File["files"] { + file, fileErr := fileHeader.Open() defer file.Close() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + if fileErr != nil { + http.Error(w, fileErr.Error(), http.StatusInternalServerError) return } buf := bytes.NewBuffer(nil) io.Copy(buf, file) + data := buf.Bytes() - filename := filepath.Base(files[i].Filename) - - uid := model.NewId() - - if model.IsFileExtImage(filepath.Ext(files[i].Filename)) { - imageNameList = append(imageNameList, uid+"/"+filename) - imageDataList = append(imageDataList, buf.Bytes()) - - // Decode image config first to check dimensions before loading the whole thing into memory later on - config, _, err := image.DecodeConfig(bytes.NewReader(buf.Bytes())) - if err != nil { - c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.image.app_error", nil, err.Error()) - return - } else if config.Width*config.Height > MaxImageSize { - c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.large_image.app_error", nil, c.T("api.file.file_upload.exceeds")) - return - } - } - - path := "teams/" + c.TeamId + "/channels/" + channelId + "/users/" + c.Session.UserId + "/" + uid + "/" + filename - - if err := WriteFile(buf.Bytes(), path); err != nil { + info, err := doUploadFile(c.TeamId, channelId, c.Session.UserId, fileHeader.Filename, data) + if err != nil { c.Err = err return } - encName := utils.UrlEncode(filename) + if info.PreviewPath != "" || info.ThumbnailPath != "" { + previewPathList = append(previewPathList, info.PreviewPath) + thumbnailPathList = append(thumbnailPathList, info.ThumbnailPath) + imageDataList = append(imageDataList, data) + } - fileUrl := "/" + channelId + "/" + c.Session.UserId + "/" + uid + "/" + encName - resStruct.Filenames = append(resStruct.Filenames, fileUrl) + resStruct.FileInfos = append(resStruct.FileInfos, info) + + if len(m.Value["client_ids"]) > 0 { + resStruct.ClientIds = append(resStruct.ClientIds, m.Value["client_ids"][i]) + } } - for _, clientId := range props["client_ids"] { - resStruct.ClientIds = append(resStruct.ClientIds, clientId) - } - - go handleImages(imageNameList, imageDataList, c.TeamId, channelId, c.Session.UserId) + handleImages(previewPathList, thumbnailPathList, imageDataList) w.Write([]byte(resStruct.ToJson())) } -func handleImages(filenames []string, fileData [][]byte, teamId, channelId, userId string) { - dest := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" +func doUploadFile(teamId string, channelId string, userId string, rawFilename string, data []byte) (*model.FileInfo, *model.AppError) { + filename := filepath.Base(rawFilename) - for i, filename := range filenames { - name := filename[:strings.LastIndex(filename, ".")] + info, err := model.GetInfoForBytes(filename, data) + if err != nil { + err.StatusCode = http.StatusBadRequest + return nil, err + } + + 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" + } + + if err := WriteFile(data, info.Path); err != nil { + return nil, err + } + + if result := <-Srv.Store.FileInfo().Save(info); result.Err != nil { + return nil, result.Err + } + + return info, nil +} + +func handleImages(previewPathList []string, thumbnailPathList []string, fileData [][]byte) { + for i := range fileData { go func() { // Decode image bytes into Image object img, imgType, err := image.Decode(bytes.NewReader(fileData[i])) if err != nil { - l4g.Error(utils.T("api.file.handle_images_forget.decode.error"), channelId, userId, filename, err) + l4g.Error(utils.T("api.file.handle_images_forget.decode.error"), err) return } width := img.Bounds().Dx() height := img.Bounds().Dy() - // Get the image's orientation and ignore any errors since not all images will have orientation data - orientation, _ := getImageOrientation(fileData[i]) - + // Fill in the background of a potentially-transparent png file as white if imgType == "png" { dst := image.NewRGBA(img.Bounds()) draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src) @@ -195,6 +213,9 @@ func handleImages(filenames []string, fileData [][]byte, teamId, channelId, user img = dst } + // Flip the image to be upright + orientation, _ := getImageOrientation(fileData[i]) + switch orientation { case UprightMirrored: img = imaging.FlipH(img) @@ -212,57 +233,8 @@ func handleImages(filenames []string, fileData [][]byte, teamId, channelId, user img = imaging.Rotate90(img) } - // Create thumbnail - go func() { - 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) - } - - buf := new(bytes.Buffer) - err = jpeg.Encode(buf, thumbnail, &jpeg.Options{Quality: 90}) - if err != nil { - l4g.Error(utils.T("api.file.handle_images_forget.encode_jpeg.error"), channelId, userId, filename, err) - return - } - - if err := WriteFile(buf.Bytes(), dest+name+"_thumb.jpg"); err != nil { - l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), channelId, userId, filename, err) - return - } - }() - - // Create preview - go func() { - 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 - } - - buf := new(bytes.Buffer) - - err = jpeg.Encode(buf, preview, &jpeg.Options{Quality: 90}) - if err != nil { - l4g.Error(utils.T("api.file.handle_images_forget.encode_preview.error"), channelId, userId, filename, err) - return - } - - if err := WriteFile(buf.Bytes(), dest+name+"_preview.jpg"); err != nil { - l4g.Error(utils.T("api.file.handle_images_forget.upload_preview.error"), channelId, userId, filename, err) - return - } - }() + go generateThumbnailImage(img, thumbnailPathList[i], width, height) + go generatePreviewImage(img, previewPathList[i], width) }() } } @@ -284,88 +256,214 @@ func getImageOrientation(imageData []byte) (int, error) { } } -type ImageGetResult struct { - Error error - ImageData []byte +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) + } + + 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) + return + } + + if err := WriteFile(buf.Bytes(), thumbnailPath); err != nil { + l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), thumbnailPath, err) + return + } +} + +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 + } + + 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) + return + } + + if err := WriteFile(buf.Bytes(), previewPath); err != nil { + l4g.Error(utils.T("api.file.handle_images_forget.upload_preview.error"), previewPath, err) + return + } +} + +func getFile(c *Context, w http.ResponseWriter, r *http.Request) { + info, err := getFileInfoForRequest(c, r, true) + if err != nil { + c.Err = err + 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 + } +} + +func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) { + info, err := getFileInfoForRequest(c, r, true) + if err != nil { + c.Err = err + return + } + + 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 + } + + 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 + } +} + +func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) { + info, err := getFileInfoForRequest(c, r, true) + if err != nil { + c.Err = err + return + } + + 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 + return + } + + if data, err := ReadFile(info.PreviewPath); 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 + } } func getFileInfo(c *Context, w http.ResponseWriter, r *http.Request) { - if len(utils.Cfg.FileSettings.DriverName) == 0 { - c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.storage.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + info, err := getFileInfoForRequest(c, r, true) + if err != nil { + c.Err = err return } - params := mux.Vars(r) - - channelId := params["channel_id"] - if len(channelId) != 26 { - c.SetInvalidParam("getFileInfo", "channel_id") - return - } - - userId := params["user_id"] - if len(userId) != 26 { - c.SetInvalidParam("getFileInfo", "user_id") - return - } - - filename := params["filename"] - if len(filename) == 0 { - c.SetInvalidParam("getFileInfo", "filename") - return - } - - if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_READ_CHANNEL) { - return - } - - path := "teams/" + c.TeamId + "/channels/" + channelId + "/users/" + userId + "/" + filename - var info *model.FileInfo - - if cached, ok := fileInfoCache.Get(path); ok { - info = cached.(*model.FileInfo) - } else { - fileData := make(chan []byte) - go readFile(path, fileData) - - newInfo, err := model.GetInfoForBytes(filename, <-fileData) - if err != nil { - c.Err = err - return - } else { - fileInfoCache.Add(path, newInfo) - info = newInfo - } - } - w.Header().Set("Cache-Control", "max-age=2592000, public") w.Write([]byte(info.ToJson())) } -func getFile(c *Context, w http.ResponseWriter, r *http.Request) { - params := mux.Vars(r) - - teamId := c.TeamId - channelId := params["channel_id"] - userId := params["user_id"] - filename := params["filename"] - - if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_READ_CHANNEL) { +func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { + 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 } - if err, bytes := getFileData(teamId, channelId, userId, filename); err != nil { + info, err := getFileInfoForRequest(c, r, false) + if err != nil { c.Err = err return - } else if err := writeFileResponse(filename, bytes, w, r); err != nil { + } + + hash := r.URL.Query().Get("h") + + if len(hash) > 0 { + correctHash := generatePublicLinkHash(info.Id, *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 + } + } else { + c.Err = model.NewLocAppError("getPublicFile", "api.file.get_file.public_invalid.app_error", nil, "") + 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 } } -func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { +func getFileInfoForRequest(c *Context, r *http.Request, requireFileVisible bool) (*model.FileInfo, *model.AppError) { + if len(utils.Cfg.FileSettings.DriverName) == 0 { + err := model.NewLocAppError("getFileInfoForRequest", "api.file.get_file_info_for_request.storage.app_error", nil, "") + err.StatusCode = http.StatusNotImplemented + return nil, err + } + + params := mux.Vars(r) + + fileId := params["file_id"] + if len(fileId) != 26 { + return nil, NewInvalidParamError("getFileInfoForRequest", "file_id") + } + + var info *model.FileInfo + if result := <-Srv.Store.FileInfo().Get(fileId); result.Err != nil { + return nil, result.Err + } else { + info = result.Data.(*model.FileInfo) + } + + // 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 + } + } + } + + 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 + } + params := mux.Vars(r) teamId := params["team_id"] @@ -375,12 +473,6 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { hash := r.URL.Query().Get("h") - 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 - } - if len(hash) > 0 { correctHash := generatePublicLinkHash(filename, *utils.Cfg.FileSettings.PublicLinkSalt) @@ -395,49 +487,28 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { return } - if err, bytes := getFileData(teamId, channelId, userId, filename); err != nil { - c.Err = err - return - } else if err := writeFileResponse(filename, bytes, w, r); err != nil { - c.Err = err - return - } -} - -func getFileData(teamId string, channelId string, userId string, filename string) (*model.AppError, []byte) { - if len(utils.Cfg.FileSettings.DriverName) == 0 { - err := model.NewLocAppError("getFileData", "api.file.upload_file.storage.app_error", nil, "") - err.StatusCode = http.StatusNotImplemented - return err, nil - } - - if len(teamId) != 26 { - return NewInvalidParamError("getFileData", "team_id"), nil - } - - if len(channelId) != 26 { - return NewInvalidParamError("getFileData", "channel_id"), nil - } - - if len(userId) != 26 { - return NewInvalidParamError("getFileData", "user_id"), nil - } - - if len(filename) == 0 { - return NewInvalidParamError("getFileData", "filename"), nil - } - path := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename - fileChan := make(chan []byte) - go readFile(path, fileChan) - - if bytes := <-fileChan; bytes == nil { - err := model.NewLocAppError("writeFileResponse", "api.file.get_file.not_found.app_error", nil, "path="+path) - err.StatusCode = http.StatusNotFound - return err, nil + var info *model.FileInfo + if result := <-Srv.Store.FileInfo().GetByPath(path); result.Err != nil { + c.Err = result.Err + return } else { - return nil, bytes + 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 } } @@ -450,9 +521,7 @@ func writeFileResponse(filename string, bytes []byte, w http.ResponseWriter, r * ua := user_agent.New(r.UserAgent()) bname, _ := ua.Browser() - parts := strings.Split(filename, "/") - filePart := strings.Split(parts[len(parts)-1], "?")[0] - w.Header().Set("Content-Disposition", "attachment;filename=\""+filePart+"\"") + w.Header().Set("Content-Disposition", "attachment;filename=\""+filename+"\"") if bname == "Edge" || bname == "Internet Explorer" || bname == "Safari" { w.Header().Set("Content-Type", "application/octet-stream") @@ -467,71 +536,183 @@ func writeFileResponse(filename string, bytes []byte, w http.ResponseWriter, r * return nil } -func readFile(path string, fileData chan []byte) { - data, getErr := ReadFile(path) - if getErr != nil { - l4g.Error(getErr) - fileData <- nil - } else { - fileData <- data - } -} - func getPublicLink(c *Context, w http.ResponseWriter, r *http.Request) { - if len(utils.Cfg.FileSettings.DriverName) == 0 { - c.Err = model.NewLocAppError("uploadFile", "api.file.upload_file.storage.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented - return - } - if !utils.Cfg.FileSettings.EnablePublicLink { c.Err = model.NewLocAppError("getPublicLink", "api.file.get_public_link.disabled.app_error", nil, "") c.Err.StatusCode = http.StatusNotImplemented return } - props := model.MapFromJson(r.Body) - - filename := props["filename"] - if len(filename) == 0 { - c.SetInvalidParam("getPublicLink", "filename") + info, err := getFileInfoForRequest(c, r, true) + if err != nil { + c.Err = err return } - matches := model.PartialUrlRegex.FindAllStringSubmatch(filename, -1) - if len(matches) == 0 || len(matches[0]) < 4 { - c.SetInvalidParam("getPublicLink", "filename") + 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 return } - channelId := matches[0][1] - userId := matches[0][2] - filename = matches[0][3] - - if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_GET_PUBLIC_LINK) { - return - } - - url := generatePublicLink(c.GetSiteURL(), c.TeamId, channelId, userId, filename) - - w.Write([]byte(model.StringToJson(url))) + w.Write([]byte(model.StringToJson(generatePublicLink(c.GetSiteURL(), info)))) } -func generatePublicLink(siteURL, teamId, channelId, userId, filename string) string { - hash := generatePublicLinkHash(filename, *utils.Cfg.FileSettings.PublicLinkSalt) - return fmt.Sprintf("%s%s/public/files/get/%s/%s/%s/%s?h=%s", siteURL, model.API_URL_SUFFIX, teamId, channelId, userId, filename, hash) +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(filename, salt string) string { +func generatePublicLinkHash(fileId, salt string) string { hash := sha256.New() hash.Write([]byte(salt)) - hash.Write([]byte(filename)) + hash.Write([]byte(fileId)) return base64.RawURLEncoding.EncodeToString(hash.Sum(nil)) } -func WriteFile(f []byte, path string) *model.AppError { +// 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)) + fileIds := make([]string, 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 + } + + 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, filename, result.Err) + continue + } + + fileIds = append(fileIds, info.Id) + infos = append(infos, info) + } + } + + // Copy and save the updated post + newPost := &model.Post{} + *newPost = *post + + 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 { + return infos + } +} + +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 "" +} + +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 + } + + 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 +} + +func WriteFile(f []byte, path string) *model.AppError { if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { var auth aws.Auth auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId @@ -556,7 +737,7 @@ func WriteFile(f []byte, path string) *model.AppError { return model.NewLocAppError("WriteFile", "api.file.write_file.s3.app_error", nil, err.Error()) } } else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL { - if err := WriteFileLocally(f, utils.Cfg.FileSettings.Directory+path); err != nil { + if err := writeFileLocally(f, utils.Cfg.FileSettings.Directory+path); err != nil { return err } } else { @@ -568,9 +749,7 @@ func WriteFile(f []byte, path string) *model.AppError { func MoveFile(oldPath, newPath string) *model.AppError { if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { - fileData := make(chan []byte) - go readFile(oldPath, fileData) - fileBytes := <-fileData + fileBytes, _ := ReadFile(oldPath) if fileBytes == nil { return model.NewLocAppError("moveFile", "api.file.move_file.get_from_s3.app_error", nil, "") @@ -606,7 +785,7 @@ func MoveFile(oldPath, newPath string) *model.AppError { return nil } -func WriteFileLocally(f []byte, path string) *model.AppError { +func writeFileLocally(f []byte, path string) *model.AppError { if err := os.MkdirAll(filepath.Dir(path), 0774); err != nil { 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()) @@ -620,7 +799,6 @@ func WriteFileLocally(f []byte, path string) *model.AppError { } func ReadFile(path string) ([]byte, *model.AppError) { - if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { var auth aws.Auth auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId @@ -668,7 +846,6 @@ func openFileWriteStream(path string) (io.Writer, *model.AppError) { fileHandle.Chmod(0644) return fileHandle, nil } - } return nil, model.NewLocAppError("openFileWriteStream", "api.file.open_file_write_stream.configured.app_error", nil, "") diff --git a/api/file_benchmark_test.go b/api/file_benchmark_test.go deleted file mode 100644 index 0e0fc105b0..0000000000 --- a/api/file_benchmark_test.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -package api - -import ( - "github.com/mattermost/platform/utils" - "testing" - "time" -) - -func BenchmarkUploadFile(b *testing.B) { - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - testPoster.UploadTestFile() - } -} - -func BenchmarkGetFile(b *testing.B) { - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - filenames, err := testPoster.UploadTestFile() - if err == false { - b.Fatal("Unable to upload file for benchmark") - } - - // wait a bit for files to ready - time.Sleep(5 * time.Second) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - if _, downErr := Client.GetFile(filenames[0]+"?h="+generatePublicLinkHash(filenames[0], *utils.Cfg.FileSettings.PublicLinkSalt), true); downErr != nil { - b.Fatal(downErr) - } - } -} - -func BenchmarkGetPublicLink(b *testing.B) { - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - filenames, err := testPoster.UploadTestFile() - if err == false { - b.Fatal("Unable to upload file for benchmark") - } - - // wait a bit for files to ready - time.Sleep(5 * time.Second) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - if _, downErr := Client.GetPublicLink(filenames[0]); downErr != nil { - b.Fatal(downErr) - } - } -} diff --git a/api/file_test.go b/api/file_test.go index 764f326cda..ded866ab6c 100644 --- a/api/file_test.go +++ b/api/file_test.go @@ -5,14 +5,14 @@ package api import ( "bytes" - "encoding/base64" "fmt" "github.com/goamz/goamz/aws" "github.com/goamz/goamz/s3" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" "io" - "mime/multipart" + "io/ioutil" "net/http" "os" "strings" @@ -22,373 +22,560 @@ import ( func TestUploadFile(t *testing.T) { th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Logf("skipping because no file driver is enabled") + return + } + Client := th.BasicClient team := th.BasicTeam user := th.BasicUser channel := th.BasicChannel - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("files", "../test.png") - if err != nil { + var uploadInfo *model.FileInfo + if data, err := readTestFile("test.png"); err != nil { t.Fatal(err) - } - - path := utils.FindDir("tests") - file, err := os.Open(path + "/test.png") - if err != nil { + } else if resp, err := Client.UploadPostAttachment(data, channel.Id, "test.png"); err != nil { t.Fatal(err) - } - defer file.Close() - - _, err = io.Copy(part, file) - if err != nil { - t.Fatal(err) - } - - field, err := writer.CreateFormField("channel_id") - if err != nil { - t.Fatal(err) - } - - _, err = field.Write([]byte(channel.Id)) - if err != nil { - t.Fatal(err) - } - - err = writer.Close() - if err != nil { - t.Fatal(err) - } - - resp, appErr := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType()) - if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { - if appErr != nil { - t.Fatal(appErr) - } - - filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/") - filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1] - if strings.Contains(filename, "../") { - t.Fatal("relative path should have been sanitized out") - } - fileId := strings.Split(filename, ".")[0] - - var auth aws.Auth - auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId - auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey - - s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region]) - bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket) - - // wait a bit for files to ready - time.Sleep(5 * time.Second) - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename) - if err != nil { - t.Fatal(err) - } - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg") - if err != nil { - t.Fatal(err) - } - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg") - if err != nil { - t.Fatal(err) - } - } else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL { - filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/") - filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1] - if strings.Contains(filename, "../") { - t.Fatal("relative path should have been sanitized out") - } - fileId := strings.Split(filename, ".")[0] - - // wait a bit for files to ready - time.Sleep(5 * time.Second) - - path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } - - path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg" - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } - - path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg" - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } + } else if len(resp.FileInfos) != 1 { + t.Fatal("should've returned a single file infos") } else { - if appErr == nil { - t.Fatal("S3 and local storage not configured, should have failed") - } + uploadInfo = resp.FileInfos[0] + } + + // The returned file info from the upload call will be missing some fields that will be stored in the database + if uploadInfo.CreatorId != user.Id { + t.Fatal("file should be assigned to user") + } else if uploadInfo.PostId != "" { + t.Fatal("file shouldn't have a post") + } else if uploadInfo.Path != "" { + t.Fatal("file path should not be set on returned info") + } else if uploadInfo.ThumbnailPath != "" { + t.Fatal("file thumbnail path should not be set on returned info") + } else if uploadInfo.PreviewPath != "" { + t.Fatal("file preview path should not be set on returned info") + } + + var info *model.FileInfo + if result := <-Srv.Store.FileInfo().Get(uploadInfo.Id); result.Err != nil { + t.Fatal(result.Err) + } else { + info = result.Data.(*model.FileInfo) + } + + if info.Id != uploadInfo.Id { + t.Fatal("file id from response should match one stored in database") + } else if info.CreatorId != user.Id { + t.Fatal("file should be assigned to user") + } else if info.PostId != "" { + t.Fatal("file shouldn't have a post") + } else if info.Path == "" { + t.Fatal("file path should be set in database") + } else if info.ThumbnailPath == "" { + t.Fatal("file thumbnail path should be set in database") + } else if info.PreviewPath == "" { + t.Fatal("file preview path should be set in database") + } + + // This also makes sure that the relative path provided above is sanitized out + expectedPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test.png", team.Id, channel.Id, user.Id, info.Id) + if info.Path != expectedPath { + t.Logf("file is saved in %v", info.Path) + t.Fatalf("file should've been saved in %v", expectedPath) + } + + expectedThumbnailPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test_thumb.jpg", team.Id, channel.Id, user.Id, info.Id) + if info.ThumbnailPath != expectedThumbnailPath { + t.Logf("file thumbnail is saved in %v", info.ThumbnailPath) + t.Fatalf("file thumbnail should've been saved in %v", expectedThumbnailPath) + } + + expectedPreviewPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test_preview.jpg", team.Id, channel.Id, user.Id, info.Id) + if info.PreviewPath != expectedPreviewPath { + t.Logf("file preview is saved in %v", info.PreviewPath) + t.Fatalf("file preview should've been saved in %v", expectedPreviewPath) + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if err := cleanupTestFile(info); err != nil { + t.Fatal(err) + } +} + +func TestGetFileInfo(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + user := th.BasicUser + channel := th.BasicChannel + + var fileId string + if data, err := readTestFile("test.png"); err != nil { + t.Fatal(err) + } else { + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + info, err := Client.GetFileInfo(fileId) + if err != nil { + t.Fatal(err) + } else if info.Id != fileId { + t.Fatal("got incorrect file") + } else if info.CreatorId != user.Id { + t.Fatal("file should be assigned to user") + } else if info.PostId != "" { + t.Fatal("file shouldn't have a post") + } else if info.Path != "" { + t.Fatal("file path shouldn't have been returned to client") + } else if info.ThumbnailPath != "" { + t.Fatal("file thumbnail path shouldn't have been returned to client") + } else if info.PreviewPath != "" { + t.Fatal("file preview path shouldn't have been returned to client") + } else if info.MimeType != "image/png" { + t.Fatal("mime type should've been image/png") + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + // Other user shouldn't be able to get file info for this file before it's attached to a post + th.LoginBasic2() + + if _, err := Client.GetFileInfo(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file info before it's attached to a post") + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + + // Other user shouldn't be able to get file info for this file if they're not in the channel for it + if _, err := Client.GetFileInfo(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file info when not in channel") + } + + Client.Must(Client.JoinChannel(channel.Id)) + + // Other user should now be able to get file info + if info2, err := Client.GetFileInfo(fileId); err != nil { + t.Fatal(err) + } else if info2.Id != fileId { + t.Fatal("other user got incorrect file") + } + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) } } func TestGetFile(t *testing.T) { th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + Client := th.BasicClient - team := th.BasicTeam - user := th.BasicUser channel := th.BasicChannel - if utils.Cfg.FileSettings.DriverName != "" { - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("files", "test.png") - if err != nil { - t.Fatal(err) - } - - path := utils.FindDir("tests") - file, err := os.Open(path + "/test.png") - if err != nil { - t.Fatal(err) - } - defer file.Close() - - _, err = io.Copy(part, file) - if err != nil { - t.Fatal(err) - } - - field, err := writer.CreateFormField("channel_id") - if err != nil { - t.Fatal(err) - } - - _, err = field.Write([]byte(channel.Id)) - if err != nil { - t.Fatal(err) - } - - err = writer.Close() - if err != nil { - t.Fatal(err) - } - - resp, upErr := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType()) - if upErr != nil { - t.Fatal(upErr) - } - - filenames := resp.Data.(*model.FileUploadResponse).Filenames - - // wait a bit for files to ready - time.Sleep(5 * time.Second) - - if _, downErr := Client.GetFile(filenames[0], false); downErr != nil { - t.Fatal(downErr) - } - - if resp, downErr := Client.GetFileInfo(filenames[0]); downErr != nil { - t.Fatal(downErr) - } else { - info := resp.Data.(*model.FileInfo) - if info.Size == 0 { - t.Fatal("No file size returned") - } - } - - if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { - var auth aws.Auth - auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId - auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey - - s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region]) - bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket) - - filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/") - filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1] - fileId := strings.Split(filename, ".")[0] - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename) - if err != nil { - t.Fatal(err) - } - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg") - if err != nil { - t.Fatal(err) - } - - err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg") - if err != nil { - t.Fatal(err) - } - } else { - filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/") - filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1] - fileId := strings.Split(filename, ".")[0] - - path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } - - path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg" - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } - - path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg" - if err := os.Remove(path); err != nil { - t.Fatal("Couldn't remove file at " + path) - } - } + var fileId string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) } else { - if _, downErr := Client.GetFile("/files/get/yxebdmbz5pgupx7q6ez88rw11a/n3btzxu9hbnapqk36iwaxkjxhc/junk.jpg", false); downErr.StatusCode != http.StatusNotImplemented { - t.Fatal("Status code should have been 501 - Not Implemented") + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if body, err := Client.GetFile(fileId); err != nil { + t.Fatal(err) + } else { + received, err := ioutil.ReadAll(body) + if err != nil { + t.Fatal(err) + } else if len(received) != len(data) { + t.Fatal("received file should be the same size as the sent one") } + + for i := range data { + if data[i] != received[i] { + t.Fatal("received file didn't match sent one") + } + } + + body.Close() + } + + // Other user shouldn't be able to get file for this file before it's attached to a post + th.LoginBasic2() + + if _, err := Client.GetFile(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file before it's attached to a post") + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + + // Other user shouldn't be able to get file for this file if they're not in the channel for it + if _, err := Client.GetFile(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file when not in channel") + } + + Client.Must(Client.JoinChannel(channel.Id)) + + // Other user should now be able to get file + if body, err := Client.GetFile(fileId); err != nil { + t.Fatal(err) + } else { + received, err := ioutil.ReadAll(body) + if err != nil { + t.Fatal(err) + } else if len(received) != len(data) { + t.Fatal("received file should be the same size as the sent one") + } + + for i := range data { + if data[i] != received[i] { + t.Fatal("received file didn't match sent one") + } + } + + body.Close() + } + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) + } +} + +func TestGetFileThumbnail(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + channel := th.BasicChannel + + var fileId string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) + } else { + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if body, err := Client.GetFileThumbnail(fileId); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + // Other user shouldn't be able to get thumbnail for this file before it's attached to a post + th.LoginBasic2() + + if _, err := Client.GetFileThumbnail(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file before it's attached to a post") + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + + // Other user shouldn't be able to get thumbnail for this file if they're not in the channel for it + if _, err := Client.GetFileThumbnail(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file when not in channel") + } + + Client.Must(Client.JoinChannel(channel.Id)) + + // Other user should now be able to get thumbnail + if body, err := Client.GetFileThumbnail(fileId); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) + } +} + +func TestGetFilePreview(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + channel := th.BasicChannel + + var fileId string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) + } else { + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if body, err := Client.GetFilePreview(fileId); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + // Other user shouldn't be able to get preview for this file before it's attached to a post + th.LoginBasic2() + + if _, err := Client.GetFilePreview(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file before it's attached to a post") + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + + // Other user shouldn't be able to get preview for this file if they're not in the channel for it + if _, err := Client.GetFilePreview(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file when not in channel") + } + + Client.Must(Client.JoinChannel(channel.Id)) + + // Other user should now be able to get preview + if body, err := Client.GetFilePreview(fileId); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) } } func TestGetPublicFile(t *testing.T) { th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink + publicLinkSalt := *utils.Cfg.FileSettings.PublicLinkSalt + defer func() { + utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink + *utils.Cfg.FileSettings.PublicLinkSalt = publicLinkSalt + }() + utils.Cfg.FileSettings.EnablePublicLink = true + *utils.Cfg.FileSettings.PublicLinkSalt = model.NewId() + Client := th.BasicClient channel := th.BasicChannel - enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink - driverName := utils.Cfg.FileSettings.DriverName - defer func() { - utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink - utils.Cfg.FileSettings.DriverName = driverName - }() - utils.Cfg.FileSettings.EnablePublicLink = true - if driverName == "" { - driverName = model.IMAGE_DRIVER_LOCAL - } - - filenames, err := uploadTestFile(Client, channel.Id) + var fileId string + data, err := readTestFile("test.png") if err != nil { - t.Fatal("failed to upload test file", err) - } - - post1 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Filenames: filenames} - - if rpost1, postErr := Client.CreatePost(post1); postErr != nil { - t.Fatal(postErr) + t.Fatal(err) } else { - post1 = rpost1.Data.(*model.Post) + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id } - var link string - if result, err := Client.GetPublicLink(filenames[0]); err != nil { - t.Fatal("failed to get public link") - } else { - link = result.Data.(string) - } + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) - // test a user that's logged in - if resp, err := http.Get(link); err != nil && resp.StatusCode != http.StatusOK { - t.Fatal("failed to get image with public link while logged in", err) + link := Client.MustGeneric(Client.GetPublicLink(fileId)).(string) + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if resp, err := http.Get(link); err != nil || resp.StatusCode != http.StatusOK { + t.Fatal("failed to get image with public link", err) } if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest { - t.Fatal("should've failed to get image with public link while logged in without hash", resp.Status) + t.Fatal("should've failed to get image with public link without hash", resp.Status) } utils.Cfg.FileSettings.EnablePublicLink = false if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented { - t.Fatal("should've failed to get image with disabled public link while logged in") + t.Fatal("should've failed to get image with disabled public link") } utils.Cfg.FileSettings.EnablePublicLink = true - // test a user that's logged out - Client.Must(Client.Logout()) - - if resp, err := http.Get(link); err != nil && resp.StatusCode != http.StatusOK { - t.Fatal("failed to get image with public link while not logged in", err) - } - - if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest { - t.Fatal("should've failed to get image with public link while not logged in without hash") - } - - utils.Cfg.FileSettings.EnablePublicLink = false - if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented { - t.Fatal("should've failed to get image with disabled public link while not logged in") - } - - utils.Cfg.FileSettings.EnablePublicLink = true - - // test a user that's logged in after the salt has changed + // test after the salt has changed *utils.Cfg.FileSettings.PublicLinkSalt = model.NewId() - th.LoginBasic() if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { - t.Fatal("should've failed to get image with public link while logged in after salt changed") + t.Fatal("should've failed to get image with public link after salt changed") } - Client.Must(Client.Logout()) if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { - t.Fatal("should've failed to get image with public link while not logged in after salt changed") + t.Fatal("should've failed to get image with public link after salt changed") } - if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil { - t.Fatal("failed to cleanup test file", err) + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) } } +func TestGetPublicFileOld(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink + publicLinkSalt := *utils.Cfg.FileSettings.PublicLinkSalt + defer func() { + utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink + *utils.Cfg.FileSettings.PublicLinkSalt = publicLinkSalt + }() + utils.Cfg.FileSettings.EnablePublicLink = true + *utils.Cfg.FileSettings.PublicLinkSalt = model.NewId() + + Client := th.BasicClient + channel := th.BasicChannel + + var fileId string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) + } else { + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + + // reconstruct old style of link + siteURL := *utils.Cfg.ServiceSettings.SiteURL + if siteURL == "" { + siteURL = "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress + } + link := generatePublicLinkOld(siteURL, th.BasicTeam.Id, channel.Id, th.BasicUser.Id, fileId+"/test.png") + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if resp, err := http.Get(link); err != nil || resp.StatusCode != http.StatusOK { + t.Fatalf("failed to get image with public link err=%v resp=%v", err, resp) + } + + if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest { + t.Fatal("should've failed to get image with public link without hash", resp.Status) + } + + utils.Cfg.FileSettings.EnablePublicLink = false + if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented { + t.Fatal("should've failed to get image with disabled public link") + } + + utils.Cfg.FileSettings.EnablePublicLink = true + + // test after the salt has changed + *utils.Cfg.FileSettings.PublicLinkSalt = model.NewId() + + if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { + t.Fatal("should've failed to get image with public link after salt changed") + } + + if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { + t.Fatal("should've failed to get image with public link after salt changed") + } + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) + } +} + +func generatePublicLinkOld(siteURL, teamId, channelId, userId, filename string) string { + hash := generatePublicLinkHash(filename, *utils.Cfg.FileSettings.PublicLinkSalt) + return fmt.Sprintf("%s%s/public/files/get/%s/%s/%s/%s?h=%s", siteURL, model.API_URL_SUFFIX, teamId, channelId, userId, filename, hash) +} + func TestGetPublicLink(t *testing.T) { th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink + defer func() { + utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink + }() + utils.Cfg.FileSettings.EnablePublicLink = true + Client := th.BasicClient channel := th.BasicChannel - enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink - driverName := utils.Cfg.FileSettings.DriverName - defer func() { - utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink - utils.Cfg.FileSettings.DriverName = driverName - }() - if driverName == "" { - driverName = model.IMAGE_DRIVER_LOCAL - } - - filenames, err := uploadTestFile(Client, channel.Id) + var fileId string + data, err := readTestFile("test.png") if err != nil { - t.Fatal("failed to upload test file", err) - } - - post1 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Filenames: filenames} - - if rpost1, postErr := Client.CreatePost(post1); postErr != nil { - t.Fatal(postErr) + t.Fatal(err) } else { - post1 = rpost1.Data.(*model.Post) + fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id } + if _, err := Client.GetPublicLink(fileId); err == nil { + t.Fatal("should've failed to get public link before file is attached to a post") + } + + // Hacky way to assign file to a post (usually would be done by CreatePost call) + store.Must(Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id)) + utils.Cfg.FileSettings.EnablePublicLink = false - if _, err := Client.GetPublicLink(filenames[0]); err == nil || err.StatusCode != http.StatusNotImplemented { - t.Fatal("should've failed when public links are disabled", err) + + if _, err := Client.GetPublicLink(fileId); err == nil { + t.Fatal("should've failed to get public link when disabled") } utils.Cfg.FileSettings.EnablePublicLink = true - if _, err := Client.GetPublicLink("garbage"); err == nil { - t.Fatal("should've failed for invalid link") - } - - if _, err := Client.GetPublicLink(filenames[0]); err != nil { - t.Fatal("should've gotten link for file", err) + if link, err := Client.GetPublicLink(fileId); err != nil { + t.Fatal(err) + } else if link == "" { + t.Fatal("should've received public link") } + // Other user shouldn't be able to get public link for this file if they're not in the channel for it th.LoginBasic2() - if _, err := Client.GetPublicLink(filenames[0]); err == nil { - t.Fatal("should've failed, user not member of channel") + if _, err := Client.GetPublicLink(fileId); err == nil { + t.Fatal("other user shouldn't be able to get file when not in channel") } - th.LoginBasic() + Client.Must(Client.JoinChannel(channel.Id)) - if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil { - t.Fatal("failed to cleanup test file", err) + // Other user should now be able to get public link + if link, err := Client.GetPublicLink(fileId); err != nil { + t.Fatal(err) + } else if link == "" { + t.Fatal("should've received public link") + } + + // Wait a bit for files to ready + time.Sleep(2 * time.Second) + + if err := cleanupTestFile(store.Must(Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil { + t.Fatal(err) } } @@ -415,48 +602,234 @@ func TestGeneratePublicLinkHash(t *testing.T) { } } -func uploadTestFile(Client *model.Client, channelId string) ([]string, error) { - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("files", "test.png") +func TestMigrateFilenamesToFileInfos(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + + user1 := th.BasicUser + + channel1 := Client.Must(Client.CreateChannel(&model.Channel{ + Name: model.NewId(), + Type: model.CHANNEL_OPEN, + // No TeamId set to simulate a direct channel + })).Data.(*model.Channel) + + var fileId1 string + var fileId2 string + data, err := readTestFile("test.png") if err != nil { - return nil, err - } - - // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever - file, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=") - - if _, err := io.Copy(part, bytes.NewReader(file)); err != nil { - return nil, err - } - - field, err := writer.CreateFormField("channel_id") - if err != nil { - return nil, err - } - - if _, err := field.Write([]byte(channelId)); err != nil { - return nil, err - } - - if err := writer.Close(); err != nil { - return nil, err - } - - if resp, err := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType()); err != nil { - return nil, err + t.Fatal(err) } else { - return resp.Data.(*model.FileUploadResponse).Filenames, nil + fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + fileId2 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + + // Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post + post1 := store.Must(Srv.Store.Post().Save(&model.Post{ + UserId: user1.Id, + ChannelId: channel1.Id, + Message: "test", + Filenames: []string{ + fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png"), + fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId2, "test.png"), + fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId2, "test.png"), // duplicate a filename to recreate a rare bug + }, + })).(*model.Post) + + if post1.FileIds != nil && len(post1.FileIds) > 0 { + t.Fatal("post shouldn't have file ids") + } else if post1.Filenames == nil || len(post1.Filenames) != 3 { + t.Fatal("post should have filenames") + } + + // Indirectly call migrateFilenamesToFileInfos by calling Client.GetFileInfosForPost + var infos []*model.FileInfo + if infosResult, err := Client.GetFileInfosForPost(post1.ChannelId, post1.Id, ""); err != nil { + t.Fatal(err) + } else { + infos = infosResult + } + + if len(infos) != 2 { + t.Log(infos) + t.Fatal("should've had 2 infos after migration") + } else if infos[0].Path != "" || infos[0].ThumbnailPath != "" || infos[0].PreviewPath != "" { + t.Fatal("shouldn't return paths to client") + } + + // Should be able to get files after migration + if body, err := Client.GetFile(infos[0].Id); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + if body, err := Client.GetFile(infos[1].Id); err != nil { + t.Fatal(err) + } else { + body.Close() + } + + // Make sure we aren't generating a new set of FileInfos on a second call to GetFileInfosForPost + if infos2 := Client.MustGeneric(Client.GetFileInfosForPost(post1.ChannelId, post1.Id, "")).([]*model.FileInfo); len(infos2) != len(infos) { + t.Fatal("should've received the same 2 infos after second call") + } else if (infos[0].Id != infos2[0].Id && infos[0].Id != infos2[1].Id) || (infos[1].Id != infos2[0].Id && infos[1].Id != infos2[1].Id) { + t.Fatal("should've returned the exact same 2 infos after second call") + } + + if result, err := Client.GetPost(post1.ChannelId, post1.Id, ""); err != nil { + t.Fatal(err) + } else if post := result.Data.(*model.PostList).Posts[post1.Id]; len(post.Filenames) != 0 { + t.Fatal("post shouldn't have filenames") + } else if len(post.FileIds) != 2 { + t.Fatal("post should have 2 file ids") + } else if (infos[0].Id != post.FileIds[0] && infos[0].Id != post.FileIds[1]) || (infos[1].Id != post.FileIds[0] && infos[1].Id != post.FileIds[1]) { + t.Fatal("post file ids should match GetFileInfosForPost results") } } -func cleanupTestFile(fullFilename, teamId, channelId, userId string) error { - filenames := strings.Split(fullFilename, "/") - filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1] - fileId := strings.Split(filename, ".")[0] +func TestFindTeamIdForFilename(t *testing.T) { + th := Setup().InitBasic() + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + + user1 := th.BasicUser + + team1 := th.BasicTeam + team2 := th.CreateTeam(th.BasicClient) + + channel1 := th.BasicChannel + + Client.SetTeamId(team2.Id) + channel2 := Client.Must(Client.CreateChannel(&model.Channel{ + Name: model.NewId(), + Type: model.CHANNEL_OPEN, + // No TeamId set to simulate a direct channel + })).Data.(*model.Channel) + Client.SetTeamId(team1.Id) + + var fileId1 string + var fileId2 string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) + } else { + fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + + Client.SetTeamId(team2.Id) + fileId2 = Client.MustGeneric(Client.UploadPostAttachment(data, channel2.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + Client.SetTeamId(team1.Id) + } + + // Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post + post1 := store.Must(Srv.Store.Post().Save(&model.Post{ + UserId: user1.Id, + ChannelId: channel1.Id, + Message: "test", + Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png")}, + })).(*model.Post) + + if teamId := findTeamIdForFilename(post1, post1.Filenames[0]); teamId != team1.Id { + t.Fatal("file should've been found under team1") + } + + Client.SetTeamId(team2.Id) + post2 := store.Must(Srv.Store.Post().Save(&model.Post{ + UserId: user1.Id, + ChannelId: channel2.Id, + Message: "test", + Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel2.Id, user1.Id, fileId2, "test.png")}, + })).(*model.Post) + Client.SetTeamId(team1.Id) + + if teamId := findTeamIdForFilename(post2, post2.Filenames[0]); teamId != team2.Id { + t.Fatal("file should've been found under team2") + } +} + +func TestGetInfoForFilename(t *testing.T) { + th := Setup().InitBasic() + + if utils.Cfg.FileSettings.DriverName == "" { + t.Skip("skipping because no file driver is enabled") + } + + Client := th.BasicClient + + user1 := th.BasicUser + + team1 := th.BasicTeam + + channel1 := th.BasicChannel + + var fileId1 string + var path string + var thumbnailPath string + var previewPath string + data, err := readTestFile("test.png") + if err != nil { + t.Fatal(err) + } else { + fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + path = store.Must(Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).Path + thumbnailPath = store.Must(Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).ThumbnailPath + previewPath = store.Must(Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).PreviewPath + } + + // Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post + post1 := store.Must(Srv.Store.Post().Save(&model.Post{ + UserId: user1.Id, + ChannelId: channel1.Id, + Message: "test", + Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png")}, + })).(*model.Post) + + if info := getInfoForFilename(post1, team1.Id, post1.Filenames[0]); info == nil { + t.Fatal("info shouldn't be nil") + } else if info.Id == "" { + t.Fatal("info.Id shouldn't be empty") + } else if info.CreatorId != user1.Id { + t.Fatal("incorrect user id") + } else if info.PostId != post1.Id { + t.Fatal("incorrect user id") + } else if info.Path != path { + t.Fatal("incorrect path") + } else if info.ThumbnailPath != thumbnailPath { + t.Fatal("incorrect thumbnail path") + } else if info.PreviewPath != previewPath { + t.Fatal("incorrect preview path") + } else if info.Name != "test.png" { + t.Fatal("incorrect name") + } +} + +func readTestFile(name string) ([]byte, error) { + path := utils.FindDir("tests") + file, err := os.Open(path + "/" + name) + if err != nil { + return nil, err + } + defer file.Close() + + data := &bytes.Buffer{} + if _, err := io.Copy(data, file); err != nil { + return nil, err + } else { + return data.Bytes(), nil + } +} + +func cleanupTestFile(info *model.FileInfo) error { if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 { - // perform clean-up on s3 var auth aws.Auth auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey @@ -464,31 +837,36 @@ func cleanupTestFile(fullFilename, teamId, channelId, userId string) error { s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region]) bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket) - if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename); err != nil { + if err := bucket.Del(info.Path); err != nil { return err } - if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"); err != nil { + if info.ThumbnailPath != "" { + if err := bucket.Del(info.ThumbnailPath); err != nil { + return err + } + } + + if info.PreviewPath != "" { + if err := bucket.Del(info.PreviewPath); err != nil { + return err + } + } + } else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL { + if err := os.Remove(utils.Cfg.FileSettings.Directory + info.Path); err != nil { return err } - if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"); err != nil { - return err - } - } else { - path := utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename - if err := os.Remove(path); err != nil { - return fmt.Errorf("Couldn't remove file at " + path) + if info.ThumbnailPath != "" { + if err := os.Remove(utils.Cfg.FileSettings.Directory + info.ThumbnailPath); err != nil { + return err + } } - path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg" - if err := os.Remove(path); err != nil { - return fmt.Errorf("Couldn't remove file at " + path) - } - - path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg" - if err := os.Remove(path); err != nil { - return fmt.Errorf("Couldn't remove file at " + path) + if info.PreviewPath != "" { + if err := os.Remove(utils.Cfg.FileSettings.Directory + info.PreviewPath); err != nil { + return err + } } } diff --git a/api/post.go b/api/post.go index 1286a23d98..498f5b3638 100644 --- a/api/post.go +++ b/api/post.go @@ -49,6 +49,7 @@ func InitPost() { BaseRoutes.NeedPost.Handle("/delete", ApiUserRequired(deletePost)).Methods("POST") BaseRoutes.NeedPost.Handle("/before/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsBefore)).Methods("GET") BaseRoutes.NeedPost.Handle("/after/{offset:[0-9]+}/{num_posts:[0-9]+}", ApiUserRequired(getPostsAfter)).Methods("GET") + BaseRoutes.NeedPost.Handle("/get_file_infos", ApiUserRequired(getFileInfosForPost)).Methods("GET") } func createPost(c *Context, w http.ResponseWriter, r *http.Request) { @@ -135,48 +136,26 @@ func CreatePost(c *Context, post *model.Post, triggerWebhooks bool) (*model.Post post.Hashtags, _ = model.ParseHashtags(post.Message) - if len(post.Filenames) > 0 { - doRemove := false - for i := len(post.Filenames) - 1; i >= 0; i-- { - path := post.Filenames[i] - - doRemove = false - if model.UrlRegex.MatchString(path) { - continue - } else if model.PartialUrlRegex.MatchString(path) { - matches := model.PartialUrlRegex.FindAllStringSubmatch(path, -1) - if len(matches) == 0 || len(matches[0]) < 4 { - doRemove = true - } - - channelId := matches[0][1] - if channelId != post.ChannelId { - doRemove = true - } - - userId := matches[0][2] - if userId != post.UserId { - doRemove = true - } - } else { - doRemove = true - } - if doRemove { - l4g.Error(utils.T("api.post.create_post.bad_filename.error"), path) - post.Filenames = append(post.Filenames[:i], post.Filenames[i+1:]...) - } - } - } - var rpost *model.Post if result := <-Srv.Store.Post().Save(post); result.Err != nil { return nil, result.Err } else { rpost = result.Data.(*model.Post) - - go handlePostEvents(c, rpost, triggerWebhooks) } + if len(post.FileIds) > 0 { + // There's a rare bug where the client sends up duplicate FileIds so protect against that + post.FileIds = utils.RemoveDuplicatesFromStringArray(post.FileIds) + + for _, fileId := range post.FileIds { + if result := <-Srv.Store.FileInfo().AttachToPost(fileId, post.Id); result.Err != nil { + l4g.Error(utils.T("api.post.create_post.attach_files.error"), post.Id, post.FileIds, c.Session.UserId, result.Err) + } + } + } + + go handlePostEvents(c, rpost, triggerWebhooks) + return rpost, nil } @@ -566,6 +545,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * pchan := Srv.Store.User().GetProfiles(c.TeamId) dpchan := Srv.Store.User().GetDirectProfiles(c.Session.UserId) mchan := Srv.Store.Channel().GetMembers(post.ChannelId) + fchan := Srv.Store.FileInfo().GetForPost(post.Id) var profileMap map[string]*model.User if result := <-pchan; result.Err != nil { @@ -785,12 +765,18 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * message.Add("sender_name", senderName) message.Add("team_id", team.Id) - if len(post.Filenames) != 0 { + if len(post.FileIds) != 0 { message.Add("otherFile", "true") - for _, filename := range post.Filenames { - ext := filepath.Ext(filename) - if model.IsFileExtImage(ext) { + var infos []*model.FileInfo + if result := <-fchan; result.Err != nil { + l4g.Warn(utils.T("api.post.send_notifications.files.error"), post.Id, result.Err) + } else { + infos = result.Data.([]*model.FileInfo) + } + + for _, info := range infos { + if info.IsImage() { message.Add("image", "true") break } @@ -915,22 +901,29 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann } func getMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string { - if len(strings.TrimSpace(post.Message)) != 0 || len(post.Filenames) == 0 { + if len(strings.TrimSpace(post.Message)) != 0 || len(post.FileIds) == 0 { return post.Message } // extract the filenames from their paths and determine what type of files are attached - filenames := make([]string, len(post.Filenames)) + var infos []*model.FileInfo + if result := <-Srv.Store.FileInfo().GetForPost(post.Id); result.Err != nil { + l4g.Warn(utils.T("api.post.get_message_for_notification.get_files.error"), post.Id, result.Err) + } else { + infos = result.Data.([]*model.FileInfo) + } + + filenames := make([]string, len(infos)) onlyImages := true - for i, filename := range post.Filenames { - var err error - if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil { + for i, info := range infos { + if escaped, err := url.QueryUnescape(filepath.Base(info.Name)); err != nil { // this should never error since filepath was escaped using url.QueryEscape - filenames[i] = filepath.Base(filename) + filenames[i] = escaped + } else { + filenames[i] = info.Name } - ext := filepath.Ext(filename) - onlyImages = onlyImages && model.IsFileExtImage(ext) + onlyImages = onlyImages && info.IsImage() } props := map[string]interface{}{"Filenames": strings.Join(filenames, ", ")} @@ -1099,9 +1092,6 @@ func SendEphemeralPost(teamId, userId string, post *model.Post) { if post.Props == nil { post.Props = model.StringInterface{} } - if post.Filenames == nil { - post.Filenames = []string{} - } message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE, "", post.ChannelId, userId, nil) message.Add("post", post.ToJson()) @@ -1156,9 +1146,13 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { } } - hashtags, _ := model.ParseHashtags(post.Message) + newPost := &model.Post{} + *newPost = *oldPost - if result := <-Srv.Store.Post().Update(oldPost, post.Message, hashtags); result.Err != nil { + newPost.Message = post.Message + newPost.Hashtags, _ = model.ParseHashtags(post.Message) + + if result := <-Srv.Store.Post().Update(newPost, oldPost); result.Err != nil { c.Err = result.Err return } else { @@ -1449,7 +1443,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { message.Add("post", post.ToJson()) go Publish(message) - go DeletePostFiles(c.TeamId, post) + go DeletePostFiles(post) go DeleteFlaggedPost(c.Session.UserId, post) result := make(map[string]string) @@ -1465,17 +1459,13 @@ func DeleteFlaggedPost(userId string, post *model.Post) { } } -func DeletePostFiles(teamId string, post *model.Post) { - if len(post.Filenames) == 0 { +func DeletePostFiles(post *model.Post) { + if len(post.FileIds) != 0 { return } - prefix := "teams/" + teamId + "/channels/" + post.ChannelId + "/users/" + post.UserId + "/" - for _, filename := range post.Filenames { - splitUrl := strings.Split(filename, "/") - oldPath := prefix + splitUrl[len(splitUrl)-2] + "/" + splitUrl[len(splitUrl)-1] - newPath := prefix + splitUrl[len(splitUrl)-2] + "/deleted_" + splitUrl[len(splitUrl)-1] - MoveFile(oldPath, newPath) + if result := <-Srv.Store.FileInfo().DeleteForPost(post.Id); result.Err != nil { + l4g.Warn(utils.T("api.post.delete_post_files.app_error.warn"), post.Id, result.Err) } } @@ -1583,3 +1573,59 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Write([]byte(posts.ToJson())) } + +func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + + channelId := params["channel_id"] + if len(channelId) != 26 { + c.SetInvalidParam("getFileInfosForPost", "channelId") + return + } + + postId := params["post_id"] + if len(postId) != 26 { + c.SetInvalidParam("getFileInfosForPost", "postId") + return + } + + pchan := Srv.Store.Post().Get(postId) + fchan := Srv.Store.FileInfo().GetForPost(postId) + + if !HasPermissionToChannelContext(c, channelId, model.PERMISSION_READ_CHANNEL) { + return + } + + var infos []*model.FileInfo + if result := <-fchan; result.Err != nil { + c.Err = result.Err + return + } else { + infos = result.Data.([]*model.FileInfo) + } + + if len(infos) == 0 { + // No FileInfos were returned so check if they need to be created for this post + var post *model.Post + if result := <-pchan; result.Err != nil { + c.Err = result.Err + return + } else { + post = result.Data.(*model.PostList).Posts[postId] + } + + if len(post.Filenames) > 0 { + // The post has Filenames that need to be replaced with FileInfos + infos = migrateFilenamesToFileInfos(post) + } + } + + etag := model.GetEtagForFileInfos(infos) + + if HandleEtag(etag, w, r) { + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, etag) + w.Write([]byte(model.FileInfosToJson(infos))) + } +} diff --git a/api/post_benchmark_test.go b/api/post_benchmark_test.go deleted file mode 100644 index 5424bc1dda..0000000000 --- a/api/post_benchmark_test.go +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -package api - -import ( - "github.com/mattermost/platform/utils" - "testing" -) - -const ( - NUM_POSTS = 100 -) - -func BenchmarkCreatePost(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - testPoster.CreateTestPosts(NUM_POSTS_RANGE) - } -} - -func BenchmarkUpdatePost(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - UPDATE_POST_LEN = 100 - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - posts, valid := testPoster.CreateTestPosts(NUM_POSTS_RANGE) - if valid == false { - b.Fatal("Unable to create test posts") - } - - for i := range posts { - posts[i].Message = utils.RandString(UPDATE_POST_LEN, utils.ALPHANUMERIC) - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for i := range posts { - if _, err := Client.UpdatePost(posts[i]); err != nil { - b.Fatal(err) - } - } - } -} - -func BenchmarkGetPosts(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - testPoster.CreateTestPosts(NUM_POSTS_RANGE) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS, "")) - } -} - -func BenchmarkSearchPosts(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - testPoster.CreateTestPosts(NUM_POSTS_RANGE) - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - Client.Must(Client.SearchPosts("nothere", false)) - Client.Must(Client.SearchPosts("n", false)) - Client.Must(Client.SearchPosts("#tag", false)) - } -} - -func BenchmarkEtagCache(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - testPoster.CreateTestPosts(NUM_POSTS_RANGE) - - etag := Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS/2, "")).Etag - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - Client.Must(Client.GetPosts(channel.Id, 0, NUM_POSTS/2, etag)) - } -} - -func BenchmarkDeletePosts(b *testing.B) { - var ( - NUM_POSTS_RANGE = utils.Range{NUM_POSTS, NUM_POSTS} - ) - - th := Setup().InitBasic() - Client := th.BasicClient - channel := th.BasicChannel - - testPoster := NewAutoPostCreator(Client, channel.Id) - posts, valid := testPoster.CreateTestPosts(NUM_POSTS_RANGE) - if valid == false { - b.Fatal("Unable to create test posts") - } - - // Benchmark Start - b.ResetTimer() - for i := 0; i < b.N; i++ { - for i := range posts { - Client.Must(Client.DeletePost(channel.Id, posts[i].Id)) - } - } - -} diff --git a/api/post_test.go b/api/post_test.go index 7b7832148b..bdc5278e4c 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -15,6 +15,7 @@ import ( "time" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" ) @@ -23,15 +24,12 @@ func TestCreatePost(t *testing.T) { Client := th.BasicClient team := th.BasicTeam team2 := th.CreateTeam(th.BasicClient) - user1 := th.BasicUser user3 := th.CreateUser(th.BasicClient) LinkUserToTeam(user3, team2) channel1 := th.BasicChannel channel2 := th.CreateChannel(Client, team) - filenames := []string{"/12345678901234567890123456/12345678901234567890123456/12345678901234567890123456/test.png", "/" + channel1.Id + "/" + user1.Id + "/test.png", "www.mattermost.com/fake/url", "junk"} - - post1 := &model.Post{ChannelId: channel1.Id, Message: "#hashtag a" + model.NewId() + "a", Filenames: filenames} + post1 := &model.Post{ChannelId: channel1.Id, Message: "#hashtag a" + model.NewId() + "a"} rpost1, err := Client.CreatePost(post1) if err != nil { t.Fatal(err) @@ -45,8 +43,8 @@ func TestCreatePost(t *testing.T) { t.Fatal("hashtag didn't match") } - if len(rpost1.Data.(*model.Post).Filenames) != 2 { - t.Fatal("filenames didn't parse correctly") + if len(rpost1.Data.(*model.Post).FileIds) != 0 { + t.Fatal("shouldn't have files") } post2 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a", RootId: rpost1.Data.(*model.Post).Id} @@ -109,6 +107,35 @@ func TestCreatePost(t *testing.T) { if _, err = Client.DoApiPost("/channels/"+channel3.Id+"/create", "garbage"); err == nil { t.Fatal("should have been an error") } + + fileIds := make([]string, 4) + if data, err := readTestFile("test.png"); err != nil { + t.Fatal(err) + } else { + for i := 0; i < 3; i++ { + fileIds[i] = Client.MustGeneric(Client.UploadPostAttachment(data, channel3.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + } + + // Make sure duplicated file ids are removed + fileIds[3] = fileIds[0] + + post9 := &model.Post{ + ChannelId: channel3.Id, + Message: "test", + FileIds: fileIds, + } + if resp, err := Client.CreatePost(post9); err != nil { + t.Fatal(err) + } else if rpost9 := resp.Data.(*model.Post); len(rpost9.FileIds) != 3 { + t.Fatal("post should have 3 files") + } else { + infos := store.Must(Srv.Store.FileInfo().GetForPost(rpost9.Id)).([]*model.FileInfo) + + if len(infos) != 3 { + t.Fatal("should've attached all 3 files to post") + } + } } func testCreatePostWithOutgoingHook( @@ -800,10 +827,8 @@ func TestFuzzyPosts(t *testing.T) { Client := th.BasicClient channel1 := th.BasicChannel - filenames := []string{"junk"} - for i := 0; i < len(utils.FUZZY_STRINGS_POSTS); i++ { - post := &model.Post{ChannelId: channel1.Id, Message: utils.FUZZY_STRINGS_POSTS[i], Filenames: filenames} + post := &model.Post{ChannelId: channel1.Id, Message: utils.FUZZY_STRINGS_POSTS[i]} _, err := Client.CreatePost(post) if err != nil { @@ -1150,19 +1175,49 @@ func TestGetFlaggedPosts(t *testing.T) { } func TestGetMessageForNotification(t *testing.T) { - Setup() + Setup().InitBasic() + + testPng := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "test1.png", + Name: "test1.png", + MimeType: "image/png", + })).(*model.FileInfo) + + testJpg1 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "test2.jpg", + Name: "test2.jpg", + MimeType: "image/jpeg", + })).(*model.FileInfo) + + testFile := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "test1.go", + Name: "test1.go", + MimeType: "text/plain", + })).(*model.FileInfo) + + testJpg2 := store.Must(Srv.Store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "test3.jpg", + Name: "test3.jpg", + MimeType: "image/jpeg", + })).(*model.FileInfo) + translateFunc := utils.GetUserTranslations("en") post := &model.Post{ - Message: "test", - Filenames: model.StringArray{}, + Id: model.NewId(), + Message: "test", } if getMessageForNotification(post, translateFunc) != "test" { t.Fatal("should've returned message text") } - post.Filenames = model.StringArray{"test1.png"} + post.FileIds = model.StringArray{testPng.Id} + store.Must(Srv.Store.FileInfo().AttachToPost(testPng.Id, post.Id)) if getMessageForNotification(post, translateFunc) != "test" { t.Fatal("should've returned message text, even with attachments") } @@ -1172,18 +1227,60 @@ func TestGetMessageForNotification(t *testing.T) { t.Fatal("should've returned number of images:", message) } - post.Filenames = model.StringArray{"test1.png", "test2.jpg"} + post.FileIds = model.StringArray{testPng.Id, testJpg1.Id} + store.Must(Srv.Store.FileInfo().AttachToPost(testJpg1.Id, post.Id)) if message := getMessageForNotification(post, translateFunc); message != "2 images sent: test1.png, test2.jpg" { t.Fatal("should've returned number of images:", message) } - post.Filenames = model.StringArray{"test1.go"} + post.Id = model.NewId() + post.FileIds = model.StringArray{testFile.Id} + store.Must(Srv.Store.FileInfo().AttachToPost(testFile.Id, post.Id)) if message := getMessageForNotification(post, translateFunc); message != "1 file sent: test1.go" { t.Fatal("should've returned number of files:", message) } - post.Filenames = model.StringArray{"test1.go", "test2.jpg"} - if message := getMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test2.jpg" { + store.Must(Srv.Store.FileInfo().AttachToPost(testJpg2.Id, post.Id)) + post.FileIds = model.StringArray{testFile.Id, testJpg2.Id} + if message := getMessageForNotification(post, translateFunc); message != "2 files sent: test1.go, test3.jpg" { t.Fatal("should've returned number of mixed files:", message) } } + +func TestGetFileInfosForPost(t *testing.T) { + th := Setup().InitBasic() + Client := th.BasicClient + channel1 := th.BasicChannel + + fileIds := make([]string, 3, 3) + if data, err := readTestFile("test.png"); err != nil { + t.Fatal(err) + } else { + for i := 0; i < 3; i++ { + fileIds[i] = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id + } + } + + post1 := Client.Must(Client.CreatePost(&model.Post{ + ChannelId: channel1.Id, + Message: "test", + FileIds: fileIds, + })).Data.(*model.Post) + + var etag string + if infos, err := Client.GetFileInfosForPost(channel1.Id, post1.Id, ""); err != nil { + t.Fatal(err) + } else if len(infos) != 3 { + t.Fatal("should've received 3 files") + } else if Client.Etag == "" { + t.Fatal("should've received etag") + } else { + etag = Client.Etag + } + + if infos, err := Client.GetFileInfosForPost(channel1.Id, post1.Id, etag); err != nil { + t.Fatal(err) + } else if len(infos) != 0 { + t.Fatal("should've returned nothing because of etag") + } +} diff --git a/i18n/en.json b/i18n/en.json index 63aa5006a6..7bfab85f6a 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -829,14 +829,6 @@ "id": "api.emoji.upload.large_image.app_error", "translation": "Unable to create emoji. Image must be at most 128 by 128 pixels." }, - { - "id": "api.file.file_upload.exceeds", - "translation": "File exceeds max image size." - }, - { - "id": "api.file.get_file.not_found.app_error", - "translation": "Could not find file." - }, { "id": "api.file.get_file.public_disabled.app_error", "translation": "Public links have been disabled by the system administrator" @@ -849,30 +841,98 @@ "id": "api.file.get_public_link.disabled.app_error", "translation": "Public links have been disabled" }, + { + "id": "api.file.get_public_link.no_post.app_error", + "translation": "Unable to get public link for file. File must be attached to a post that can be read by the current user." + }, + { + "id": "api.file.get_file_preview.no_thumbnail.app_error", + "translation": "File doesn't have a preview image" + }, + { + "id": "api.file.get_file_thumbnail.no_thumbnail.app_error", + "translation": "File doesn't have a thumbnail image" + }, + { + "id": "api.file.get_public_file_old.storage.app_error", + "translation": "Unable to get file. Image storage is not configured." + }, + { + "id": "api.file.get_public_file_old.storage.app_error", + "translation": "Unable to get file. Image storage is not configured." + }, + { + "id": "api.file.get_info_for_request.no_post.app_error", + "translation": "Unable to get info for file. File must be attached to a post that can be read by the current user." + }, + { + "id": "api.file.get_info_for_request.storage.app_error", + "translation": "Unable to get info for file. Image storage is not configured." + }, { "id": "api.file.handle_images_forget.decode.error", - "translation": "Unable to decode image channelId=%v userId=%v filename=%v err=%v" + "translation": "Unable to decode image err=%v" }, { "id": "api.file.handle_images_forget.encode_jpeg.error", - "translation": "Unable to encode image as jpeg channelId=%v userId=%v filename=%v err=%v" + "translation": "Unable to encode image as jpeg path=%v err=%v" }, { "id": "api.file.handle_images_forget.encode_preview.error", - "translation": "Unable to encode image as preview jpg channelId=%v userId=%v filename=%v err=%v" + "translation": "Unable to encode image as preview jpg path=%v err=%v" }, { "id": "api.file.handle_images_forget.upload_preview.error", - "translation": "Unable to upload preview channelId=%v userId=%v filename=%v err=%v" + "translation": "Unable to upload preview path=%v err=%v" }, { "id": "api.file.handle_images_forget.upload_thumb.error", - "translation": "Unable to upload thumbnail channelId=%v userId=%v filename=%v err=%v" + "translation": "Unable to upload thumbnail path=%v err=%v" }, { "id": "api.file.init.debug", "translation": "Initializing file api routes" }, + { + "id": "api.file.migrate_filenames_to_file_infos.channel.app_error", + "translation": "Unable to get channel when migrating post to use FileInfos, post_id=%v, channel_id=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.file_not_found.warn", + "translation": "Unable to find file when migrating post to use FileInfos, post_id=%v, filename=%v, path=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.info.app_error", + "translation": "Unable to fully decode file info when migrating post to use FileInfos, post_id=%v, filename=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.mismatched_filename.warn", + "translation": "Found an unusual filename when migrating post to use FileInfos, post_id=%v, channel_id=%v, user_id=%v, filename=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.no_filenames.warn", + "translation": "Unable to migrate post to use FileInfos with an empty Filenames field, post_id=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.save_file_info.warn", + "translation": "Unable to save post when migrating post to use FileInfos, post_id=%v, new_file_ids=%v, old_filenames=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.save_post.warn", + "translation": "Unable to save file info when migrating post to use FileInfos, post_id=%v, file_id=%v, filename=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.team_id.app_error", + "translation": "Unable to find team for FileInfos, post_id=%v, filenames=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.teams.app_error", + "translation": "Unable to get teams when migrating post to use FileInfos, post_id=%v, err=%v" + }, + { + "id": "api.file.migrate_filenames_to_file_infos.unexpected_filename.error", + "translation": "Unable to decipher filename when migrating post to use FileInfos, post_id=%v, filename=%v" + }, { "id": "api.file.move_file.configured.app_error", "translation": "File storage not configured properly. Please configure for either S3 or local server file storage." @@ -917,22 +977,6 @@ "id": "api.file.read_file.reading_local.app_error", "translation": "Encountered an error reading from local server storage" }, - { - "id": "api.file.upload_file.image.app_error", - "translation": "Unable to upload image file." - }, - { - "id": "api.file.upload_file.large_image.app_error", - "translation": "Unable to upload image file. File is too large." - }, - { - "id": "api.file.upload_file.storage.app_error", - "translation": "Unable to upload file. Image storage is not configured." - }, - { - "id": "api.file.upload_file.too_large.app_error", - "translation": "Unable to upload file. File is too large." - }, { "id": "api.file.write_file.configured.app_error", "translation": "File storage not configured properly. Please configure for either S3 or local server file storage." @@ -949,6 +993,18 @@ "id": "api.file.write_file_locally.writing.app_error", "translation": "Encountered an error writing to local server storage" }, + { + "id": "api.file.upload_file.large_image.app_error", + "translation": "Unable to upload image file. File is too large." + }, + { + "id": "api.file.upload_file.storage.app_error", + "translation": "Unable to upload file. Image storage is not configured." + }, + { + "id": "api.file.upload_file.too_large.app_error", + "translation": "Unable to upload file. File is too large." + }, { "id": "api.general.init.debug", "translation": "Initializing general api routes" @@ -1161,6 +1217,10 @@ "id": "api.post.check_for_out_of_channel_mentions.message.one", "translation": "{{.Username}} was mentioned, but they did not receive a notification because they do not belong to this channel." }, + { + "id": "api.post.create_post.attach_files.error", + "translation": "Encountered error attaching files to post, post_id=%s, user_id=%s, file_ids=%v, err=%v" + }, { "id": "api.post.create_post.bad_filename.error", "translation": "Bad filename discarded, filename=%v" @@ -1197,6 +1257,14 @@ "id": "api.post.delete_post.permissions.app_error", "translation": "You do not have the appropriate permissions" }, + { + "id": "api.post.delete_post_files.app_error.warn", + "translation": "Encountered error when deleting files for post, post_id=%v, err=%v" + }, + { + "id": "api.post.get_message_for_notification.get_files.error", + "translation": "Encountered error when getting files for notification message, post_id=%v, err=%v" + }, { "id": "api.post.get_message_for_notification.files_sent", "translation": { @@ -1287,6 +1355,10 @@ "id": "api.post.send_notifications_and_forget.comment_thread.error", "translation": "Failed to retrieve comment thread posts in notifications root_post_id=%v, err=%v" }, + { + "id": "api.post.send_notifications_and_forget.files.error", + "translation": "Failed to get files for post notification post_id=%v, err=%v" + }, { "id": "api.post.send_notifications_and_forget.get_teams.error", "translation": "Failed to get teams when sending cross-team DM user_id=%v, err=%v" @@ -3423,6 +3495,10 @@ "id": "model.post.is_valid.filenames.app_error", "translation": "Invalid filenames" }, + { + "id": "model.post.is_valid.file_ids.app_error", + "translation": "Invalid file ids" + }, { "id": "model.post.is_valid.hashtags.app_error", "translation": "Invalid hashtags" @@ -3875,6 +3951,10 @@ "id": "store.sql_channel.get_extra_members.app_error", "translation": "We couldn't get the extra info for channel members" }, + { + "id": "store.sql_channel.get_for_post.app_error", + "translation": "We couldn't get the channel for the given post" + }, { "id": "store.sql_channel.get_member.app_error", "translation": "We couldn't get the channel member" @@ -3887,6 +3967,10 @@ "id": "store.sql_channel.get_member_count.app_error", "translation": "We couldn't get the channel member count" }, + { + "id": "store.sql_channel.get_member_for_post.app_error", + "translation": "We couldn't get the channel member for the given post" + }, { "id": "store.sql_channel.get_members.app_error", "translation": "We couldn't get the channel members" @@ -4071,6 +4155,30 @@ "id": "store.sql_emoji.save.app_error", "translation": "We couldn't save the emoji" }, + { + "id": "store.sql_file_info.attach_to_post.app_error", + "translation": "We couldn't attach the file info to the post" + }, + { + "id": "store.sql_file_info.delete_for_post.app_error", + "translation": "We couldn't delete the file info to the post" + }, + { + "id": "store.sql_file_info.get.app_error", + "translation": "We couldn't get the file info" + }, + { + "id": "store.sql_file_info.get_by_path.app_error", + "translation": "We couldn't get the file info by path" + }, + { + "id": "store.sql_file_info.get_for_post.app_error", + "translation": "We couldn't get the file info for the post" + }, + { + "id": "store.sql_file_info.save.app_error", + "translation": "We couldn't save the file info" + }, { "id": "store.sql_license.get.app_error", "translation": "We encountered an error getting the license" diff --git a/model/client.go b/model/client.go index 1048239be5..f9a56b86ef 100644 --- a/model/client.go +++ b/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 + } +} diff --git a/model/compliance_post.go b/model/compliance_post.go index ce26a3660e..027e534b70 100644 --- a/model/compliance_post.go +++ b/model/compliance_post.go @@ -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, } } diff --git a/model/compliance_post_test.go b/model/compliance_post_test.go index 28e20ba4bc..49f41a1216 100644 --- a/model/compliance_post_test.go +++ b/model/compliance_post_test.go @@ -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" { diff --git a/model/file.go b/model/file.go index fa98a3b3ab..c218c4246a 100644 --- a/model/file.go +++ b/model/file.go @@ -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 { diff --git a/model/file_info.go b/model/file_info.go index f785042b38..687473d4f7 100644 --- a/model/file_info.go +++ b/model/file_info.go @@ -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) +} diff --git a/model/file_info_test.go b/model/file_info_test.go index 90256aed7a..d3671f2522 100644 --- a/model/file_info_test.go +++ b/model/file_info_test.go @@ -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) } } diff --git a/model/post.go b/model/post.go index 33caeb9ea2..da14b650ff 100644 --- a/model/post.go +++ b/model/post.go @@ -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{}) { diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 99a36b1cda..07c0370754 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -596,6 +596,36 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) StoreChannel return storeChannel } +func (s SqlChannelStore) GetMemberForPost(postId string, userId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + member := &model.ChannelMember{} + if err := s.GetReplica().SelectOne( + member, + `SELECT + ChannelMembers.* + FROM + ChannelMembers, + Posts + WHERE + ChannelMembers.ChannelId = Posts.ChannelId + AND ChannelMembers.UserId = :UserId + AND Posts.Id = :PostId`, map[string]interface{}{"UserId": userId, "PostId": postId}); err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetMemberForPost", "store.sql_channel.get_member_for_post.app_error", nil, "postId="+postId+", err="+err.Error()) + } else { + result.Data = member + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel { storeChannel := make(StoreChannel, 1) @@ -878,6 +908,35 @@ func (s SqlChannelStore) GetAll(teamId string) StoreChannel { return storeChannel } +func (s SqlChannelStore) GetForPost(postId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + channel := &model.Channel{} + if err := s.GetReplica().SelectOne( + channel, + `SELECT + Channels.* + FROM + Channels, + Posts + WHERE + Channels.Id = Posts.ChannelId + AND Posts.Id = :PostId`, map[string]interface{}{"PostId": postId}); err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.GetForPost", "store.sql_channel.get_for_post.app_error", nil, "postId="+postId+", err="+err.Error()) + } else { + result.Data = channel + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index d7d99f581b..0bd059e5fc 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -197,6 +197,29 @@ func TestChannelStoreGet(t *testing.T) { } } +func TestChannelStoreGetForPost(t *testing.T) { + Setup() + + o1 := Must(store.Channel().Save(&model.Channel{ + TeamId: model.NewId(), + DisplayName: "Name", + Name: "a" + model.NewId() + "b", + Type: model.CHANNEL_OPEN, + })).(*model.Channel) + + p1 := Must(store.Post().Save(&model.Post{ + UserId: model.NewId(), + ChannelId: o1.Id, + Message: "test", + })).(*model.Post) + + if r1 := <-store.Channel().GetForPost(p1.Id); r1.Err != nil { + t.Fatal(r1.Err) + } else if r1.Data.(*model.Channel).Id != o1.Id { + t.Fatal("incorrect channel returned") + } +} + func TestChannelStoreDelete(t *testing.T) { Setup() @@ -745,6 +768,39 @@ func TestGetMember(t *testing.T) { } } +func TestChannelStoreGetMemberForPost(t *testing.T) { + Setup() + + o1 := Must(store.Channel().Save(&model.Channel{ + TeamId: model.NewId(), + DisplayName: "Name", + Name: "a" + model.NewId() + "b", + Type: model.CHANNEL_OPEN, + })).(*model.Channel) + + m1 := Must(store.Channel().SaveMember(&model.ChannelMember{ + ChannelId: o1.Id, + UserId: model.NewId(), + NotifyProps: model.GetDefaultChannelNotifyProps(), + })).(*model.ChannelMember) + + p1 := Must(store.Post().Save(&model.Post{ + UserId: model.NewId(), + ChannelId: o1.Id, + Message: "test", + })).(*model.Post) + + if r1 := <-store.Channel().GetMemberForPost(p1.Id, m1.UserId); r1.Err != nil { + t.Fatal(r1.Err) + } else if r1.Data.(*model.ChannelMember).ToJson() != m1.ToJson() { + t.Fatal("invalid returned channel member") + } + + if r2 := <-store.Channel().GetMemberForPost(p1.Id, model.NewId()); r2.Err == nil { + t.Fatal("shouldn't have returned a member") + } +} + func TestGetMemberCount(t *testing.T) { Setup() diff --git a/store/sql_compliance_store.go b/store/sql_compliance_store.go index 6aaef856d4..0a131d2899 100644 --- a/store/sql_compliance_store.go +++ b/store/sql_compliance_store.go @@ -199,7 +199,7 @@ func (s SqlComplianceStore) ComplianceExport(job *model.Compliance) StoreChannel Posts.Type AS PostType, Posts.Props AS PostProps, Posts.Hashtags AS PostHashtags, - Posts.Filenames AS PostFilenames + Posts.FileIds AS PostFileIds FROM Teams, Channels, diff --git a/store/sql_file_info_store.go b/store/sql_file_info_store.go new file mode 100644 index 0000000000..5c3f6b1a47 --- /dev/null +++ b/store/sql_file_info_store.go @@ -0,0 +1,197 @@ +// See License.txt for license information. + +package store + +import ( + "github.com/mattermost/platform/model" +) + +type SqlFileInfoStore struct { + *SqlStore +} + +func NewSqlFileInfoStore(sqlStore *SqlStore) FileInfoStore { + s := &SqlFileInfoStore{sqlStore} + + for _, db := range sqlStore.GetAllConns() { + table := db.AddTableWithName(model.FileInfo{}, "FileInfo").SetKeys(false, "Id") + table.ColMap("Id").SetMaxSize(26) + table.ColMap("CreatorId").SetMaxSize(26) + table.ColMap("PostId").SetMaxSize(26) + table.ColMap("Path").SetMaxSize(512) + table.ColMap("ThumbnailPath").SetMaxSize(512) + table.ColMap("PreviewPath").SetMaxSize(512) + table.ColMap("Name").SetMaxSize(256) + table.ColMap("Extension").SetMaxSize(64) + table.ColMap("MimeType").SetMaxSize(256) + } + + return s +} + +func (fs SqlFileInfoStore) CreateIndexesIfNotExists() { +} + +func (fs SqlFileInfoStore) Save(info *model.FileInfo) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + info.PreSave() + if result.Err = info.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + if err := fs.GetMaster().Insert(info); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.Save", "store.sql_file_info.save.app_error", nil, err.Error()) + } else { + result.Data = info + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (fs SqlFileInfoStore) Get(id string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + info := &model.FileInfo{} + + if err := fs.GetReplica().SelectOne(info, + `SELECT + * + FROM + FileInfo + WHERE + Id = :Id + AND DeleteAt = 0`, map[string]interface{}{"Id": id}); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.Get", "store.sql_file_info.get.app_error", nil, "id="+id+", "+err.Error()) + } else { + result.Data = info + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (fs SqlFileInfoStore) GetByPath(path string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + info := &model.FileInfo{} + + if err := fs.GetReplica().SelectOne(info, + `SELECT + * + FROM + FileInfo + WHERE + Path = :Path + AND DeleteAt = 0`, map[string]interface{}{"Path": path}); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.GetByPath", "store.sql_file_info.get_by_path.app_error", nil, "path="+path+", "+err.Error()) + } else { + result.Data = info + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (fs SqlFileInfoStore) GetForPost(postId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var infos []*model.FileInfo + + if _, err := fs.GetReplica().Select(&infos, + `SELECT + * + FROM + FileInfo + WHERE + PostId = :PostId + AND DeleteAt = 0 + ORDER BY + CreateAt`, map[string]interface{}{"PostId": postId}); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.GetForPost", + "store.sql_file_info.get_for_post.app_error", nil, "post_id="+postId+", "+err.Error()) + } else { + result.Data = infos + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (fs SqlFileInfoStore) AttachToPost(fileId, postId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := fs.GetMaster().Exec( + `UPDATE + FileInfo + SET + PostId = :PostId + WHERE + Id = :Id + AND PostId = ''`, map[string]interface{}{"PostId": postId, "Id": fileId}); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.AttachToPost", + "store.sql_file_info.attach_to_post.app_error", nil, "post_id="+postId+", file_id="+fileId+", err="+err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (fs SqlFileInfoStore) DeleteForPost(postId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := fs.GetMaster().Exec( + `UPDATE + FileInfo + SET + DeleteAt = :DeleteAt + WHERE + PostId = :PostId`, map[string]interface{}{"DeleteAt": model.GetMillis(), "PostId": postId}); err != nil { + result.Err = model.NewLocAppError("SqlFileInfoStore.DeleteForPost", + "store.sql_file_info.delete_for_post.app_error", nil, "post_id="+postId+", err="+err.Error()) + } else { + result.Data = postId + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_file_info_store_test.go b/store/sql_file_info_store_test.go new file mode 100644 index 0000000000..edb9dbd547 --- /dev/null +++ b/store/sql_file_info_store_test.go @@ -0,0 +1,208 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package store + +import ( + "fmt" + "testing" + + "github.com/mattermost/platform/model" +) + +func TestFileInfoSaveGet(t *testing.T) { + Setup() + + info := &model.FileInfo{ + CreatorId: model.NewId(), + Path: "file.txt", + } + + if result := <-store.FileInfo().Save(info); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 { + t.Fatal("should've assigned an id to FileInfo") + } else { + info = returned + } + + if result := <-store.FileInfo().Get(info.Id); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id { + t.Log(info) + t.Log(returned) + t.Fatal("should've returned correct FileInfo") + } + + info2 := Must(store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "file.txt", + DeleteAt: 123, + })).(*model.FileInfo) + + if result := <-store.FileInfo().Get(info2.Id); result.Err == nil { + t.Fatal("shouldn't have gotten deleted file") + } +} + +func TestFileInfoSaveGetByPath(t *testing.T) { + Setup() + + info := &model.FileInfo{ + CreatorId: model.NewId(), + Path: fmt.Sprintf("%v/file.txt", model.NewId()), + } + + if result := <-store.FileInfo().Save(info); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.(*model.FileInfo); len(returned.Id) == 0 { + t.Fatal("should've assigned an id to FileInfo") + } else { + info = returned + } + + if result := <-store.FileInfo().GetByPath(info.Path); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.(*model.FileInfo); returned.Id != info.Id { + t.Log(info) + t.Log(returned) + t.Fatal("should've returned correct FileInfo") + } + + info2 := Must(store.FileInfo().Save(&model.FileInfo{ + CreatorId: model.NewId(), + Path: "file.txt", + DeleteAt: 123, + })).(*model.FileInfo) + + if result := <-store.FileInfo().GetByPath(info2.Id); result.Err == nil { + t.Fatal("shouldn't have gotten deleted file") + } +} + +func TestFileInfoGetForPost(t *testing.T) { + Setup() + + userId := model.NewId() + postId := model.NewId() + + infos := []*model.FileInfo{ + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + }, + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + }, + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + DeleteAt: 123, + }, + { + PostId: model.NewId(), + CreatorId: userId, + Path: "file.txt", + }, + } + + for i, info := range infos { + infos[i] = Must(store.FileInfo().Save(info)).(*model.FileInfo) + } + + if result := <-store.FileInfo().GetForPost(postId); result.Err != nil { + t.Fatal(result.Err) + } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { + t.Fatal("should've returned exactly 2 file infos") + } +} + +func TestFileInfoAttachToPost(t *testing.T) { + Setup() + + userId := model.NewId() + postId := model.NewId() + + info1 := Must(store.FileInfo().Save(&model.FileInfo{ + CreatorId: userId, + Path: "file.txt", + })).(*model.FileInfo) + + if len(info1.PostId) != 0 { + t.Fatal("file shouldn't have a PostId") + } + + if result := <-store.FileInfo().AttachToPost(info1.Id, postId); result.Err != nil { + t.Fatal(result.Err) + } else { + info1 = Must(store.FileInfo().Get(info1.Id)).(*model.FileInfo) + } + + if len(info1.PostId) == 0 { + t.Fatal("file should now have a PostId") + } + + info2 := Must(store.FileInfo().Save(&model.FileInfo{ + CreatorId: userId, + Path: "file.txt", + })).(*model.FileInfo) + + if result := <-store.FileInfo().AttachToPost(info2.Id, postId); result.Err != nil { + t.Fatal(result.Err) + } else { + info2 = Must(store.FileInfo().Get(info2.Id)).(*model.FileInfo) + } + + if result := <-store.FileInfo().GetForPost(postId); result.Err != nil { + t.Fatal(result.Err) + } else if infos := result.Data.([]*model.FileInfo); len(infos) != 2 { + t.Fatal("should've returned exactly 2 file infos") + } +} + +func TestFileInfoDeleteForPost(t *testing.T) { + Setup() + + userId := model.NewId() + postId := model.NewId() + + infos := []*model.FileInfo{ + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + }, + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + }, + { + PostId: postId, + CreatorId: userId, + Path: "file.txt", + DeleteAt: 123, + }, + { + PostId: model.NewId(), + CreatorId: userId, + Path: "file.txt", + }, + } + + for i, info := range infos { + infos[i] = Must(store.FileInfo().Save(info)).(*model.FileInfo) + } + + if result := <-store.FileInfo().DeleteForPost(postId); result.Err != nil { + t.Fatal(result.Err) + } + + if infos := Must(store.FileInfo().GetForPost(postId)).([]*model.FileInfo); len(infos) != 0 { + t.Fatal("shouldn't have returned any file infos") + } +} diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 212492df09..ec8679b314 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -32,6 +32,7 @@ func NewSqlPostStore(sqlStore *SqlStore) PostStore { table.ColMap("Hashtags").SetMaxSize(1000) table.ColMap("Props").SetMaxSize(8000) table.ColMap("Filenames").SetMaxSize(4000) + table.ColMap("FileIds").SetMaxSize(150) } return s @@ -94,42 +95,39 @@ func (s SqlPostStore) Save(post *model.Post) StoreChannel { return storeChannel } -func (s SqlPostStore) Update(oldPost *model.Post, newMessage string, newHashtags string) StoreChannel { +func (s SqlPostStore) Update(newPost *model.Post, oldPost *model.Post) StoreChannel { storeChannel := make(StoreChannel, 1) go func() { result := StoreResult{} - editPost := *oldPost - editPost.Message = newMessage - editPost.UpdateAt = model.GetMillis() - editPost.Hashtags = newHashtags + newPost.UpdateAt = model.GetMillis() - oldPost.DeleteAt = editPost.UpdateAt - oldPost.UpdateAt = editPost.UpdateAt + oldPost.DeleteAt = newPost.UpdateAt + oldPost.UpdateAt = newPost.UpdateAt oldPost.OriginalId = oldPost.Id oldPost.Id = model.NewId() - if result.Err = editPost.IsValid(); result.Err != nil { + if result.Err = newPost.IsValid(); result.Err != nil { storeChannel <- result close(storeChannel) return } - if _, err := s.GetMaster().Update(&editPost); err != nil { - result.Err = model.NewLocAppError("SqlPostStore.Update", "store.sql_post.update.app_error", nil, "id="+editPost.Id+", "+err.Error()) + if _, err := s.GetMaster().Update(newPost); err != nil { + result.Err = model.NewLocAppError("SqlPostStore.Update", "store.sql_post.update.app_error", nil, "id="+newPost.Id+", "+err.Error()) } else { time := model.GetMillis() - s.GetMaster().Exec("UPDATE Channels SET LastPostAt = :LastPostAt WHERE Id = :ChannelId", map[string]interface{}{"LastPostAt": time, "ChannelId": editPost.ChannelId}) + s.GetMaster().Exec("UPDATE Channels SET LastPostAt = :LastPostAt WHERE Id = :ChannelId", map[string]interface{}{"LastPostAt": time, "ChannelId": newPost.ChannelId}) - if len(editPost.RootId) > 0 { - s.GetMaster().Exec("UPDATE Posts SET UpdateAt = :UpdateAt WHERE Id = :RootId", map[string]interface{}{"UpdateAt": time, "RootId": editPost.RootId}) + if len(newPost.RootId) > 0 { + s.GetMaster().Exec("UPDATE Posts SET UpdateAt = :UpdateAt WHERE Id = :RootId", map[string]interface{}{"UpdateAt": time, "RootId": newPost.RootId}) } // mark the old post as deleted s.GetMaster().Insert(oldPost) - result.Data = &editPost + result.Data = newPost } storeChannel <- result @@ -972,7 +970,7 @@ func (s SqlPostStore) AnalyticsPostCount(teamId string, mustHaveFile bool, mustH } if mustHaveFile { - query += " AND Posts.Filenames != '[]'" + query += " AND (Posts.FileIds != '[]' OR Posts.Filenames != '[]')" } if mustHaveHashtag { diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index 6105cbace5..d685ea41ef 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -87,45 +87,73 @@ func TestPostStoreUpdate(t *testing.T) { ro1 := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id] ro2 := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id] - ro6 := (<-store.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id] + ro3 := (<-store.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id] if ro1.Message != o1.Message { t.Fatal("Failed to save/get") } - msg := o1.Message + "BBBBBBBBBB" - if result := <-store.Post().Update(ro1, msg, ""); result.Err != nil { + o1a := &model.Post{} + *o1a = *ro1 + o1a.Message = ro1.Message + "BBBBBBBBBB" + if result := <-store.Post().Update(o1a, ro1); result.Err != nil { t.Fatal(result.Err) } - msg2 := o2.Message + "DDDDDDD" - if result := <-store.Post().Update(ro2, msg2, ""); result.Err != nil { + ro1a := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id] + + if ro1a.Message != o1a.Message { + t.Fatal("Failed to update/get") + } + + o2a := &model.Post{} + *o2a = *ro2 + o2a.Message = ro2.Message + "DDDDDDD" + if result := <-store.Post().Update(o2a, ro2); result.Err != nil { t.Fatal(result.Err) } - msg3 := o3.Message + "WWWWWWW" - if result := <-store.Post().Update(ro6, msg3, "#hashtag"); result.Err != nil { + ro2a := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id] + + if ro2a.Message != o2a.Message { + t.Fatal("Failed to update/get") + } + + o3a := &model.Post{} + *o3a = *ro3 + o3a.Message = ro3.Message + "WWWWWWW" + if result := <-store.Post().Update(o3a, ro3); result.Err != nil { t.Fatal(result.Err) } - ro3 := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o1.Id] + ro3a := (<-store.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id] - if ro3.Message != msg { + if ro3a.Message != o3a.Message && ro3a.Hashtags != o3a.Hashtags { t.Fatal("Failed to update/get") } - ro4 := (<-store.Post().Get(o1.Id)).Data.(*model.PostList).Posts[o2.Id] + o4 := Must(store.Post().Save(&model.Post{ + ChannelId: model.NewId(), + UserId: model.NewId(), + Message: model.NewId(), + Filenames: []string{"test"}, + })).(*model.Post) - if ro4.Message != msg2 { - t.Fatal("Failed to update/get") + ro4 := (<-store.Post().Get(o4.Id)).Data.(*model.PostList).Posts[o4.Id] + + o4a := &model.Post{} + *o4a = *ro4 + o4a.Filenames = []string{} + o4a.FileIds = []string{model.NewId()} + if result := <-store.Post().Update(o4a, ro4); result.Err != nil { + t.Fatal(result.Err) } - ro5 := (<-store.Post().Get(o3.Id)).Data.(*model.PostList).Posts[o3.Id] - - if ro5.Message != msg3 && ro5.Hashtags != "#hashtag" { - t.Fatal("Failed to update/get") + if ro4a := Must(store.Post().Get(o4.Id)).(*model.PostList).Posts[o4.Id]; len(ro4a.Filenames) != 0 { + t.Fatal("Failed to clear Filenames") + } else if len(ro4a.FileIds) != 1 { + t.Fatal("Failed to set FileIds") } - } func TestPostStoreDelete(t *testing.T) { diff --git a/store/sql_store.go b/store/sql_store.go index 4185bb7059..a2bc8f1b8c 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -81,6 +81,7 @@ type SqlStore struct { recovery PasswordRecoveryStore emoji EmojiStore status StatusStore + fileInfo FileInfoStore SchemaVersion string } @@ -129,6 +130,7 @@ func NewSqlStore() Store { sqlStore.recovery = NewSqlPasswordRecoveryStore(sqlStore) sqlStore.emoji = NewSqlEmojiStore(sqlStore) sqlStore.status = NewSqlStatusStore(sqlStore) + sqlStore.fileInfo = NewSqlFileInfoStore(sqlStore) err := sqlStore.master.CreateTablesIfNotExists() if err != nil { @@ -155,6 +157,7 @@ func NewSqlStore() Store { sqlStore.recovery.(*SqlPasswordRecoveryStore).CreateIndexesIfNotExists() sqlStore.emoji.(*SqlEmojiStore).CreateIndexesIfNotExists() sqlStore.status.(*SqlStatusStore).CreateIndexesIfNotExists() + sqlStore.fileInfo.(*SqlFileInfoStore).CreateIndexesIfNotExists() sqlStore.preference.(*SqlPreferenceStore).DeleteUnusedFeatures() @@ -643,6 +646,10 @@ func (ss SqlStore) Status() StatusStore { return ss.status } +func (ss SqlStore) FileInfo() FileInfoStore { + return ss.fileInfo +} + func (ss SqlStore) DropAllTables() { ss.master.TruncateTables() } diff --git a/store/sql_upgrade.go b/store/sql_upgrade.go index 0029df8d49..7ee7ea199f 100644 --- a/store/sql_upgrade.go +++ b/store/sql_upgrade.go @@ -181,7 +181,6 @@ func UpgradeDatabaseToVersion33(sqlStore *SqlStore) { func UpgradeDatabaseToVersion34(sqlStore *SqlStore) { if shouldPerformUpgrade(sqlStore, VERSION_3_3_0, VERSION_3_4_0) { - sqlStore.CreateColumnIfNotExists("Status", "Manual", "BOOLEAN", "BOOLEAN", "0") sqlStore.CreateColumnIfNotExists("Status", "ActiveChannel", "varchar(26)", "varchar(26)", "") @@ -199,6 +198,9 @@ func UpgradeDatabaseToVersion35(sqlStore *SqlStore) { sqlStore.GetMaster().Exec("UPDATE ChannelMembers SET Roles = 'channel_user' WHERE Roles = ''") sqlStore.GetMaster().Exec("UPDATE ChannelMembers SET Roles = 'channel_user channel_admin' WHERE Roles = 'admin'") + // The rest of the migration from Filenames -> FileIds is done lazily in api.GetFileInfosForPost + sqlStore.CreateColumnIfNotExists("Posts", "FileIds", "varchar(150)", "varchar(150)", "[]") + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // UNCOMMENT WHEN WE DO RELEASE // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/store/store.go b/store/store.go index fbe4159860..c01b6d8bcf 100644 --- a/store/store.go +++ b/store/store.go @@ -45,6 +45,7 @@ type Store interface { PasswordRecovery() PasswordRecoveryStore Emoji() EmojiStore Status() StatusStore + FileInfo() FileInfoStore MarkSystemRanUnitTests() Close() DropAllTables() @@ -86,11 +87,13 @@ type ChannelStore interface { GetMoreChannels(teamId string, userId string) StoreChannel GetChannelCounts(teamId string, userId string) StoreChannel GetAll(teamId string) StoreChannel + GetForPost(postId string) StoreChannel SaveMember(member *model.ChannelMember) StoreChannel UpdateMember(member *model.ChannelMember) StoreChannel GetMembers(channelId string) StoreChannel GetMember(channelId string, userId string) StoreChannel + GetMemberForPost(postId string, userId string) StoreChannel GetMemberCount(channelId string) StoreChannel RemoveMember(channelId string, userId string) StoreChannel PermanentDeleteMembersByUser(userId string) StoreChannel @@ -104,7 +107,7 @@ type ChannelStore interface { type PostStore interface { Save(post *model.Post) StoreChannel - Update(post *model.Post, newMessage string, newHashtags string) StoreChannel + Update(newPost *model.Post, oldPost *model.Post) StoreChannel Get(id string) StoreChannel Delete(postId string, time int64) StoreChannel PermanentDeleteByUser(userId string) StoreChannel @@ -277,3 +280,12 @@ type StatusStore interface { GetTotalActiveUsersCount() StoreChannel UpdateLastActivityAt(userId string, lastActivityAt int64) StoreChannel } + +type FileInfoStore interface { + Save(info *model.FileInfo) StoreChannel + Get(id string) StoreChannel + GetByPath(path string) StoreChannel + GetForPost(postId string) StoreChannel + AttachToPost(fileId string, postId string) StoreChannel + DeleteForPost(postId string) StoreChannel +} diff --git a/utils/utils.go b/utils/utils.go index 87c81b70f2..dd60f60607 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -34,3 +34,17 @@ func FileExistsInConfigFolder(filename string) bool { } return false } + +func RemoveDuplicatesFromStringArray(arr []string) []string { + result := make([]string, 0, len(arr)) + seen := make(map[string]bool) + + for _, item := range arr { + if !seen[item] { + result = append(result, item) + seen[item] = true + } + } + + return result +} diff --git a/utils/utils_test.go b/utils/utils_test.go index 41e995e631..88356dadbf 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -28,3 +28,19 @@ func TestStringArrayIntersection(t *testing.T) { t.Fatal("should be 1") } } + +func TestRemoveDuplicatesFromStringArray(t *testing.T) { + a := []string{ + "a", + "b", + "a", + "a", + "b", + "c", + "a", + } + + if len(RemoveDuplicatesFromStringArray(a)) != 3 { + t.Fatal("should be 3") + } +} diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index bc7562d44e..81c06fe93c 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -294,11 +294,11 @@ export function showGetPostLinkModal(post) { }); } -export function showGetPublicLinkModal(filename) { +export function showGetPublicLinkModal(fileId) { AppDispatcher.handleViewAction({ type: ActionTypes.TOGGLE_GET_PUBLIC_LINK_MODAL, value: true, - filename + fileId }); } @@ -388,7 +388,6 @@ export function sendEphemeralPost(message, channelId) { type: Constants.POST_TYPE_EPHEMERAL, create_at: timestamp, update_at: timestamp, - filenames: [], props: {} }; diff --git a/webapp/client/client.jsx b/webapp/client/client.jsx index 439d41f78b..334f8374d8 100644 --- a/webapp/client/client.jsx +++ b/webapp/client/client.jsx @@ -113,10 +113,14 @@ export default class Client { return `${this.url}${this.urlVersion}/users`; } - getFilesRoute() { + getTeamFilesRoute() { return `${this.url}${this.urlVersion}/teams/${this.getTeamId()}/files`; } + getFileRoute(fileId) { + return `${this.url}${this.urlVersion}/files/${fileId}`; + } + getOAuthRoute() { return `${this.url}${this.urlVersion}/oauth`; } @@ -1520,34 +1524,20 @@ export default class Client { end(this.handleResponse.bind(this, 'getFlaggedPosts', success, error)); } + getFileInfosForPost(channelId, postId, success, error) { + request. + get(`${this.getChannelNeededRoute(channelId)}/posts/${postId}/get_file_infos`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getFileInfosForPost', success, error)); + } + // Routes for Files - getFileInfo(filename, success, error) { - request. - get(`${this.getFilesRoute()}/get_info${filename}`). - set(this.defaultHeaders). - type('application/json'). - accept('application/json'). - end(this.handleResponse.bind(this, 'getFileInfo', success, error)); - } - - getPublicLink(filename, success, error) { - const data = { - filename - }; - - request. - post(`${this.getFilesRoute()}/get_public_link`). - set(this.defaultHeaders). - type('application/json'). - accept('application/json'). - send(data). - end(this.handleResponse.bind(this, 'getPublicLink', success, error)); - } - uploadFile(file, filename, channelId, clientId, success, error) { return request. - post(`${this.getFilesRoute()}/upload`). + post(`${this.getTeamFilesRoute()}/upload`). set(this.defaultHeaders). attach('files', file, filename). field('channel_id', channelId). @@ -1556,6 +1546,51 @@ export default class Client { end(this.handleResponse.bind(this, 'uploadFile', success, error)); } + getFile(fileId, success, error) { + request. + get(`${this.getFileRoute(fileId)}/get`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getFile', success, error)); + } + + getFileThumbnail(fileId, success, error) { + request. + get(`${this.getFileRoute(fileId)}/get_thumbnail`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getFileThumbnail', success, error)); + } + + getFilePreview(fileId, success, error) { + request. + get(`${this.getFileRoute(fileId)}/get`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getFilePreview', success, error)); + } + + getFileInfo(fileId, success, error) { + request. + get(`${this.getFileRoute(fileId)}/get_info`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getFileInfo', success, error)); + } + + getPublicLink(fileId, success, error) { + request. + get(`${this.getFileRoute(fileId)}/get_public_link`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + end(this.handleResponse.bind(this, 'getPublicLink', success, error)); + } + // Routes for OAuth registerOAuthApp(app, success, error) { diff --git a/webapp/components/audio_video_preview.jsx b/webapp/components/audio_video_preview.jsx index dd2e910b39..4956900a95 100644 --- a/webapp/components/audio_video_preview.jsx +++ b/webapp/components/audio_video_preview.jsx @@ -76,10 +76,8 @@ export default class AudioVideoPreview extends React.Component { if (!this.state.canPlay) { return ( ); } @@ -94,7 +92,7 @@ export default class AudioVideoPreview extends React.Component { // add a key to the video to prevent React from using an old video source while a new one is loading return (