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 (
"errors"
"github.com/gochan-org/gochan/pkg/events"
"github.com/gochan-org/gochan/pkg/gcutil"
)
@ -53,14 +52,6 @@ func (p *Post) AttachFile(upload *Upload) error {
if upload == nil {
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 (
post_id, file_order, original_filename, filename, checksum, file_size,
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)
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).
Caller().Send()
return
}
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")
return
}
@ -310,10 +310,23 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
os.Remove(thumbPath)
os.Remove(catalogThumbPath)
}
server.ServeErrorPage(writer, "Unable to insert post: "+err.Error())
server.ServeError(writer, "Unable to insert post", wantsJSON, nil)
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 {
errEv.Err(err).Caller().
Str("sql", "postInsertion").
@ -322,7 +335,9 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
os.Remove(thumbPath)
os.Remove(catalogThumbPath)
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
}
if upload != nil {
@ -342,12 +357,12 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
// rebuild the board page
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
}
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
}

View file

@ -76,7 +76,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
data, err := io.ReadAll(file)
if err != nil {
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
}
defer file.Close()
@ -145,7 +147,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller().
Int("thumbWidth", boardConfig.ThumbWidth).
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
}
} else {
@ -154,7 +158,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
Str("thumbPath", thumbPath).
Int("thumbWidth", boardConfig.ThumbWidthReply).
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
}
}
@ -164,14 +170,18 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
Str("thumbPath", thumbPath).
Int("thumbWidth", boardConfig.ThumbWidthCatalog).
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
}
outputBytes, err := exec.Command("ffprobe", "-v", "quiet", "-show_format", "-show_streams", filePath).CombinedOutput()
if err != nil {
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
}
if outputBytes != nil {
@ -203,7 +213,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if err != nil {
errEv.Err(err).Caller().
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
}
upload.FileSize = int(stat.Size())
@ -247,7 +259,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
os.Remove(filePath)
errEv.Err(err).Caller().
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
}
// Get image filesize
@ -255,7 +269,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if err != nil {
errEv.Err(err).Caller().
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
}
upload.FileSize = int(stat.Size())
@ -278,14 +294,16 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
if request.FormValue("spoiler") == "on" {
// If spoiler is enabled, symlink thumbnail to spoiler image
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
}
if err = syscall.Symlink(path.Join(documentRoot, "spoiler.png"), thumbPath); err != nil {
gcutil.LogError(err).
Str("thumbPath", thumbPath).
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
}
}
@ -303,7 +321,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller().
Str("thumbPath", catalogThumbPath).
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
}
} else {
@ -313,7 +333,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller().
Str("thumbPath", thumbPath).
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
}
} else {
@ -324,7 +346,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller().
Str("thumbPath", thumbPath).
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
}
if post.ThreadID == 0 {
@ -334,7 +358,9 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
errEv.Err(err).Caller().
Str("thumbPath", catalogThumbPath).
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
}
}