1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-12 09:04:14 +02:00
focalboard/server/app/files.go

69 lines
1.7 KiB
Go
Raw Normal View History

package app
import (
"fmt"
"io"
"path/filepath"
"strings"
"github.com/mattermost/focalboard/server/services/mlog"
2021-01-27 00:13:46 +02:00
"github.com/mattermost/focalboard/server/utils"
"github.com/mattermost/mattermost-server/v5/shared/filestore"
)
func (a *App) SaveFile(reader io.Reader, workspaceID, rootID, filename string) (string, error) {
// NOTE: File extension includes the dot
fileExtension := strings.ToLower(filepath.Ext(filename))
if fileExtension == ".jpeg" {
fileExtension = ".jpg"
}
createdFilename := fmt.Sprintf(`%s%s`, utils.CreateGUID(), fileExtension)
2021-03-30 19:06:11 +02:00
filePath := filepath.Join(workspaceID, rootID, createdFilename)
_, appErr := a.filesBackend.WriteFile(reader, filePath)
if appErr != nil {
return "", fmt.Errorf("unable to store the file in the files storage: %w", appErr)
}
return createdFilename, nil
}
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
if workspaceID == "0" && !exists {
oldExists, err2 := a.filesBackend.FileExists(filename)
if err2 != nil {
return nil, err2
}
if oldExists {
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 {
a.logger.Debug("Moved file",
mlog.String("old", filename),
mlog.String("new", filePath),
)
2021-04-01 00:30:25 +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
}