From 3733c201b3cf5e25440218738d1e43204211ef71 Mon Sep 17 00:00:00 2001 From: Code by Ben Date: Tue, 17 Sep 2024 20:10:17 +1000 Subject: [PATCH] added crop argument to TextImager. It was breaking ansi art! --- handler/app/dirs.go | 6 ++++- handler/htmx/artifact.go | 2 +- handler/htmx/transfer.go | 6 ++++- internal/command/images.go | 45 ++++++++++++++++++--------------- internal/command/images_test.go | 4 +-- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/handler/app/dirs.go b/handler/app/dirs.go index 32ed4970..f6594878 100644 --- a/handler/app/dirs.go +++ b/handler/app/dirs.go @@ -199,7 +199,11 @@ func (dir Dirs) updateMagics(db *sql.DB, logger *zap.SugaredLogger, helper.File(filepath.Join(dirs.Thumbnail, uid+".webp")) { return data } - if err := dirs.TextImager(logger, name, uid); err != nil { + crop := true + if modMagic == magicnumber.ANSIEscapeText.Title() { + crop = false + } + if err := dirs.TextImager(logger, name, uid, crop); err != nil { logger.Error(errorWithID(err, "text imager", uid)) } data["missingAssets"] = "" diff --git a/handler/htmx/artifact.go b/handler/htmx/artifact.go index 80ced914..a49107ce 100644 --- a/handler/htmx/artifact.go +++ b/handler/htmx/artifact.go @@ -126,7 +126,7 @@ func RecordReadmeImager(c echo.Context, logger *zap.SugaredLogger, dirs command. if st.Size() == 0 { return c.String(http.StatusOK, "The file is empty and was not used.") } - if err := dirs.TextImager(logger, src, unid); err != nil { + if err := dirs.TextImager(logger, src, unid, false); err != nil { return badRequest(c, err) } c = pageRefresh(c) diff --git a/handler/htmx/transfer.go b/handler/htmx/transfer.go index fe658e3b..951815ce 100644 --- a/handler/htmx/transfer.go +++ b/handler/htmx/transfer.go @@ -575,7 +575,11 @@ func UploadPreview(c echo.Context, previewDir, thumbnailDir string) error { return reloader(c, file.Filename) } if texters(magic) { - if err := dirs.TextImager(nil, dst.Name(), up.unid); err != nil { + crop := true + if magic == magicnumber.ANSIEscapeText { + crop = false + } + if err := dirs.TextImager(nil, dst.Name(), up.unid, crop); err != nil { return badRequest(c, err) } return reloader(c, file.Filename) diff --git a/internal/command/images.go b/internal/command/images.go index 9bc86e1a..18d85bf6 100644 --- a/internal/command/images.go +++ b/internal/command/images.go @@ -361,18 +361,20 @@ func Write80x29(src, dst string) error { // TextImager converts the src text file and creates a PNG image in the preview directory. // A webp thumbnail image is also created and copied to the thumbnail directory. -func (dir Dirs) TextImager(debug *zap.SugaredLogger, src, unid string) error { +func (dir Dirs) TextImager(debug *zap.SugaredLogger, src, unid string, crop bool) error { args := Args{} args.AnsiMsDos() - - path, err := helper.MkContent(src) - if err != nil { - return err - } - defer os.RemoveAll(path) - tmpText := filepath.Join(path, unid+".txt") - if err = Write80x29(src, tmpText); err != nil { - return fmt.Errorf("dirs text imager %w", err) + tmpText := src + if crop { + path, err := helper.MkContent(src) + if err != nil { + return err + } + defer os.RemoveAll(path) + tmpText := filepath.Join(path, unid+".txt") + if err = Write80x29(src, tmpText); err != nil { + return fmt.Errorf("dirs text imager %w", err) + } } arg := []string{tmpText} // source text file arg = append(arg, args...) // command line arguments @@ -387,20 +389,21 @@ func (dir Dirs) TextImager(debug *zap.SugaredLogger, src, unid string) error { // TextImager converts the src text file and creates a PNG image using an Amiga Topaz+ font // and stores it in the preview directory. // A webp thumbnail image is also created and copied to the thumbnail directory. -func (dir Dirs) TextAmigaImager(debug *zap.SugaredLogger, src, unid string) error { +func (dir Dirs) TextAmigaImager(debug *zap.SugaredLogger, src, unid string, crop bool) error { args := Args{} args.AnsiMsDos() - - path, err := helper.MkContent(src) - if err != nil { - return err - } - defer os.RemoveAll(path) - tmpText := filepath.Join(path, unid+".txt") - if err = Write80x29(src, tmpText); err != nil { - return fmt.Errorf("dirs text imager %w", err) + tmpText := src + if crop { + path, err := helper.MkContent(src) + if err != nil { + return err + } + defer os.RemoveAll(path) + tmpText := filepath.Join(path, unid+".txt") + if err = Write80x29(src, tmpText); err != nil { + return fmt.Errorf("dirs text imager %w", err) + } } - arg := []string{tmpText} // source text file arg = append(arg, args...) // command line arguments tmp := BaseNamePath(src) + png // destination file diff --git a/internal/command/images_test.go b/internal/command/images_test.go index 392695b3..1e76274b 100644 --- a/internal/command/images_test.go +++ b/internal/command/images_test.go @@ -53,9 +53,9 @@ func TestDirs(t *testing.T) { dir := command.Dirs{} err := dir.PictureImager(nil, "", "") require.Error(t, err) - err = dir.TextImager(nil, "", "") + err = dir.TextImager(nil, "", "", true) require.Error(t, err) - err = dir.TextAmigaImager(nil, "", "") + err = dir.TextAmigaImager(nil, "", "", false) require.Error(t, err) err = dir.PreviewPhoto(nil, "", "") require.Error(t, err)