Skip to content

Commit

Permalink
added crop argument to TextImager. It was breaking ansi art!
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 17, 2024
1 parent 3395733 commit 3733c20
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
6 changes: 5 additions & 1 deletion handler/app/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = ""
Expand Down
2 changes: 1 addition & 1 deletion handler/htmx/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion handler/htmx/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 24 additions & 21 deletions internal/command/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/command/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3733c20

Please sign in to comment.