2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-09 14:19:03 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/mlog"
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-05-29 08:23:10 +02:00
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v5/shared/filestore"
|
2020-10-21 11:32:13 +02:00
|
|
|
)
|
|
|
|
|
2021-03-30 01:27:35 +02:00
|
|
|
func (a *App) SaveFile(reader io.Reader, workspaceID, rootID, filename string) (string, error) {
|
2020-10-21 11:32:13 +02:00
|
|
|
// NOTE: File extension includes the dot
|
|
|
|
fileExtension := strings.ToLower(filepath.Ext(filename))
|
|
|
|
if fileExtension == ".jpeg" {
|
|
|
|
fileExtension = ".jpg"
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:19:03 +02:00
|
|
|
createdFilename := fmt.Sprintf(`%s%s`, utils.CreateGUID(), fileExtension)
|
2021-03-30 19:06:11 +02:00
|
|
|
filePath := filepath.Join(workspaceID, rootID, createdFilename)
|
2020-10-21 11:32:13 +02:00
|
|
|
|
2021-03-30 01:27:35 +02:00
|
|
|
_, appErr := a.filesBackend.WriteFile(reader, filePath)
|
2020-10-21 11:32:13 +02:00
|
|
|
if appErr != nil {
|
2021-07-09 03:09:02 +02:00
|
|
|
return "", fmt.Errorf("unable to store the file in the files storage: %w", appErr)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-02-23 21:42:28 +02:00
|
|
|
return createdFilename, nil
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
func (a *App) GetFileReader(workspaceID, rootID, filename string) (filestore.ReadCloseSeeker, error) {
|
|
|
|
filePath := filepath.Join(workspaceID, rootID, filename)
|
|
|
|
exists, err := a.filesBackend.FileExists(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-01 00:30:25 +02:00
|
|
|
// FIXUP: Check the deprecated old location
|
2021-05-24 19:06:11 +02:00
|
|
|
if workspaceID == "0" && !exists {
|
2021-06-21 11:21:42 +02:00
|
|
|
oldExists, err2 := a.filesBackend.FileExists(filename)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
if oldExists {
|
2021-06-21 11:21:42 +02:00
|
|
|
err2 := a.filesBackend.MoveFile(filename, filePath)
|
|
|
|
if err2 != nil {
|
|
|
|
a.logger.Error("ERROR moving file",
|
|
|
|
mlog.String("old", filename),
|
|
|
|
mlog.String("new", filePath),
|
|
|
|
mlog.Err(err2),
|
|
|
|
)
|
2021-04-01 00:30:25 +02:00
|
|
|
} else {
|
2021-06-21 11:21:42 +02:00
|
|
|
a.logger.Debug("Moved file",
|
|
|
|
mlog.String("old", filename),
|
|
|
|
mlog.String("new", filePath),
|
|
|
|
)
|
2021-04-01 00:30:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
reader, err := a.filesBackend.Reader(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reader, nil
|
2021-04-01 00:30:25 +02:00
|
|
|
}
|