diff --git a/app/file.go b/app/file.go index 5be9636b46..6371386b97 100644 --- a/app/file.go +++ b/app/file.go @@ -434,8 +434,13 @@ func (a *App) UploadFiles(teamId string, channelId string, userId string, files // UploadFile uploads a single file in form of a completely constructed byte array for a channel. func (a *App) UploadFile(data []byte, channelId string, filename string) (*model.FileInfo, *model.AppError) { - info, _, appError := a.DoUploadFileExpectModification(time.Now(), "noteam", channelId, "nouser", filename, data) + _, err := a.GetChannel(channelId) + if err != nil && channelId != "" { + return nil, model.NewAppError("UploadFile", "api.file.upload_file.incorrect_channelId.app_error", + map[string]interface{}{"channelId": channelId}, "", http.StatusBadRequest) + } + info, _, appError := a.DoUploadFileExpectModification(time.Now(), "noteam", channelId, "nouser", filename, data) if appError != nil { return nil, appError } diff --git a/app/file_test.go b/app/file_test.go index 0e3e82f2b5..12fb84d1eb 100644 --- a/app/file_test.go +++ b/app/file_test.go @@ -87,14 +87,21 @@ func TestDoUploadFile(t *testing.T) { } func TestUploadFile(t *testing.T) { - th := Setup(t) + th := Setup(t).InitBasic() defer th.TearDown() - channelId := model.NewId() + channelId := th.BasicChannel.Id filename := "test" data := []byte("abcd") - info1, err := th.App.UploadFile(data, channelId, filename) + info1, err := th.App.UploadFile(data, "wrong", filename) + require.Error(t, err, "Wrong Channel ID.") + require.Nil(t, info1, "Channel ID does not exist.") + + info1, err = th.App.UploadFile(data, "", filename) + require.Nil(t, err, "empty channel IDs should be valid") + + info1, err = th.App.UploadFile(data, channelId, filename) require.Nil(t, err, "UploadFile should succeed with valid data") defer func() { th.App.Srv().Store.FileInfo().PermanentDelete(info1.Id) diff --git a/i18n/en.json b/i18n/en.json index 52ddb4c6b9..44419387a3 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1360,6 +1360,10 @@ "id": "api.file.test_connection.s3.connection.app_error", "translation": "Bad connection to S3 or minio." }, + { + "id": "api.file.upload_file.incorrect_channelId.app_error", + "translation": "Unable to upload the file. Incorrect channel ID: {{.channelId}}" + }, { "id": "api.file.upload_file.incorrect_number_of_client_ids.app_error", "translation": "Unable to upload file(s). Have {{.NumClientIds}} client_ids for {{.NumFiles}} files."