Fix bug where workspace era uploaded files won't load on team era codebase (#3907)
* WIP * Added comment for explaination * Lint fix * move file if found in channel directory * lint fixes Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
parent
9e861a62a6
commit
2c27d2626e
2 changed files with 38 additions and 1 deletions
|
@ -2,12 +2,15 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattermost/focalboard/server/app"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/mattermost/focalboard/server/model"
|
"github.com/mattermost/focalboard/server/model"
|
||||||
"github.com/mattermost/focalboard/server/services/audit"
|
"github.com/mattermost/focalboard/server/services/audit"
|
||||||
|
@ -144,10 +147,26 @@ func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fileReader, err := a.app.GetFileReader(board.TeamID, boardID, filename)
|
fileReader, err := a.app.GetFileReader(board.TeamID, boardID, filename)
|
||||||
|
if err != nil && !errors.Is(err, app.ErrFileNotFound) {
|
||||||
|
a.errorResponse(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, app.ErrFileNotFound) && board.ChannelID != "" {
|
||||||
|
// prior to moving from workspaces to teams, the filepath was constructed from
|
||||||
|
// workspaceID, which is the channel ID in plugin mode.
|
||||||
|
// If a file is not found from team ID as we tried above, try looking for it via
|
||||||
|
// channel ID.
|
||||||
|
fileReader, err = a.app.GetFileReader(board.ChannelID, boardID, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.errorResponse(w, r, err)
|
a.errorResponse(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// move file to team location
|
||||||
|
// nothing to do if there is an error
|
||||||
|
_ = a.app.MoveFile(board.ChannelID, board.TeamID, boardID, filename)
|
||||||
|
}
|
||||||
|
|
||||||
defer fileReader.Close()
|
defer fileReader.Close()
|
||||||
http.ServeContent(w, r, filename, time.Now(), fileReader)
|
http.ServeContent(w, r, filename, time.Now(), fileReader)
|
||||||
auditRec.Success()
|
auditRec.Success()
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
const emptyString = "empty"
|
const emptyString = "empty"
|
||||||
|
|
||||||
var errEmptyFilename = errors.New("IsFileArchived: empty filename not allowed")
|
var errEmptyFilename = errors.New("IsFileArchived: empty filename not allowed")
|
||||||
|
var ErrFileNotFound = errors.New("file not found")
|
||||||
|
|
||||||
func (a *App) SaveFile(reader io.Reader, teamID, rootID, filename string) (string, error) {
|
func (a *App) SaveFile(reader io.Reader, teamID, rootID, filename string) (string, error) {
|
||||||
// NOTE: File extension includes the dot
|
// NOTE: File extension includes the dot
|
||||||
|
@ -111,6 +112,8 @@ func (a *App) GetFileReader(teamID, rootID, filename string) (filestore.ReadClos
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if !exists {
|
||||||
|
return nil, ErrFileNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
reader, err := a.filesBackend.Reader(filePath)
|
reader, err := a.filesBackend.Reader(filePath)
|
||||||
|
@ -120,3 +123,18 @@ func (a *App) GetFileReader(teamID, rootID, filename string) (filestore.ReadClos
|
||||||
|
|
||||||
return reader, nil
|
return reader, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) MoveFile(channelID, teamID, boardID, filename string) error {
|
||||||
|
oldPath := filepath.Join(channelID, boardID, filename)
|
||||||
|
newPath := filepath.Join(teamID, boardID, filename)
|
||||||
|
err := a.filesBackend.MoveFile(oldPath, newPath)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("ERROR moving file",
|
||||||
|
mlog.String("old", oldPath),
|
||||||
|
mlog.String("new", newPath),
|
||||||
|
mlog.Err(err),
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue