Skip to content

Commit

Permalink
- define error response for the ui
Browse files Browse the repository at this point in the history
- dont serialize integrations empty fields
  • Loading branch information
ddvk committed Nov 18, 2024
1 parent 3371a3c commit bba2bdf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (app *App) integrationsGetFile(c *gin.Context) {

reader, size, err := integrationProvider.Download(fileID)
if err != nil {
log.Error(err)
log.Errorf("cannot download file %s, %v", fileID, err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
Expand Down
11 changes: 6 additions & 5 deletions internal/integrations/localfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/ddvk/rmfakecloud/internal/messages"
"github.com/ddvk/rmfakecloud/internal/model"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -63,7 +63,7 @@ func (d *localFS) List(folder string, depth int) (*messages.IntegrationFolder, e

startPath := path.Clean(folder)

logrus.Info("[localfs] query for: ", startPath, " depth: ", depth)
log.Infof("[localfs] query for '%s' depth %d: ", startPath, depth)

err := visitDir(d.rootPath, startPath, depth, response, func(s string) ([]fs.FileInfo, error) {
di, err := os.ReadDir(s)
Expand All @@ -74,7 +74,7 @@ func (d *localFS) List(folder string, depth int) (*messages.IntegrationFolder, e
for _, d := range di {
fi, err := d.Info()
if err != nil {
logrus.Warnf("[localfs] cant get fileinfo %v", err)
log.Warnf("[localfs] cant get fileinfo %v", err)
continue
}
result = append(result, fi)
Expand All @@ -95,6 +95,7 @@ func (d *localFS) Download(fileID string) (io.ReadCloser, int64, error) {
}

localPath := path.Join(d.rootPath, path.Clean(decoded))
log.Infof("[localfs] getting local file %s", localPath)

st, err := os.Stat(localPath)
if err != nil {
Expand All @@ -115,11 +116,11 @@ func (d *localFS) Upload(folderID, name, fileType string, reader io.ReadCloser)
}
//TODO: more cleanup and checks
filePath := path.Clean(path.Join(folder, name+"."+fileType))
logrus.Trace(loggerfs, "Cleaned: ", filePath)
log.Trace(loggerfs, "Cleaned: ", filePath)

fullPath := path.Join(d.rootPath, filePath)

logrus.Trace(loggerfs, "Uploading to: ", fullPath)
log.Trace(loggerfs, "Uploading to: ", fullPath)
writer, err := os.Create(fullPath)
if err != nil {
return
Expand Down
16 changes: 8 additions & 8 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ type IntegrationConfig struct {
Name string

// WebDav // FTP
Username string
Password string
Address string
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Address string `yaml:"address,omitempty"`

// FTP
ActiveTransfers bool
ActiveTransfers bool `yaml:"activetransfers,omitempty"`

// Insecure ignore TLS cert errors
Insecure bool
Insecure bool `yaml:"insecure,omitempty"`

// Dropbox
Accesstoken string
Accesstoken string `yaml:"accesstoken,omitempty"`

// Localfs
//TODO: experimental, security blah blah
Path string
// really dangerous as it allows path traversal
Path string `yaml:"path,omitempty"`
}

// GenPassword generates a new random password
Expand Down
15 changes: 8 additions & 7 deletions internal/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (app *ReactAppWrapper) register(c *gin.Context) {
if client != "localhost" &&
client != "::1" &&
client != "127.0.0.1" {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "Registrations are closed"})
c.AbortWithStatusJSON(http.StatusForbidden, viewmodel.NewErrorResponse("Registrations are closed"))
return
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func (app *ReactAppWrapper) changePassword(c *gin.Context) {

if user.ID != uid {
log.Error("Trying to change password for a different user.")
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "cant do that"})
c.AbortWithStatusJSON(http.StatusBadRequest, viewmodel.NewErrorResponse("cant do that"))
return
}

Expand All @@ -188,7 +188,7 @@ func (app *ReactAppWrapper) changePassword(c *gin.Context) {
if err != nil {
log.Error(err)
}
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid email or password"})
c.AbortWithStatusJSON(http.StatusBadRequest, viewmodel.NewErrorResponse("Invalid email or password"))
return
}

Expand All @@ -213,14 +213,14 @@ func (app *ReactAppWrapper) newCode(c *gin.Context) {
user, err := app.userStorer.GetUser(uid)
if err != nil {
log.Error("Unable to find user: ", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.AbortWithStatusJSON(http.StatusInternalServerError, viewmodel.NewErrorResponse(err.Error()))
return
}

code, err := app.codeConnector.NewCode(user.ID)
if err != nil {
log.Error("Unable to generate new device code: ", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Unable to generate new code"})
c.AbortWithStatusJSON(http.StatusInternalServerError, viewmodel.NewErrorResponse("Unable to generate new code"))
return
}

Expand Down Expand Up @@ -389,7 +389,7 @@ func (app *ReactAppWrapper) getAppUsers(c *gin.Context) {

if err != nil {
log.Error(err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Unable to get users."})
c.AbortWithStatusJSON(http.StatusInternalServerError, viewmodel.NewErrorResponse("Unable to get users."))
return
}

Expand Down Expand Up @@ -537,7 +537,8 @@ func warnLocalfsEdition(c *gin.Context, int *model.IntegrationConfig) {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "To avoid security issues with local directory integration, you have to manually edit your .userprofile file:\n\n" + string(s)})
c.AbortWithStatusJSON(http.StatusForbidden,
viewmodel.NewErrorResponse("To avoid security issues with local directory integration, you have to manually edit your .userprofile file:\n\n" + string(s)))
}

func (app *ReactAppWrapper) createIntegration(c *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ func (w ReactAppWrapper) Open(filepath string) (http.File, error) {
return f, err
}
func badReq(c *gin.Context, message string) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": message})
c.AbortWithStatusJSON(http.StatusBadRequest, viewmodel.NewErrorResponse(message))
}
10 changes: 10 additions & 0 deletions internal/ui/viewmodel/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ type ChangeEmailForm struct {
CurrentPassword string `json:"currentPassword"`
}

// ErrorResponse
type ErrorResponse struct {
Error string `json:"error"`
}
func NewErrorResponse(errormsg string) ErrorResponse {
return ErrorResponse {
Error: errormsg,
}
}

// DocumentTree a tree of documents
type DocumentTree struct {
Entries []Entry
Expand Down

0 comments on commit bba2bdf

Please sign in to comment.