From 5e81907cc82f1641599c7c90f8dfbdf1fb811f9a Mon Sep 17 00:00:00 2001 From: Code by Ben Date: Tue, 17 Sep 2024 13:52:04 +1000 Subject: [PATCH] automatic thumbnails for textfiles artifacts. --- handler/app/dirs.go | 61 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/handler/app/dirs.go b/handler/app/dirs.go index 20ba8de2..4cf12432 100644 --- a/handler/app/dirs.go +++ b/handler/app/dirs.go @@ -28,6 +28,7 @@ import ( "github.com/Defacto2/server/handler/readme" "github.com/Defacto2/server/handler/render" "github.com/Defacto2/server/handler/sess" + "github.com/Defacto2/server/internal/command" "github.com/Defacto2/server/internal/postgres/models" "github.com/Defacto2/server/internal/tags" "github.com/Defacto2/server/model" @@ -182,18 +183,23 @@ func (dir Dirs) updateMagics(db *sql.DB, logger *zap.SugaredLogger, } return data } - switch modMagic { - case - magicnumber.ARChiveSEA.Title(), - magicnumber.YoshiLHA.Title(), - magicnumber.ArchiveRobertJung.Title(), - magicnumber.PKWAREZipImplode.Title(), - magicnumber.PKWAREZipReduce.Title(), - magicnumber.PKWAREZipShrink.Title(): - case magicnumber.PKWAREZip.Title(): + switch { + case redundantArchive(modMagic): + case modMagic == magicnumber.PKWAREZip.Title(): if !repackZIP(name) { return data } + case plainText(modMagic): + dirs := command.Dirs{ + Download: dir.Download, + Preview: dir.Preview, + Thumbnail: dir.Thumbnail, + } + if err := dirs.TextImager(logger, name, uid); err != nil { + logger.Error(errorWithID(err, "text imager", uid)) + } + data["missingAssets"] = "" + return data default: return data } @@ -211,6 +217,43 @@ func (dir Dirs) updateMagics(db *sql.DB, logger *zap.SugaredLogger, return data } +func redundantArchive(modMagic interface{}) bool { + switch modMagic.(type) { + case string: + default: + return false + } + switch modMagic.(string) { + case + magicnumber.ARChiveSEA.Title(), + magicnumber.YoshiLHA.Title(), + magicnumber.ArchiveRobertJung.Title(), + magicnumber.PKWAREZipImplode.Title(), + magicnumber.PKWAREZipReduce.Title(), + magicnumber.PKWAREZipShrink.Title(): + return true + default: + return false + } +} + +func plainText(modMagic interface{}) bool { + switch modMagic.(type) { + case string: + default: + return false + } + switch modMagic.(string) { + case + magicnumber.UTF8Text.Title(), + magicnumber.ANSIEscapeText.Title(), + magicnumber.PlainText.Title(): + return true + default: + return false + } +} + func (dir Dirs) embed(art *models.File, data map[string]interface{}) (map[string]interface{}, error) { if art == nil { return data, nil