2020-10-21 11:32:13 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-04-01 00:30:25 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-10-21 11:32:13 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
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 13:19:03 +01: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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-30 01:27:35 +02:00
|
|
|
func (a *App) GetFilePath(workspaceID, rootID, filename string) string {
|
2020-10-21 11:32:13 +02:00
|
|
|
folderPath := a.config.FilesPath
|
2021-03-30 01:27:35 +02:00
|
|
|
rootPath := filepath.Join(folderPath, workspaceID, rootID)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-04-01 00:30:25 +02:00
|
|
|
filePath := filepath.Join(rootPath, filename)
|
|
|
|
|
|
|
|
// FIXUP: Check the deprecated old location
|
|
|
|
if workspaceID == "0" && !fileExists(filePath) {
|
|
|
|
oldFilePath := filepath.Join(folderPath, filename)
|
|
|
|
if fileExists(oldFilePath) {
|
|
|
|
err := os.Rename(oldFilePath, filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR moving old file from '%s' to '%s'", oldFilePath, filePath)
|
|
|
|
} else {
|
|
|
|
log.Printf("Moved old file from '%s' to '%s'", oldFilePath, filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return !os.IsNotExist(err)
|
2020-10-21 11:32:13 +02:00
|
|
|
}
|