1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-06 11:46:24 -07:00

Add check for embed before deleting post files

This commit is contained in:
Eggbertx 2025-03-31 23:10:05 -07:00
parent 734274d4ff
commit 1bea3a7e1c
5 changed files with 22 additions and 13 deletions

View file

@ -8,6 +8,7 @@ import (
"os"
"path"
"strconv"
"strings"
"sync"
"time"
@ -58,6 +59,10 @@ func coalesceErrors(errs ...error) error {
// deleteFile asynchronously deletes the post's file and thumb (if it has one, it returns nil if not) and
// thread HTML file if it is an OP and "File only" is unchecked, returning an error if one occcured for any file
func (u *delPost) deleteFile(delThread bool) error {
if u.filename == "" || u.filename == "deleted" || strings.HasPrefix(u.filename, "embed:") {
// no file to delete
return nil
}
var errCatalog, errThumb, errFile, errThread, errJSON error
var wg sync.WaitGroup
wg.Add(2)

View file

@ -182,7 +182,7 @@ func editPost(checkedPosts []int, editBtn string, doEdit string, writer http.Res
server.ServeError(writer, server.NewServerError("Error unlinking old upload from post: "+err.Error(), http.StatusInternalServerError), wantsJSON, nil)
return
}
if oldUpload.Filename != "deleted" {
if oldUpload.Filename != "deleted" && !oldUpload.IsEmbed() {
os.Remove(filePath)
os.Remove(thumbPath)
if post.IsTopPost {
@ -202,10 +202,12 @@ func editPost(checkedPosts []int, editBtn string, doEdit string, writer http.Res
filePath = path.Join(documentRoot, board.Dir, "src", upload.Filename)
thumbPath, catalogThumbPath = uploads.GetThumbnailFilenames(
path.Join(documentRoot, board.Dir, "thumb", upload.Filename))
os.Remove(filePath)
os.Remove(thumbPath)
if post.IsTopPost {
os.Remove(catalogThumbPath)
if !upload.IsEmbed() {
os.Remove(filePath)
os.Remove(thumbPath)
if post.IsTopPost {
os.Remove(catalogThumbPath)
}
}
}
} else {

View file

@ -381,7 +381,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
return err
}
var filePath string
if upload != nil {
if upload != nil && !upload.IsEmbed() {
filePath = path.Join(boardDir, "src", upload.Filename)
if err = os.Remove(filePath); err != nil {
errEv.Err(err).Caller().

View file

@ -103,7 +103,7 @@ func HandleFilterAction(filter *gcsql.Filter, post *gcsql.Post, upload *gcsql.Up
}
wantsJSON := serverutil.IsRequestingJSON(request)
documentRoot := config.GetSystemCriticalConfig().DocumentRoot
if upload != nil {
if upload != nil && !upload.IsEmbed() {
filePath := path.Join(documentRoot, board.Dir, "thumb", upload.Filename)
thumbPath, catalogThumbPath := uploads.GetThumbnailFilenames(
path.Join(documentRoot, board.Dir, "thumb", upload.Filename))
@ -503,16 +503,18 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
errEv.Err(err).Caller().
Str("sql", "postInsertion").
Msg("Unable to attach upload to post")
os.Remove(filePath)
os.Remove(thumbPath)
os.Remove(catalogThumbPath)
if upload != nil && !upload.IsEmbed() {
os.Remove(filePath)
os.Remove(thumbPath)
os.Remove(catalogThumbPath)
}
post.Delete()
server.ServeError(writer, "Unable to attach upload", wantsJSON, map[string]any{
"filename": upload.OriginalFilename,
})
return
}
if upload != nil {
if upload != nil && !upload.IsEmbed() {
if err = config.TakeOwnership(filePath); err != nil {
errEv.Err(err).Caller().
Str("file", filePath).Send()
@ -562,7 +564,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
server.ServeError(writer, "Unable to prune post from cyclic thread", wantsJSON, nil)
return
}
if prunePost.Filename != "" && prunePost.Filename != "deleted" {
if prunePost.Filename != "" && prunePost.Filename != "deleted" && !strings.HasPrefix(prunePost.Filename, "embed:") {
prunePostFile := path.Join(documentRoot, prunePost.Dir, "src", prunePost.Filename)
prunePostThumbName, _ := uploads.GetThumbnailFilenames(prunePost.Filename)
prunePostThumb := path.Join(documentRoot, prunePost.Dir, "thumb", prunePostThumbName)

View file

@ -26,7 +26,7 @@ func tempCleaner() {
if err != nil {
continue
}
if upload.OriginalFilename == "" {
if upload.OriginalFilename == "" || upload.Filename == "deleted" || upload.IsEmbed() {
continue
}
board, err := post.GetBoard()