Commit eb1a00ef authored by Chris's avatar Chris Committed by Jonathan

Reorganize file util functionality (#7848)

* reorganize file util functionality

* fix api test compilation

* fix rebase issue
parent ef69d93a
...@@ -16,7 +16,6 @@ import ( ...@@ -16,7 +16,6 @@ import (
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
) )
...@@ -51,7 +50,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -51,7 +50,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if emojiInterface := einterfaces.GetEmojiInterface(); emojiInterface != nil && if emojiInterface := c.App.Emoji; emojiInterface != nil &&
!emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) { !emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) {
c.Err = model.NewAppError("createEmoji", "api.emoji.create.permissions.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized) c.Err = model.NewAppError("createEmoji", "api.emoji.create.permissions.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized)
return return
...@@ -106,7 +105,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -106,7 +105,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
if imageData := m.File["image"]; len(imageData) == 0 { if imageData := m.File["image"]; len(imageData) == 0 {
c.SetInvalidParam("createEmoji", "image") c.SetInvalidParam("createEmoji", "image")
return return
} else if err := app.UploadEmojiImage(emoji.Id, imageData[0]); err != nil { } else if err := c.App.UploadEmojiImage(emoji.Id, imageData[0]); err != nil {
c.Err = err c.Err = err
return return
} }
......
...@@ -272,7 +272,7 @@ func TestDeleteEmoji(t *testing.T) { ...@@ -272,7 +272,7 @@ func TestDeleteEmoji(t *testing.T) {
func createTestEmoji(t *testing.T, a *app.App, emoji *model.Emoji, imageData []byte) *model.Emoji { func createTestEmoji(t *testing.T, a *app.App, emoji *model.Emoji, imageData []byte) *model.Emoji {
emoji = store.Must(a.Srv.Store.Emoji().Save(emoji)).(*model.Emoji) emoji = store.Must(a.Srv.Store.Emoji().Save(emoji)).(*model.Emoji)
if err := utils.WriteFile(imageData, "emoji/"+emoji.Id+"/image"); err != nil { if err := a.WriteFile(imageData, "emoji/"+emoji.Id+"/image"); err != nil {
store.Must(a.Srv.Store.Emoji().Delete(emoji.Id, time.Now().Unix())) store.Must(a.Srv.Store.Emoji().Delete(emoji.Id, time.Now().Unix()))
t.Fatalf("failed to write image: %v", err.Error()) t.Fatalf("failed to write image: %v", err.Error())
} }
......
...@@ -95,7 +95,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -95,7 +95,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.Path); err != nil { if data, err := c.App.ReadFile(info.Path); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil { } else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil {
...@@ -116,7 +116,7 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -116,7 +116,7 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.ThumbnailPath); err != nil { if data, err := c.App.ReadFile(info.ThumbnailPath); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, data, w, r); err != nil { } else if err := writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, data, w, r); err != nil {
...@@ -137,7 +137,7 @@ func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -137,7 +137,7 @@ func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.PreviewPath); err != nil { if data, err := c.App.ReadFile(info.PreviewPath); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, data, w, r); err != nil { } else if err := writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, data, w, r); err != nil {
...@@ -186,7 +186,7 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -186,7 +186,7 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.Path); err != nil { if data, err := c.App.ReadFile(info.Path); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil { } else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil {
...@@ -277,7 +277,7 @@ func getPublicFileOld(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -277,7 +277,7 @@ func getPublicFileOld(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.Path); err != nil { if data, err := c.App.ReadFile(info.Path); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil { } else if err := writeFileResponse(info.Name, info.MimeType, data, w, r); err != nil {
......
...@@ -849,7 +849,7 @@ func TestGetInfoForFilename(t *testing.T) { ...@@ -849,7 +849,7 @@ func TestGetInfoForFilename(t *testing.T) {
date := time.Now().Format("20060102") date := time.Now().Format("20060102")
if info := app.GetInfoForFilename(post1, team1.Id, post1.Filenames[0]); info == nil { if info := th.App.GetInfoForFilename(post1, team1.Id, post1.Filenames[0]); info == nil {
t.Fatal("info shouldn't be nil") t.Fatal("info shouldn't be nil")
} else if info.Id == "" { } else if info.Id == "" {
t.Fatal("info.Id shouldn't be empty") t.Fatal("info.Id shouldn't be empty")
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
) )
...@@ -31,7 +30,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -31,7 +30,7 @@ func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if emojiInterface := einterfaces.GetEmojiInterface(); emojiInterface != nil && if emojiInterface := c.App.Emoji; emojiInterface != nil &&
!emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) { !emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) {
c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized) c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized)
return return
......
...@@ -123,7 +123,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -123,7 +123,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
data, err := utils.ReadFile(info.Path) data, err := c.App.ReadFile(info.Path)
if err != nil { if err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
...@@ -164,7 +164,7 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -164,7 +164,7 @@ func getFileThumbnail(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.ThumbnailPath); err != nil { if data, err := c.App.ReadFile(info.ThumbnailPath); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, data, forceDownload, w, r); err != nil { } else if err := writeFileResponse(info.Name, THUMBNAIL_IMAGE_TYPE, data, forceDownload, w, r); err != nil {
...@@ -233,7 +233,7 @@ func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -233,7 +233,7 @@ func getFilePreview(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.PreviewPath); err != nil { if data, err := c.App.ReadFile(info.PreviewPath); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, data, forceDownload, w, r); err != nil { } else if err := writeFileResponse(info.Name, PREVIEW_IMAGE_TYPE, data, forceDownload, w, r); err != nil {
...@@ -294,7 +294,7 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -294,7 +294,7 @@ func getPublicFile(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if data, err := utils.ReadFile(info.Path); err != nil { if data, err := c.App.ReadFile(info.Path); err != nil {
c.Err = err c.Err = err
c.Err.StatusCode = http.StatusNotFound c.Err.StatusCode = http.StatusNotFound
} else if err := writeFileResponse(info.Name, info.MimeType, data, true, w, r); err != nil { } else if err := writeFileResponse(info.Name, info.MimeType, data, true, w, r); err != nil {
......
...@@ -43,6 +43,7 @@ type App struct { ...@@ -43,6 +43,7 @@ type App struct {
Compliance einterfaces.ComplianceInterface Compliance einterfaces.ComplianceInterface
DataRetention einterfaces.DataRetentionInterface DataRetention einterfaces.DataRetentionInterface
Elasticsearch einterfaces.ElasticsearchInterface Elasticsearch einterfaces.ElasticsearchInterface
Emoji einterfaces.EmojiInterface
Ldap einterfaces.LdapInterface Ldap einterfaces.LdapInterface
Metrics einterfaces.MetricsInterface Metrics einterfaces.MetricsInterface
Mfa einterfaces.MfaInterface Mfa einterfaces.MfaInterface
...@@ -133,6 +134,12 @@ func RegisterAccountMigrationInterface(f func(*App) einterfaces.AccountMigration ...@@ -133,6 +134,12 @@ func RegisterAccountMigrationInterface(f func(*App) einterfaces.AccountMigration
accountMigrationInterface = f accountMigrationInterface = f
} }
var brandInterface func(*App) einterfaces.BrandInterface
func RegisterBrandInterface(f func(*App) einterfaces.BrandInterface) {
brandInterface = f
}
var clusterInterface func(*App) einterfaces.ClusterInterface var clusterInterface func(*App) einterfaces.ClusterInterface
func RegisterClusterInterface(f func(*App) einterfaces.ClusterInterface) { func RegisterClusterInterface(f func(*App) einterfaces.ClusterInterface) {
...@@ -151,6 +158,18 @@ func RegisterDataRetentionInterface(f func(*App) einterfaces.DataRetentionInterf ...@@ -151,6 +158,18 @@ func RegisterDataRetentionInterface(f func(*App) einterfaces.DataRetentionInterf
dataRetentionInterface = f dataRetentionInterface = f
} }
var elasticsearchInterface func(*App) einterfaces.ElasticsearchInterface
func RegisterElasticsearchInterface(f func(*App) einterfaces.ElasticsearchInterface) {
elasticsearchInterface = f
}
var emojiInterface func(*App) einterfaces.EmojiInterface
func RegisterEmojiInterface(f func(*App) einterfaces.EmojiInterface) {
emojiInterface = f
}
var jobsDataRetentionJobInterface func(*App) ejobs.DataRetentionJobInterface var jobsDataRetentionJobInterface func(*App) ejobs.DataRetentionJobInterface
func RegisterJobsDataRetentionJobInterface(f func(*App) ejobs.DataRetentionJobInterface) { func RegisterJobsDataRetentionJobInterface(f func(*App) ejobs.DataRetentionJobInterface) {
...@@ -203,14 +222,21 @@ func (a *App) initEnterprise() { ...@@ -203,14 +222,21 @@ func (a *App) initEnterprise() {
if accountMigrationInterface != nil { if accountMigrationInterface != nil {
a.AccountMigration = accountMigrationInterface(a) a.AccountMigration = accountMigrationInterface(a)
} }
a.Brand = einterfaces.GetBrandInterface() if brandInterface != nil {
a.Brand = brandInterface(a)
}
if clusterInterface != nil { if clusterInterface != nil {
a.Cluster = clusterInterface(a) a.Cluster = clusterInterface(a)
} }
if complianceInterface != nil { if complianceInterface != nil {
a.Compliance = complianceInterface(a) a.Compliance = complianceInterface(a)
} }
a.Elasticsearch = einterfaces.GetElasticsearchInterface() if elasticsearchInterface != nil {
a.Elasticsearch = elasticsearchInterface(a)
}
if emojiInterface != nil {
a.Emoji = emojiInterface(a)
}
if ldapInterface != nil { if ldapInterface != nil {
a.Ldap = ldapInterface(a) a.Ldap = ldapInterface(a)
utils.AddConfigListener(func(_, cfg *model.Config) { utils.AddConfigListener(func(_, cfg *model.Config) {
......
...@@ -51,7 +51,7 @@ func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartIma ...@@ -51,7 +51,7 @@ func (a *App) CreateEmoji(sessionUserId string, emoji *model.Emoji, multiPartIma
if imageData := multiPartImageData.File["image"]; len(imageData) == 0 { if imageData := multiPartImageData.File["image"]; len(imageData) == 0 {
err := model.NewAppError("Context", "api.context.invalid_body_param.app_error", map[string]interface{}{"Name": "createEmoji"}, "", http.StatusBadRequest) err := model.NewAppError("Context", "api.context.invalid_body_param.app_error", map[string]interface{}{"Name": "createEmoji"}, "", http.StatusBadRequest)
return nil, err return nil, err
} else if err := UploadEmojiImage(emoji.Id, imageData[0]); err != nil { } else if err := a.UploadEmojiImage(emoji.Id, imageData[0]); err != nil {
return nil, err return nil, err
} }
...@@ -74,7 +74,7 @@ func (a *App) GetEmojiList(page, perPage int) ([]*model.Emoji, *model.AppError) ...@@ -74,7 +74,7 @@ func (a *App) GetEmojiList(page, perPage int) ([]*model.Emoji, *model.AppError)
} }
} }
func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError { func (a *App) UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppError {
file, err := imageData.Open() file, err := imageData.Open()
if err != nil { if err != nil {
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "", http.StatusBadRequest) return model.NewAppError("uploadEmojiImage", "api.emoji.upload.open.app_error", nil, "", http.StatusBadRequest)
...@@ -100,7 +100,7 @@ func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppErro ...@@ -100,7 +100,7 @@ func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppErro
if err := gif.EncodeAll(newbuf, resized_gif); err != nil { if err := gif.EncodeAll(newbuf, resized_gif); err != nil {
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest) return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.gif_encode_error", nil, "", http.StatusBadRequest)
} }
if err := utils.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil { if err := a.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
return err return err
} }
} }
...@@ -112,18 +112,14 @@ func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppErro ...@@ -112,18 +112,14 @@ func UploadEmojiImage(id string, imageData *multipart.FileHeader) *model.AppErro
if err := png.Encode(newbuf, resized_image); err != nil { if err := png.Encode(newbuf, resized_image); err != nil {
return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest) return model.NewAppError("uploadEmojiImage", "api.emoji.upload.large_image.encode_error", nil, "", http.StatusBadRequest)
} }
if err := utils.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil { if err := a.WriteFile(newbuf.Bytes(), getEmojiImagePath(id)); err != nil {
return err return err
} }
} }
} }
} else {
if err := utils.WriteFile(buf.Bytes(), getEmojiImagePath(id)); err != nil {
return err
}
} }
return nil return a.WriteFile(buf.Bytes(), getEmojiImagePath(id))
} }
func (a *App) DeleteEmoji(emoji *model.Emoji) *model.AppError { func (a *App) DeleteEmoji(emoji *model.Emoji) *model.AppError {
...@@ -131,7 +127,7 @@ func (a *App) DeleteEmoji(emoji *model.Emoji) *model.AppError { ...@@ -131,7 +127,7 @@ func (a *App) DeleteEmoji(emoji *model.Emoji) *model.AppError {
return err return err
} }
deleteEmojiImage(emoji.Id) a.deleteEmojiImage(emoji.Id)
a.deleteReactionsForEmoji(emoji.Name) a.deleteReactionsForEmoji(emoji.Name)
return nil return nil
} }
...@@ -158,7 +154,7 @@ func (a *App) GetEmojiImage(emojiId string) (imageByte []byte, imageType string, ...@@ -158,7 +154,7 @@ func (a *App) GetEmojiImage(emojiId string) (imageByte []byte, imageType string,
} else { } else {
var img []byte var img []byte
if data, err := utils.ReadFile(getEmojiImagePath(emojiId)); err != nil { if data, err := a.ReadFile(getEmojiImagePath(emojiId)); err != nil {
return nil, "", model.NewAppError("getEmojiImage", "api.emoji.get_image.read.app_error", nil, err.Error(), http.StatusNotFound) return nil, "", model.NewAppError("getEmojiImage", "api.emoji.get_image.read.app_error", nil, err.Error(), http.StatusNotFound)
} else { } else {
img = data img = data
...@@ -217,8 +213,8 @@ func imageToPaletted(img image.Image) *image.Paletted { ...@@ -217,8 +213,8 @@ func imageToPaletted(img image.Image) *image.Paletted {
return pm return pm
} }
func deleteEmojiImage(id string) { func (a *App) deleteEmojiImage(id string) {
if err := utils.MoveFile(getEmojiImagePath(id), "emoji/"+id+"/image_deleted"); err != nil { if err := a.MoveFile(getEmojiImagePath(id), "emoji/"+id+"/image_deleted"); err != nil {
l4g.Error("Failed to rename image when deleting emoji %v", id) l4g.Error("Failed to rename image when deleting emoji %v", id)
} }
} }
......
...@@ -57,7 +57,43 @@ const ( ...@@ -57,7 +57,43 @@ const (
IMAGE_PREVIEW_PIXEL_WIDTH = 1024 IMAGE_PREVIEW_PIXEL_WIDTH = 1024
) )
func GetInfoForFilename(post *model.Post, teamId string, filename string) *model.FileInfo { func (a *App) FileBackend() (utils.FileBackend, *model.AppError) {
return utils.NewFileBackend(&a.Config().FileSettings)
}
func (a *App) ReadFile(path string) ([]byte, *model.AppError) {
backend, err := a.FileBackend()
if err != nil {
return nil, err
}
return backend.ReadFile(path)
}
func (a *App) MoveFile(oldPath, newPath string) *model.AppError {
backend, err := a.FileBackend()
if err != nil {
return err
}
return backend.MoveFile(oldPath, newPath)
}
func (a *App) WriteFile(f []byte, path string) *model.AppError {
backend, err := a.FileBackend()
if err != nil {
return err
}
return backend.WriteFile(f, path)
}
func (a *App) RemoveFile(path string) *model.AppError {
backend, err := a.FileBackend()
if err != nil {
return err
}
return backend.RemoveFile(path)
}
func (a *App) GetInfoForFilename(post *model.Post, teamId string, filename string) *model.FileInfo {
// Find the path from the Filename of the form /{channelId}/{userId}/{uid}/{nameWithExtension} // Find the path from the Filename of the form /{channelId}/{userId}/{uid}/{nameWithExtension}
split := strings.SplitN(filename, "/", 5) split := strings.SplitN(filename, "/", 5)
if len(split) < 5 { if len(split) < 5 {
...@@ -79,7 +115,7 @@ func GetInfoForFilename(post *model.Post, teamId string, filename string) *model ...@@ -79,7 +115,7 @@ func GetInfoForFilename(post *model.Post, teamId string, filename string) *model
// Open the file and populate the fields of the FileInfo // Open the file and populate the fields of the FileInfo
var info *model.FileInfo var info *model.FileInfo
if data, err := utils.ReadFile(path); err != nil { if data, err := a.ReadFile(path); err != nil {
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.file_not_found.error"), post.Id, filename, path, err) l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.file_not_found.error"), post.Id, filename, path, err)
return nil return nil
} else { } else {
...@@ -121,7 +157,7 @@ func (a *App) FindTeamIdForFilename(post *model.Post, filename string) string { ...@@ -121,7 +157,7 @@ func (a *App) FindTeamIdForFilename(post *model.Post, filename string) string {
} else { } else {
for _, team := range teams { for _, team := range teams {
path := fmt.Sprintf("teams/%s/channels/%s/users/%s/%s/%s", team.Id, post.ChannelId, post.UserId, id, name) path := fmt.Sprintf("teams/%s/channels/%s/users/%s/%s/%s", team.Id, post.ChannelId, post.UserId, id, name)
if _, err := utils.ReadFile(path); err == nil { if _, err := a.ReadFile(path); err == nil {
// Found the team that this file was posted from // Found the team that this file was posted from
return team.Id return team.Id
} }
...@@ -168,7 +204,7 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo { ...@@ -168,7 +204,7 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.team_id.error"), post.Id, filenames) l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.team_id.error"), post.Id, filenames)
} else { } else {
for _, filename := range filenames { for _, filename := range filenames {
info := GetInfoForFilename(post, teamId, filename) info := a.GetInfoForFilename(post, teamId, filename)
if info == nil { if info == nil {
continue continue
} }
...@@ -286,7 +322,7 @@ func (a *App) UploadFiles(teamId string, channelId string, userId string, fileHe ...@@ -286,7 +322,7 @@ func (a *App) UploadFiles(teamId string, channelId string, userId string, fileHe
} }
} }
HandleImages(previewPathList, thumbnailPathList, imageDataList) a.HandleImages(previewPathList, thumbnailPathList, imageDataList)
return resStruct, nil return resStruct, nil
} }
...@@ -321,7 +357,7 @@ func (a *App) DoUploadFile(now time.Time, rawTeamId string, rawChannelId string, ...@@ -321,7 +357,7 @@ func (a *App) DoUploadFile(now time.Time, rawTeamId string, rawChannelId string,
info.ThumbnailPath = pathPrefix + nameWithoutExtension + "_thumb.jpg" info.ThumbnailPath = pathPrefix + nameWithoutExtension + "_thumb.jpg"
} }
if err := utils.WriteFile(data, info.Path); err != nil { if err := a.WriteFile(data, info.Path); err != nil {
return nil, err return nil, err
} }
...@@ -332,7 +368,7 @@ func (a *App) DoUploadFile(now time.Time, rawTeamId string, rawChannelId string, ...@@ -332,7 +368,7 @@ func (a *App) DoUploadFile(now time.Time, rawTeamId string, rawChannelId string,
return info, nil return info, nil
} }
func HandleImages(previewPathList []string, thumbnailPathList []string, fileData [][]byte) { func (a *App) HandleImages(previewPathList []string, thumbnailPathList []string, fileData [][]byte) {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
for i := range fileData { for i := range fileData {
...@@ -341,12 +377,12 @@ func HandleImages(previewPathList []string, thumbnailPathList []string, fileData ...@@ -341,12 +377,12 @@ func HandleImages(previewPathList []string, thumbnailPathList []string, fileData
wg.Add(2) wg.Add(2)
go func(img *image.Image, path string, width int, height int) { go func(img *image.Image, path string, width int, height int) {
defer wg.Done() defer wg.Done()
generateThumbnailImage(*img, path, width, height) a.generateThumbnailImage(*img, path, width, height)
}(img, thumbnailPathList[i], width, height) }(img, thumbnailPathList[i], width, height)
go func(img *image.Image, path string, width int) { go func(img *image.Image, path string, width int) {
defer wg.Done() defer wg.Done()
generatePreviewImage(*img, path, width) a.generatePreviewImage(*img, path, width)
}(img, previewPathList[i], width) }(img, previewPathList[i], width)
} }
} }
...@@ -417,7 +453,7 @@ func getImageOrientation(input io.Reader) (int, error) { ...@@ -417,7 +453,7 @@ func getImageOrientation(input io.Reader) (int, error) {
} }
} }
func generateThumbnailImage(img image.Image, thumbnailPath string, width int, height int) { func (a *App) generateThumbnailImage(img image.Image, thumbnailPath string, width int, height int) {
thumbWidth := float64(IMAGE_THUMBNAIL_PIXEL_WIDTH) thumbWidth := float64(IMAGE_THUMBNAIL_PIXEL_WIDTH)
thumbHeight := float64(IMAGE_THUMBNAIL_PIXEL_HEIGHT) thumbHeight := float64(IMAGE_THUMBNAIL_PIXEL_HEIGHT)
imgWidth := float64(width) imgWidth := float64(width)
...@@ -438,13 +474,13 @@ func generateThumbnailImage(img image.Image, thumbnailPath string, width int, he ...@@ -438,13 +474,13 @@ func generateThumbnailImage(img image.Image, thumbnailPath string, width int, he
return return
} }
if err := utils.WriteFile(buf.Bytes(), thumbnailPath); err != nil { if err := a.WriteFile(buf.Bytes(), thumbnailPath); err != nil {
l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), thumbnailPath, err) l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), thumbnailPath, err)
return return
} }
} }
func generatePreviewImage(img image.Image, previewPath string, width int) { func (a *App) generatePreviewImage(img image.Image, previewPath string, width int) {
var preview image.Image var preview image.Image
if width > IMAGE_PREVIEW_PIXEL_WIDTH { if width > IMAGE_PREVIEW_PIXEL_WIDTH {
...@@ -460,7 +496,7 @@ func generatePreviewImage(img image.Image, previewPath string, width int) { ...@@ -460,7 +496,7 @@ func generatePreviewImage(img image.Image, previewPath string, width int) {
return return
} }
if err := utils.WriteFile(buf.Bytes(), previewPath); err != nil { if err := a.WriteFile(buf.Bytes(), previewPath); err != nil {
l4g.Error(utils.T("api.file.handle_images_forget.upload_preview.error"), previewPath, err) l4g.Error(utils.T("api.file.handle_images_forget.upload_preview.error"), previewPath, err)
return return
} }
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"time" "time"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
) )
func TestGeneratePublicLinkHash(t *testing.T) { func TestGeneratePublicLinkHash(t *testing.T) {
...@@ -51,7 +50,7 @@ func TestDoUploadFile(t *testing.T) { ...@@ -51,7 +50,7 @@ func TestDoUploadFile(t *testing.T) {
} else { } else {
defer func() { defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id) <-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
utils.RemoveFile(info1.Path) th.App.RemoveFile(info1.Path)
}() }()
} }
...@@ -65,7 +64,7 @@ func TestDoUploadFile(t *testing.T) { ...@@ -65,7 +64,7 @@ func TestDoUploadFile(t *testing.T) {
} else { } else {
defer func() { defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id) <-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
utils.RemoveFile(info2.Path) th.App.RemoveFile(info2.Path)
}() }()
} }
...@@ -79,7 +78,7 @@ func TestDoUploadFile(t *testing.T) { ...@@ -79,7 +78,7 @@ func TestDoUploadFile(t *testing.T) {
} else { } else {
defer func() { defer func() {
<-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id) <-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
utils.RemoveFile(info3.Path) th.App.RemoveFile(info3.Path)
}() }()
} }
...@@ -93,7 +92,7 @@ func TestDoUploadFile(t *testing.T) { ...@@ -93,7 +92,7 @@ func TestDoUploadFile(t *testing.T) {
} else { } else {
defer func() { defer func() {