2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-09 13:19:03 +01:00
|
|
|
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2020-10-21 11:32:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *App) SaveFile(reader io.Reader, filename string) (string, error) {
|
|
|
|
// NOTE: File extension includes the dot
|
|
|
|
fileExtension := strings.ToLower(filepath.Ext(filename))
|
|
|
|
if fileExtension == ".jpeg" {
|
|
|
|
fileExtension = ".jpg"
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
createdFilename := fmt.Sprintf(`%s%s`, utils.CreateGUID(), fileExtension)
|
2020-10-21 11:32:13 +02:00
|
|
|
|
|
|
|
_, appErr := a.filesBackend.WriteFile(reader, createdFilename)
|
|
|
|
if appErr != nil {
|
|
|
|
return "", errors.New("unable to store the file in the files storage")
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-02-23 20:42:28 +01:00
|
|
|
return createdFilename, nil
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetFilePath(filename string) string {
|
|
|
|
folderPath := a.config.FilesPath
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 11:32:13 +02:00
|
|
|
return filepath.Join(folderPath, filename)
|
|
|
|
}
|