soft error if file do not exist in file system

This commit is contained in:
Lars Hoogestraat 2018-10-31 22:37:08 +01:00
parent 5a984b4211
commit e39244aa01
9 changed files with 50 additions and 20 deletions

View File

@ -310,6 +310,10 @@ main p {
background-color: #87CEEB;
}
.alert-warning {
background-color: #FFCC00;
}
.alert-danger {
background-color: #FF6666;
}

View File

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"git.hoogi.eu/go-blog/components/logger"
"git.hoogi.eu/go-blog/middleware"
@ -242,11 +243,16 @@ func AdminUploadDeletePostHandler(ctx *middleware.AppContext, w http.ResponseWri
err = ctx.FileService.DeleteFile(id, ctx.ConfigService.File.Location, u)
warnMsg := ""
if err != nil {
return &middleware.Template{
RedirectPath: "/admin/files",
Err: err,
Active: "files",
if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
warnMsg = "File removed from database, but was not found in file system anymore"
} else {
return &middleware.Template{
RedirectPath: "/admin/files",
Err: err,
Active: "files",
}
}
}
@ -254,5 +260,6 @@ func AdminUploadDeletePostHandler(ctx *middleware.AppContext, w http.ResponseWri
Active: "files",
RedirectPath: "admin/files",
SuccessMsg: "File successfully deleted",
WarnMsg: warnMsg,
}
}

View File

@ -43,17 +43,21 @@ func AdminUsersHandler(ctx *middleware.AppContext, w http.ResponseWriter, r *htt
}
}
userInvites, err := ctx.UserInviteService.ListUserInvites()
var userInvites []models.UserInvite
if err != nil {
return &middleware.Template{
Name: tplAdminUsers,
Err: err,
Active: "users",
Data: map[string]interface{}{
"users": users,
"pagination": p,
},
if cu, _ := middleware.User(r); cu.IsAdmin {
userInvites, err = ctx.UserInviteService.ListUserInvites()
if err != nil {
return &middleware.Template{
Name: tplAdminUsers,
Err: err,
Active: "users",
Data: map[string]interface{}{
"users": users,
"pagination": p,
},
}
}
}

View File

@ -30,6 +30,7 @@ type Template struct {
Active string
Data map[string]interface{}
SuccessMsg string
WarnMsg string
RedirectPath string
Err error
}

View File

@ -32,7 +32,7 @@ type TemplateHandler struct {
type Handler func(*AppContext, http.ResponseWriter, *http.Request) *Template
func (fn TemplateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
var errorMsg, successMsg string
var errorMsg, warnMsg, successMsg string
statusCode := 200
t := fn.Handler(fn.AppCtx, rw, r)
@ -48,6 +48,7 @@ func (fn TemplateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
}
successMsg = t.SuccessMsg
warnMsg = t.WarnMsg
if t.Err != nil {
switch e := t.Err.(type) {
@ -82,6 +83,14 @@ func (fn TemplateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
t.Data["ErrorMsg"] = fl
}
fl, err = getFlash(rw, r, "WarnMsg")
if err != nil {
logger.Log.Error(err)
} else if len(fl) > 0 {
t.Data["WarnMsg"] = fl
}
t.Data[csrf.TemplateTag] = csrf.TemplateField(r)
t.Data["active"] = t.Active
@ -95,6 +104,8 @@ func (fn TemplateHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
statusCode = http.StatusFound
if len(errorMsg) > 0 {
setCookie(rw, "ErrorMsg", "/", errorMsg)
} else if len(warnMsg) > 0 {
setCookie(rw, "WarnMsg", "/", warnMsg)
} else if len(successMsg) > 0 {
setCookie(rw, "SuccessMsg", "/", successMsg)
}

View File

@ -98,13 +98,13 @@ func (fs FileService) DeleteFile(fileID int, location string, u *User) error {
}
}
err = os.Remove(filepath.Join(location, file.Filename))
err = fs.Datasource.Delete(fileID)
if err != nil {
return err
}
return fs.Datasource.Delete(fileID)
return os.Remove(filepath.Join(location, file.Filename))
}
//UploadFile uploaded files will be saved at the configured file location, filename is saved in the database

View File

@ -18,7 +18,7 @@ func (m Mailer) SendActivationLink(ui *UserInvite) error {
mail := mail.Mail{
To: ui.Email,
Subject: "Password change",
Subject: "You got an invitation",
Body: fmt.Sprintf("Hi %s, \n\n you are invited join %s. To activate your account click the following link and enter a password %s", ui.DisplayName, m.AppConfig.Title, activation),
}

View File

@ -184,12 +184,12 @@ func (cfg *Settings) CheckConfig() error {
}
if len(cfg.Application.Domain) == 0 {
return errors.New("config: please specify a domain name 'blog_domain'")
return errors.New("config: please specify a domain name 'application_domain'")
}
_, err := url.ParseRequestURI(cfg.Application.Domain)
if err != nil {
return fmt.Errorf("config: invalid url setting for key 'blog_domain' value '%s'", cfg.Application.Domain)
return fmt.Errorf("config: invalid url setting for key 'application_domain' value '%s'", cfg.Application.Domain)
}
//server settings

View File

@ -2,6 +2,9 @@
{{if .ErrorMsg}}
<div style="margin-top: 10px" class="alert alert-danger" role="status">{{.ErrorMsg}}</div>
{{end}}
{{if .WarnMsg}}
<div style="margin-top: 10px" class="alert alert-warning" role="status">{{.WarnMsg}}</div>
{{end}}
{{if .SuccessMsg}}
<div style="margin-top: 10px" class="alert alert-success" role="status">{{.SuccessMsg}}</div>
{{end}}