1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 11:46:22 -07:00

Use ServeError instead of ServeErrorPage when handling upload errors

This commit is contained in:
Eggbertx 2023-06-15 12:15:12 -07:00
parent 49d9b0ae20
commit d62520591a
3 changed files with 61 additions and 29 deletions

View file

@ -3,7 +3,6 @@ package gcsql
import ( import (
"errors" "errors"
"github.com/gochan-org/gochan/pkg/events"
"github.com/gochan-org/gochan/pkg/gcutil" "github.com/gochan-org/gochan/pkg/gcutil"
) )
@ -53,14 +52,6 @@ func (p *Post) AttachFile(upload *Upload) error {
if upload == nil { if upload == nil {
return nil // no upload to attach, so no error return nil // no upload to attach, so no error
} }
_, err, recovered := events.TriggerEvent("incoming-upload", upload)
if recovered {
return events.ErrRecovered
}
if err != nil {
return err
}
const query = `INSERT INTO DBPREFIXfiles ( const query = `INSERT INTO DBPREFIXfiles (
post_id, file_order, original_filename, filename, checksum, file_size, post_id, file_order, original_filename, filename, checksum, file_size,
is_spoilered, thumbnail_width, thumbnail_height, width, height) is_spoilered, thumbnail_width, thumbnail_height, width, height)

View file

@ -265,13 +265,13 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
captchaSuccess, err := SubmitCaptchaResponse(request) captchaSuccess, err := SubmitCaptchaResponse(request)
if err != nil { if err != nil {
server.ServeErrorPage(writer, "Error submitting captcha response:"+err.Error()) server.ServeError(writer, "Error submitting captcha response:"+err.Error(), wantsJSON, nil)
errEv.Err(err). errEv.Err(err).
Caller().Send() Caller().Send()
return return
} }
if !captchaSuccess { if !captchaSuccess {
server.ServeErrorPage(writer, "Missing or invalid captcha response") server.ServeError(writer, "Missing or invalid captcha response", wantsJSON, nil)
errEv.Msg("Missing or invalid captcha response") errEv.Msg("Missing or invalid captcha response")
return return
} }
@ -310,10 +310,23 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
os.Remove(thumbPath) os.Remove(thumbPath)
os.Remove(catalogThumbPath) os.Remove(catalogThumbPath)
} }
server.ServeErrorPage(writer, "Unable to insert post: "+err.Error()) server.ServeError(writer, "Unable to insert post", wantsJSON, nil)
return return
} }
_, err, recovered = events.TriggerEvent("incoming-upload", upload)
if recovered {
writer.WriteHeader(http.StatusInternalServerError)
server.ServeError(writer, "Recovered from a panic in an event handler (incoming-upload)", wantsJSON, nil)
return
}
if err != nil {
errEv.Err(err).Caller().
Str("event", "incoming-upload").
Send()
server.ServeError(writer, "Unable to attach upload to post: "+err.Error(), wantsJSON, nil)
return
}
if err = post.AttachFile(upload); err != nil { if err = post.AttachFile(upload); err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("sql", "postInsertion"). Str("sql", "postInsertion").
@ -322,7 +335,9 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
os.Remove(thumbPath) os.Remove(thumbPath)
os.Remove(catalogThumbPath) os.Remove(catalogThumbPath)
post.Delete() post.Delete()
server.ServeErrorPage(writer, "Unable to attach upload: "+err.Error()) server.ServeError(writer, "Unable to attach upload", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return return
} }
if upload != nil { if upload != nil {
@ -342,12 +357,12 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
// rebuild the board page // rebuild the board page
if err = building.BuildBoards(false, postBoard.ID); err != nil { if err = building.BuildBoards(false, postBoard.ID); err != nil {
server.ServeErrorPage(writer, "Error building boards: "+err.Error()) server.ServeError(writer, "Unable to build boards", wantsJSON, nil)
return return
} }
if err = building.BuildFrontPage(); err != nil { if err = building.BuildFrontPage(); err != nil {
server.ServeErrorPage(writer, "Error building front page: "+err.Error()) server.ServeError(writer, "Unable to build front page", wantsJSON, nil)
return return
} }

View file

@ -76,7 +76,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
data, err := io.ReadAll(file) data, err := io.ReadAll(file)
if err != nil { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
server.ServeErrorPage(writer, "Error while trying to read file: "+err.Error()) server.ServeError(writer, "Error while trying to read file: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
defer file.Close() defer file.Close()
@ -145,7 +147,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Int("thumbWidth", boardConfig.ThumbWidth). Int("thumbWidth", boardConfig.ThumbWidth).
Msg("Error creating video thumbnail") Msg("Error creating video thumbnail")
server.ServeErrorPage(writer, "Error creating video thumbnail: "+err.Error()) server.ServeError(writer, "Error creating video thumbnail: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} else { } else {
@ -154,7 +158,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
Str("thumbPath", thumbPath). Str("thumbPath", thumbPath).
Int("thumbWidth", boardConfig.ThumbWidthReply). Int("thumbWidth", boardConfig.ThumbWidthReply).
Msg("Error creating video thumbnail for reply") Msg("Error creating video thumbnail for reply")
server.ServeErrorPage(writer, "Error creating video thumbnail: "+err.Error()) server.ServeError(writer, "Error creating video thumbnail: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} }
@ -164,14 +170,18 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
Str("thumbPath", thumbPath). Str("thumbPath", thumbPath).
Int("thumbWidth", boardConfig.ThumbWidthCatalog). Int("thumbWidth", boardConfig.ThumbWidthCatalog).
Msg("Error creating video thumbnail for catalog") Msg("Error creating video thumbnail for catalog")
server.ServeErrorPage(writer, "Error creating video thumbnail: "+err.Error()) server.ServeError(writer, "Error creating video thumbnail: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
outputBytes, err := exec.Command("ffprobe", "-v", "quiet", "-show_format", "-show_streams", filePath).CombinedOutput() outputBytes, err := exec.Command("ffprobe", "-v", "quiet", "-show_format", "-show_streams", filePath).CombinedOutput()
if err != nil { if err != nil {
gcutil.LogError(err).Msg("Error getting video info") gcutil.LogError(err).Msg("Error getting video info")
server.ServeErrorPage(writer, "Error getting video info: "+err.Error()) server.ServeError(writer, "Error getting video info: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
if outputBytes != nil { if outputBytes != nil {
@ -203,7 +213,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if err != nil { if err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("filePath", filePath).Send() Str("filePath", filePath).Send()
server.ServeErrorPage(writer, "Couldn't get upload filesize: "+err.Error()) server.ServeError(writer, "Couldn't get upload filesize: "+err.Error(), wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
upload.FileSize = int(stat.Size()) upload.FileSize = int(stat.Size())
@ -247,7 +259,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
os.Remove(filePath) os.Remove(filePath)
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("filePath", filePath).Send() Str("filePath", filePath).Send()
server.ServeErrorPage(writer, "Upload filetype not supported") server.ServeError(writer, "Upload filetype not supported", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
// Get image filesize // Get image filesize
@ -255,7 +269,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if err != nil { if err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("filePath", filePath).Send() Str("filePath", filePath).Send()
server.ServeErrorPage(writer, "Couldn't get image filesize: "+err.Error()) server.ServeError(writer, "Couldn't get image filesize", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
upload.FileSize = int(stat.Size()) upload.FileSize = int(stat.Size())
@ -278,14 +294,16 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if request.FormValue("spoiler") == "on" { if request.FormValue("spoiler") == "on" {
// If spoiler is enabled, symlink thumbnail to spoiler image // If spoiler is enabled, symlink thumbnail to spoiler image
if _, err := os.Stat(path.Join(documentRoot, "spoiler.png")); err != nil { if _, err := os.Stat(path.Join(documentRoot, "spoiler.png")); err != nil {
server.ServeErrorPage(writer, "missing spoiler.png") server.ServeError(writer, "missing spoiler.png", wantsJSON, nil)
return nil, true return nil, true
} }
if err = syscall.Symlink(path.Join(documentRoot, "spoiler.png"), thumbPath); err != nil { if err = syscall.Symlink(path.Join(documentRoot, "spoiler.png"), thumbPath); err != nil {
gcutil.LogError(err). gcutil.LogError(err).
Str("thumbPath", thumbPath). Str("thumbPath", thumbPath).
Msg("Error creating symbolic link to thumbnail path") Msg("Error creating symbolic link to thumbnail path")
server.ServeErrorPage(writer, err.Error()) server.ServeError(writer, "Failed creating spoiler thumbnail", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} }
@ -303,7 +321,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("thumbPath", catalogThumbPath). Str("thumbPath", catalogThumbPath).
Msg("Couldn't generate catalog thumbnail") Msg("Couldn't generate catalog thumbnail")
server.ServeErrorPage(writer, "Couldn't generate catalog thumbnail: "+err.Error()) server.ServeError(writer, "Couldn't generate catalog thumbnail", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} else { } else {
@ -313,7 +333,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("thumbPath", thumbPath). Str("thumbPath", thumbPath).
Msg("Couldn't generate catalog thumbnail") Msg("Couldn't generate catalog thumbnail")
server.ServeErrorPage(writer, "Couldn't save thumbnail: "+err.Error()) server.ServeError(writer, "Couldn't save thumbnail", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} else { } else {
@ -324,7 +346,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("thumbPath", thumbPath). Str("thumbPath", thumbPath).
Msg("Couldn't generate catalog thumbnail") Msg("Couldn't generate catalog thumbnail")
server.ServeErrorPage(writer, "Couldn't create thumbnail: "+err.Error()) server.ServeError(writer, "Couldn't create thumbnail", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
if post.ThreadID == 0 { if post.ThreadID == 0 {
@ -334,7 +358,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("thumbPath", catalogThumbPath). Str("thumbPath", catalogThumbPath).
Msg("Couldn't generate catalog thumbnail") Msg("Couldn't generate catalog thumbnail")
server.ServeErrorPage(writer, "Couldn't generate catalog thumbnail: "+err.Error()) server.ServeError(writer, "Couldn't generate catalog thumbnail", wantsJSON, map[string]interface{}{
"filename": upload.OriginalFilename,
})
return nil, true return nil, true
} }
} }