1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 15:06:23 -07:00

Don't write file thumbnail if spoiler (otherwise it overwrites /spoiler.png)

Also add spoilering for videos and non-video non-image uploads
This commit is contained in:
Eggbertx 2024-07-29 21:05:03 -07:00
parent 8bc9c225e2
commit b85b91c04d
6 changed files with 41 additions and 18 deletions

View file

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Before After
Before After

View file

@ -178,6 +178,7 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
Str("referer", request.Referer()) Str("referer", request.Referer())
upload.IsSpoilered = request.FormValue("spoiler") == "on" upload.IsSpoilered = request.FormValue("spoiler") == "on"
gcutil.LogBool("isSpoiler", upload.IsSpoilered, infoEv, accessEv, errEv)
uploadHandler, ok := uploadHandlers[ext] uploadHandler, ok := uploadHandlers[ext]
if !ok { if !ok {
@ -187,6 +188,7 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
} }
if err = uploadHandler(upload, post, postBoard.Dir, filePath, thumbPath, catalogThumbPath, infoEv, accessEv, errEv); err != nil { if err = uploadHandler(upload, post, postBoard.Dir, filePath, thumbPath, catalogThumbPath, infoEv, accessEv, errEv); err != nil {
// uploadHandler is assumed to handle logging
return nil, errors.New("error processing upload: " + err.Error()) return nil, errors.New("error processing upload: " + err.Error())
} }

View file

@ -7,7 +7,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"syscall"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/gochan-org/gochan/pkg/config" "github.com/gochan-org/gochan/pkg/config"
@ -67,6 +66,7 @@ func processImage(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
Str("filePath", filePath).Send() Str("filePath", filePath).Send()
return err return err
} }
accessEv.Str("handler", "image")
// Get image width and height, as well as thumbnail width and height // Get image width and height, as well as thumbnail width and height
upload.Width = img.Bounds().Max.X upload.Width = img.Bounds().Max.X
@ -77,19 +77,12 @@ func processImage(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
} }
upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize(upload.Width, upload.Height, board, thumbType) upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize(upload.Width, upload.Height, board, thumbType)
documentRoot := config.GetSystemCriticalConfig().DocumentRoot
if upload.IsSpoilered { if upload.IsSpoilered {
// If spoiler is enabled, symlink thumbnail to spoiler image if err = createSpoilerThumbnail(upload, board, post.IsTopPost, thumbPath); err != nil {
if _, err := os.Stat(path.Join(documentRoot, "spoiler.png")); err != nil { errEv.Err(err).Caller().Msg("Unable to create spoiler thumbnail")
errEv.Err(err).Caller().Send() return ErrUnableToCreateSpoiler
return err
}
if err = syscall.Symlink(path.Join(documentRoot, "spoiler.png"), thumbPath); err != nil {
errEv.Err(err).Caller().
Str("thumbPath", thumbPath).
Msg("Error creating symbolic link to thumbnail path")
return err
} }
return nil
} }
shouldThumb := ShouldCreateThumbnail(filePath, shouldThumb := ShouldCreateThumbnail(filePath,
@ -121,7 +114,7 @@ func processImage(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
// If image fits in thumbnail size, symlink thumbnail to original // If image fits in thumbnail size, symlink thumbnail to original
upload.ThumbnailWidth = img.Bounds().Max.X upload.ThumbnailWidth = img.Bounds().Max.X
upload.ThumbnailHeight = img.Bounds().Max.Y upload.ThumbnailHeight = img.Bounds().Max.Y
if err := syscall.Symlink(filePath, thumbPath); err != nil { if err := os.Symlink(filePath, thumbPath); err != nil {
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")
@ -138,6 +131,5 @@ func processImage(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
} }
} }
} }
accessEv.Str("handler", "image")
return nil return nil
} }

View file

@ -17,11 +17,14 @@ var (
func processOther(upload *gcsql.Upload, post *gcsql.Post, board string, filePath string, thumbPath string, catalogThumbPath string, infoEv *zerolog.Event, accessEv *zerolog.Event, errEv *zerolog.Event) error { func processOther(upload *gcsql.Upload, post *gcsql.Post, board string, filePath string, thumbPath string, catalogThumbPath string, infoEv *zerolog.Event, accessEv *zerolog.Event, errEv *zerolog.Event) error {
boardConfig := config.GetBoardConfig(board) boardConfig := config.GetBoardConfig(board)
ext := path.Ext(filePath) ext := path.Ext(filePath)
cfgThumb, ok := boardConfig.AllowOtherExtensions[ext] thumbnailFilename, ok := boardConfig.AllowOtherExtensions[ext]
if !ok { if !ok {
errEv.Err(ErrUnsupportedFileExt).Str("ext", ext).Caller().Send() errEv.Err(ErrUnsupportedFileExt).Str("ext", ext).Caller().Send()
return ErrUnsupportedFileExt return ErrUnsupportedFileExt
} }
if upload.IsSpoilered {
thumbnailFilename = "spoiler.png"
}
infoEv.Str("post", "withOther") infoEv.Str("post", "withOther")
if post.ThreadID == 0 { if post.ThreadID == 0 {
@ -33,8 +36,8 @@ func processOther(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
upload.ThumbnailWidth = boardConfig.ThumbWidthReply upload.ThumbnailWidth = boardConfig.ThumbWidthReply
upload.ThumbnailHeight = boardConfig.ThumbHeightReply upload.ThumbnailHeight = boardConfig.ThumbHeightReply
} }
staticThumbPath := path.Join("static/", cfgThumb) originalThumbPath := path.Join(config.GetSystemCriticalConfig().DocumentRoot, "static", thumbnailFilename)
originalThumbPath := path.Join(config.GetSystemCriticalConfig().DocumentRoot, staticThumbPath)
_, err := os.Stat(originalThumbPath) _, err := os.Stat(originalThumbPath)
if err != nil { if err != nil {
errEv.Err(err).Str("originalThumbPath", originalThumbPath).Send() errEv.Err(err).Str("originalThumbPath", originalThumbPath).Send()

View file

@ -14,7 +14,15 @@ import (
func processVideo(upload *gcsql.Upload, post *gcsql.Post, board string, filePath string, thumbPath string, catalogThumbPath string, infoEv *zerolog.Event, accessEv *zerolog.Event, errEv *zerolog.Event) error { func processVideo(upload *gcsql.Upload, post *gcsql.Post, board string, filePath string, thumbPath string, catalogThumbPath string, infoEv *zerolog.Event, accessEv *zerolog.Event, errEv *zerolog.Event) error {
boardConfig := config.GetBoardConfig(board) boardConfig := config.GetBoardConfig(board)
infoEv.Str("post", "withVideo") infoEv.Str("post", "withVideo")
accessEv.Str("handler", "video")
var err error var err error
if upload.IsSpoilered {
if err = createSpoilerThumbnail(upload, board, post.IsTopPost, thumbPath); err != nil {
errEv.Err(err).Caller().Msg("Unable to create spoiler thumbnail")
return ErrUnableToCreateSpoiler
}
return nil
}
if post.ThreadID == 0 { if post.ThreadID == 0 {
if err = createVideoThumbnail(filePath, thumbPath, boardConfig.ThumbWidth); err != nil { if err = createVideoThumbnail(filePath, thumbPath, boardConfig.ThumbWidth); err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
@ -67,6 +75,5 @@ func processVideo(upload *gcsql.Upload, post *gcsql.Post, board string, filePath
upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize( upload.ThumbnailWidth, upload.ThumbnailHeight = getThumbnailSize(
upload.Width, upload.Height, board, thumbType) upload.Width, upload.Height, board, thumbType)
} }
accessEv.Str("handler", "video")
return nil return nil
} }

View file

@ -3,6 +3,7 @@ package uploads
import ( import (
"errors" "errors"
"image" "image"
"os"
"os/exec" "os/exec"
"path" "path"
"strconv" "strconv"
@ -12,6 +13,7 @@ import (
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/gochan-org/gochan/pkg/config" "github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/gcutil" "github.com/gochan-org/gochan/pkg/gcutil"
) )
@ -32,6 +34,7 @@ var (
".jfif": ".jpg", ".jfif": ".jpg",
".jpeg": ".jpg", ".jpeg": ".jpg",
} }
ErrUnableToCreateSpoiler = errors.New("unable to create spoiler thumbnail")
) )
func GetThumbnailExtension(fileExt string) string { func GetThumbnailExtension(fileExt string) string {
@ -83,6 +86,22 @@ func createVideoThumbnail(video, thumb string, size int) error {
return err return err
} }
func createSpoilerThumbnail(upload *gcsql.Upload, board string, isOP bool, thumbnailPath string) error {
boardCfg := config.GetBoardConfig(board)
spoilerPath := path.Join(config.GetSystemCriticalConfig().DocumentRoot, "static/spoiler.png")
if _, err := os.Stat(spoilerPath); err != nil {
return err
}
if isOP {
upload.ThumbnailWidth = boardCfg.ThumbWidth
upload.ThumbnailHeight = boardCfg.ThumbHeight
} else {
upload.ThumbnailWidth = boardCfg.ThumbWidthReply
upload.ThumbnailHeight = boardCfg.ThumbHeightReply
}
return os.Symlink(spoilerPath, thumbnailPath)
}
func getBoardThumbnailSize(boardDir string, thumbType ThumbnailCategory) (int, int) { func getBoardThumbnailSize(boardDir string, thumbType ThumbnailCategory) (int, int) {
boardCfg := config.GetBoardConfig(boardDir) boardCfg := config.GetBoardConfig(boardDir)
switch thumbType { switch thumbType {