Moved file attachments to be stored in data/channels instead of data/teams/ID/channels (#3416)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
b9cf09b5a4
Коммит
ed75dfc6c0
37
api/file.go
37
api/file.go
@@ -149,7 +149,7 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
path := "teams/" + c.TeamId + "/channels/" + channelId + "/users/" + c.Session.UserId + "/" + uid + "/" + filename
|
path := "channels/" + channelId + "/users/" + c.Session.UserId + "/" + uid + "/" + filename
|
||||||
|
|
||||||
if err := WriteFile(buf.Bytes(), path); err != nil {
|
if err := WriteFile(buf.Bytes(), path); err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
@@ -172,7 +172,7 @@ func uploadFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleImages(filenames []string, fileData [][]byte, teamId, channelId, userId string) {
|
func handleImages(filenames []string, fileData [][]byte, teamId, channelId, userId string) {
|
||||||
dest := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/"
|
dest := "channels/" + channelId + "/users/" + userId + "/"
|
||||||
|
|
||||||
for i, filename := range filenames {
|
for i, filename := range filenames {
|
||||||
name := filename[:strings.LastIndex(filename, ".")]
|
name := filename[:strings.LastIndex(filename, ".")]
|
||||||
@@ -318,18 +318,21 @@ func getFileInfo(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cchan := Srv.Store.Channel().CheckPermissionsTo(c.TeamId, channelId, c.Session.UserId)
|
cchan := Srv.Store.Channel().CheckPermissionsToNoTeam(channelId, c.Session.UserId)
|
||||||
|
|
||||||
path := "teams/" + c.TeamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
path := "channels/" + channelId + "/users/" + userId + "/" + filename
|
||||||
var info *model.FileInfo
|
var info *model.FileInfo
|
||||||
|
|
||||||
if cached, ok := fileInfoCache.Get(path); ok {
|
if cached, ok := fileInfoCache.Get(path); ok {
|
||||||
info = cached.(*model.FileInfo)
|
info = cached.(*model.FileInfo)
|
||||||
} else {
|
} else {
|
||||||
fileData := make(chan []byte)
|
err, bytes := getFileData(c.TeamId, channelId, userId, filename)
|
||||||
go readFile(path, fileData)
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
newInfo, err := model.GetInfoForBytes(filename, <-fileData)
|
newInfo, err := model.GetInfoForBytes(filename, bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
return
|
return
|
||||||
@@ -356,7 +359,7 @@ func getFile(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
userId := params["user_id"]
|
userId := params["user_id"]
|
||||||
filename := params["filename"]
|
filename := params["filename"]
|
||||||
|
|
||||||
if !c.HasPermissionsToChannel(Srv.Store.Channel().CheckPermissionsTo(teamId, channelId, userId), "getFile") {
|
if !c.HasPermissionsToChannel(Srv.Store.Channel().CheckPermissionsToNoTeam(channelId, userId), "getFile") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,10 +417,6 @@ func getFileData(teamId string, channelId string, userId string, filename string
|
|||||||
return err, nil
|
return err, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(teamId) != 26 {
|
|
||||||
return NewInvalidParamError("getFileData", "team_id"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(channelId) != 26 {
|
if len(channelId) != 26 {
|
||||||
return NewInvalidParamError("getFileData", "channel_id"), nil
|
return NewInvalidParamError("getFileData", "channel_id"), nil
|
||||||
}
|
}
|
||||||
@@ -430,17 +429,19 @@ func getFileData(teamId string, channelId string, userId string, filename string
|
|||||||
return NewInvalidParamError("getFileData", "filename"), nil
|
return NewInvalidParamError("getFileData", "filename"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
path := "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
path := "channels/" + channelId + "/users/" + userId + "/" + filename
|
||||||
|
|
||||||
fileChan := make(chan []byte)
|
// try using the new path and then fall back to the old one if that doesn't work
|
||||||
go readFile(path, fileChan)
|
if bytes, readErr := ReadFile(path); readErr == nil {
|
||||||
|
return nil, bytes
|
||||||
|
} else if bytes, readErr := ReadFile("teams/" + teamId + "/" + path); readErr == nil {
|
||||||
|
return nil, bytes
|
||||||
|
} else {
|
||||||
|
l4g.Error(readErr)
|
||||||
|
|
||||||
if bytes := <-fileChan; bytes == nil {
|
|
||||||
err := model.NewLocAppError("writeFileResponse", "api.file.get_file.not_found.app_error", nil, "path="+path)
|
err := model.NewLocAppError("writeFileResponse", "api.file.get_file.not_found.app_error", nil, "path="+path)
|
||||||
err.StatusCode = http.StatusNotFound
|
err.StatusCode = http.StatusNotFound
|
||||||
return err, nil
|
return err, nil
|
||||||
} else {
|
|
||||||
return nil, bytes
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import (
|
|||||||
func TestUploadFile(t *testing.T) {
|
func TestUploadFile(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
Client := th.BasicClient
|
Client := th.BasicClient
|
||||||
team := th.BasicTeam
|
|
||||||
user := th.BasicUser
|
user := th.BasicUser
|
||||||
channel := th.BasicChannel
|
channel := th.BasicChannel
|
||||||
|
|
||||||
@@ -84,17 +83,17 @@ func TestUploadFile(t *testing.T) {
|
|||||||
// wait a bit for files to ready
|
// wait a bit for files to ready
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -109,17 +108,17 @@ func TestUploadFile(t *testing.T) {
|
|||||||
// wait a bit for files to ready
|
// wait a bit for files to ready
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
path := utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
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"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
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"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
t.Fatal("Couldn't remove file at " + path)
|
||||||
}
|
}
|
||||||
@@ -133,7 +132,6 @@ func TestUploadFile(t *testing.T) {
|
|||||||
func TestGetFile(t *testing.T) {
|
func TestGetFile(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
Client := th.BasicClient
|
Client := th.BasicClient
|
||||||
team := th.BasicTeam
|
|
||||||
user := th.BasicUser
|
user := th.BasicUser
|
||||||
channel := th.BasicChannel
|
channel := th.BasicChannel
|
||||||
|
|
||||||
@@ -207,17 +205,17 @@ func TestGetFile(t *testing.T) {
|
|||||||
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
||||||
fileId := strings.Split(filename, ".")[0]
|
fileId := strings.Split(filename, ".")[0]
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
err = bucket.Del("channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -226,17 +224,17 @@ func TestGetFile(t *testing.T) {
|
|||||||
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
||||||
fileId := strings.Split(filename, ".")[0]
|
fileId := strings.Split(filename, ".")[0]
|
||||||
|
|
||||||
path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
path := utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
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"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
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"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
t.Fatal("Couldn't remove file at " + path)
|
t.Fatal("Couldn't remove file at " + path)
|
||||||
}
|
}
|
||||||
@@ -347,7 +345,7 @@ func TestGetPublicFile(t *testing.T) {
|
|||||||
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 while not logged in after salt changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil {
|
if err := cleanupTestFile(filenames[0], channel.Id, th.BasicUser.Id); err != nil {
|
||||||
t.Fatal("failed to cleanup test file", err)
|
t.Fatal("failed to cleanup test file", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -403,7 +401,7 @@ func TestGetPublicLink(t *testing.T) {
|
|||||||
|
|
||||||
th.LoginBasic()
|
th.LoginBasic()
|
||||||
|
|
||||||
if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil {
|
if err := cleanupTestFile(filenames[0], channel.Id, th.BasicUser.Id); err != nil {
|
||||||
t.Fatal("failed to cleanup test file", err)
|
t.Fatal("failed to cleanup test file", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -443,7 +441,7 @@ func uploadTestFile(Client *model.Client, channelId string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanupTestFile(fullFilename, teamId, channelId, userId string) error {
|
func cleanupTestFile(fullFilename, channelId, userId string) error {
|
||||||
filenames := strings.Split(fullFilename, "/")
|
filenames := strings.Split(fullFilename, "/")
|
||||||
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
||||||
fileId := strings.Split(filename, ".")[0]
|
fileId := strings.Split(filename, ".")[0]
|
||||||
@@ -457,29 +455,29 @@ func cleanupTestFile(fullFilename, teamId, channelId, userId string) error {
|
|||||||
s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region])
|
s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region])
|
||||||
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
||||||
|
|
||||||
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename); err != nil {
|
if err := bucket.Del("channels/" + channelId + "/users/" + userId + "/" + filename); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"); err != nil {
|
if err := bucket.Del("channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"); err != nil {
|
if err := bucket.Del("channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path := utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
path := utils.Cfg.FileSettings.Directory + "channels/" + channelId + "/users/" + userId + "/" + filename
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
return fmt.Errorf("Couldn't remove file at " + path)
|
return fmt.Errorf("Couldn't remove file at " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
return fmt.Errorf("Couldn't remove file at " + path)
|
return fmt.Errorf("Couldn't remove file at " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"
|
path = utils.Cfg.FileSettings.Directory + "channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
return fmt.Errorf("Couldn't remove file at " + path)
|
return fmt.Errorf("Couldn't remove file at " + path)
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user