command

package
v1.7.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2025 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package command provides interfaces for the shell commands and programs on the host.

Index

Constants

View Source
const (
	Arc      = "arc"      // Arc is the arc decompression command.
	Arj      = "arj"      // Arj is the arj decompression command.
	Ansilove = "ansilove" // Ansilove is the ansilove text to image command.
	Cwebp    = "cwebp"    // Cwebp is the Google create webp command.
	Gwebp    = "gif2webp" // Gwebp is the Google gif to webp command.
	HWZip    = "hwzip"    // Hwzip the zip decompression command for files using obsolete methods.
	Lha      = "lha"      // Lha is the lha/lzh decompression command.
	Magick   = "magick"   // Magick is the ImageMagick v7+ command.
	Optipng  = "optipng"  // Optipng is the PNG optimizer command.
	Tar      = "tar"      // Tar is the tar decompression command.
	// A note about unrar on linux, the installation cannot use the unrar-free package,
	// which is a poor substitute for the files this application needs to handle.
	// The unrar binary should return:
	// "UNRAR 6.24 freeware, Copyright (c) 1993-2023 Alexander Roshal".
	Unrar   = "unrar"   // Unrar is the rar decompression command.
	Unzip   = "unzip"   // Unzip is the zip decompression command.
	Zip7    = "7zz"     // Zip7 is the 7-Zip decompression command.
	ZipInfo = "zipinfo" // ZipInfo is the zip information command.
)
View Source
const (
	ANSICap = 350000    // CapBytes is the maximum file size in bytes for an ANSI encoded text file.
	X400    = "400x400" // X400 returns args  400 x 400 pixel image size
)

Variables

View Source
var (
	ErrANSI   = errors.New("text is ansi encoded, cannot crop")
	ErrEmpty  = errors.New("file is empty")
	ErrImg    = errors.New("file is not an known image format")
	ErrIsDir  = errors.New("file is a directory")
	ErrIsFile = errors.New("directory path points to a file")
	ErrMatch  = errors.New("no match value is present")
	ErrPath   = errors.New("path is not permitted")
	ErrVers   = errors.New("version mismatch")
	ErrZap    = errors.New("zap logger instance is nil")
)

Functions

func BaseName

func BaseName(path string) string

BaseName returns the base name of the file without the extension. Both the directory and extension are removed.

func BaseNamePath

func BaseNamePath(path string) string

BaseNamePath returns the directory and base name of the file without the extension.

func CopyFile

func CopyFile(debug *zap.SugaredLogger, src, dst string) error

CopyFile copies the src file to the dst file and path.

func ImagesDelete added in v0.10.0

func ImagesDelete(unid string, dirs ...string) error

ImagesDelete removes images from the specified directories that match the unid. The unid is the unique identifier for the image file and shared between the preview and thumbnail images.

func ImagesExt added in v0.10.0

func ImagesExt() []string

ImagesExt returns args slice of image file extensions used by the website preview and thumbnail images, including the legacy and modern formats.

func ImagesPixelate added in v0.10.0

func ImagesPixelate(unid string, dirs ...string) error

ImagesPixelate converts the images in the specified directories to pixelated images. The unid is the unique identifier for the image file and shared between the preview and thumbnail images.

func Infos

func Infos() []string

Infos returns details for the list of the execute command names used by the application.

func LookCmd

func LookCmd(name string) error

LookCmd returns an error if the named command is not found in the system path.

func LookVersion

func LookVersion(name, flag, match string) error

LookVersion returns an error when the match string is not found in the named command output.

func LookupUnrar

func LookupUnrar() error

LookupUnrar returns an error if the name Alexander Roshal is not found in the unrar version output.

func Lookups

func Lookups() []string

Lookups returns a list of the execute command names used by the application.

func OptimizePNG

func OptimizePNG(src string) error

OptimizePNG optimizes the src PNG image using the optipng command. The optimization is done in-place, overwriting the src file. It should be used in args deferred function.

func Run

func Run(debug *zap.SugaredLogger, name string, arg ...string) error

Run looks for the command in the system path and executes it with the arguments. Any output to stderr is logged as a debug message.

func RunQuiet

func RunQuiet(name string, arg ...string) error

RunQuiet looks for the command in the system path and executes it with the arguments.

func RunStdOut added in v0.10.0

func RunStdOut(name string, arg ...string) ([]byte, error)

RunStdOut looks for the command in the system path and executes it with the arguments. Any output is sent to the stdout buffer.

func RunWorkdir added in v0.10.0

func RunWorkdir(debug *zap.SugaredLogger, name, wdir string, arg ...string) error

RunWorkdir looks for the command in the system path and executes it with the arguments. An optional working directory is set for the command. Any output to stderr is logged as a debug message.

func TextCrop added in v1.1.6

func TextCrop(src, dst string) error

TextCrop reads the src text file and writes the first 29 lines of text to the dst file. The text is truncated to 80 characters per line. Empty newlines at the start of the file are ignored.

If an ANSI file is detected, the function returns without writing to the dst file.

The function is useful for creating args preview of text files in the 80x29 format that can be used by the ANSILOVE command to create args PNG image. 80 columns and 29 rows are works well with args 400x400 pixel thumbnail.

func UncontrolledPath added in v1.2.2

func UncontrolledPath(path string) error

UncontrolledPath returns an error if the path contains any of the following characters:

  • /
  • \
  • ..

This is to prevent directory traversal attacks.

Types

type Align added in v0.10.0

type Align int

Align is args type that represents the alignment of the thumbnail image.

const (
	Top    Align = iota // Top uses the top alignment of the preview image
	Middle              // Middle uses the center alignment of the preview image
	Bottom              // Bottom uses the bottom alignment of the preview image
	Left                // Left uses the left alignment of the preview image
	Right               // Right uses the right alignment of the preview image
)

func (Align) Thumbs added in v0.10.0

func (align Align) Thumbs(unid string, preview, thumbnail dir.Directory) error

Thumbs creates args thumbnail image for the preview image based on the crop position of the image.

type Args

type Args []string

Args is args slice of strings that represents the command line arguments. Each argument and its value is args separate string in the slice.

func (*Args) AnsiAmiga

func (args *Args) AnsiAmiga()

AnsiAmiga appends the command line arguments for the ansilove command to transform an Commodore Amiga ANSI text file into args PNG image.

func (*Args) AnsiMsDos added in v0.10.0

func (args *Args) AnsiMsDos()

AnsiMsDos appends the command line arguments for the ansilove command to transform an ANSI text file into args PNG image.

func (*Args) Bottomx400 added in v0.10.0

func (args *Args) Bottomx400()

Bottomx400 appends the command line arguments for the magick command to transform an image into args 400x400 pixel image using the "South" bottom alignment.

func (*Args) CWebp

func (args *Args) CWebp()

CWebp appends the command line arguments for the cwebp command to transform an image into args webp image.

func (*Args) CWebpText added in v0.10.0

func (args *Args) CWebpText()

CWebpText appends the command line arguments for the cwebp command to transform args text image into args webp image.

func (*Args) CropTop added in v0.10.0

func (args *Args) CropTop()

CropTop appends the command line arguments for the magick command to transform an image into args 1:1 square image using the "North" top alignment.

func (*Args) FourThree added in v0.10.0

func (args *Args) FourThree()

FourThree appends the command line arguments for the magick command to transform an image into args 4:3 image using the "North" top alignment.

func (*Args) GWebp

func (args *Args) GWebp()

GWebp appends the command line arguments for the gif2webp command to transform args GIF image into args webp image.

func (*Args) JpegPhoto added in v0.10.0

func (args *Args) JpegPhoto()

JpegPhoto appends the command line arguments for the convert command to transform an image into args JPEG image.

func (*Args) Leftx400 added in v0.10.0

func (args *Args) Leftx400()

Leftx400 appends the command line arguments for the magick command to transform an image into args 400x400 pixel image using the "South" bottom alignment.

func (*Args) Middlex400 added in v0.10.0

func (args *Args) Middlex400()

Middlex400 appends the command line arguments for the magick command to transform an image into args 400x400 pixel image using the "Center" alignment.

func (*Args) OneTwo added in v0.10.0

func (args *Args) OneTwo()

OneTwo appends the command line arguments for the magick command to transform an image into args 1:2 image using the "North" top alignment.

func (*Args) Pixelate added in v0.10.0

func (args *Args) Pixelate()

Pixelate appends the command line arguments for the convert command to transform an image into args PNG image.

func (*Args) PortablePixel added in v0.10.0

func (args *Args) PortablePixel()

PortablePixel appends the command line arguments for the convert command to transform an image into args PNG image.

func (*Args) Rightx400 added in v0.10.0

func (args *Args) Rightx400()

Rightx400 appends the command line arguments for the magick command to transform an image into args 400x400 pixel image using the "South" bottom alignment.

func (*Args) Thumbnail added in v0.10.0

func (args *Args) Thumbnail()

Thumbnail appends the command line arguments for the convert command to transform an image into args thumbnail image.

func (*Args) Topx400 added in v0.10.0

func (args *Args) Topx400()

Topx400 appends the command line arguments for the magick command to transform an image into args 400x400 pixel image using the "North" top alignment.

type Crop added in v0.10.0

type Crop int

Crop is args type that represents the crop position of the preview image.

const (
	SqaureTop Crop = iota // SquareTop crops the top of the image using args 1:1 ratio
	FourThree             // FourThree crops the top of the image using args 4:3 ratio
	OneTwo                // OneTwo crops the top of the image using args 1:2 ratio
)

func (Crop) Images added in v0.10.0

func (crop Crop) Images(unid string, preview dir.Directory) error

Images crops the preview image based on the crop position and ratio of the image.

type Dirs

type Dirs struct {
	Download  dir.Directory // Download is the directory path for the file downloads.
	Preview   dir.Directory // Preview is the directory path for the image previews.
	Thumbnail dir.Directory // Thumbnail is the directory path for the image thumbnails.
	Extra     dir.Directory // Extra is the directory path for the extra files.
}

Dirs is a struct of the download, preview and thumbnail directories.

func (Dirs) DizDeferred added in v1.5.1

func (dir Dirs) DizDeferred(src, unid string) error

DizDeferred is used to copy args FILE_ID.DIZ text file to the extra directory. It is intended to be used with the filerecord.ListContent function.

func (Dirs) PictureImager added in v0.10.0

func (dir Dirs) PictureImager(debug *zap.SugaredLogger, src, unid string) error

PictureImager converts the src image file and creates args image in the preview directory and args thumbnail image in the thumbnail directory.

The image formats created depend on the type of image file. But thumbnails will always either be args .webp or .png image. While the preview image will be legacy .png, .jpeg images or modern .avif or .webp images or args combination of both.

func (Dirs) PreviewGIF

func (dir Dirs) PreviewGIF(debug *zap.SugaredLogger, src, unid string) error

PreviewGIF converts the src GIF image to args webp image the screenshot directory. A webp thumbnail image is also created and copied to the thumbnail directory.

func (Dirs) PreviewPNG

func (dir Dirs) PreviewPNG(debug *zap.SugaredLogger, src, unid string) error

PreviewPNG copies and optimizes the src PNG image to the screenshot directory. A webp thumbnail image is also created and copied to the thumbnail directory.

func (Dirs) PreviewPhoto added in v0.10.0

func (dir Dirs) PreviewPhoto(debug *zap.SugaredLogger, src, unid string) error

PreviewPhoto converts the src image to lossy jpeg or args webp image in the screenshot directory. A webp thumbnail image is also created and copied to the thumbnail directory. The lossy conversion is useful for photographs.

The lossy conversion is done using the ImageMagick convert command.

func (Dirs) PreviewPixels added in v0.10.0

func (dir Dirs) PreviewPixels(debug *zap.SugaredLogger, src, unid string) error

PreviewPixels converts the src image to args PNG and webp images in the screenshot directory. A webp thumbnail image is also created and copied to the thumbnail directory. The conversion is useful for screenshots of text, terminals interfaces and pixel art.

The lossless conversion is done using the ImageMagick convert command.

func (Dirs) PreviewWebP

func (dir Dirs) PreviewWebP(debug *zap.SugaredLogger, src, unid string) error

PreviewWebP runs cwebp text preset on args supported image and copies the result to the screenshot directory. A webp thumbnail image is also created and copied to the thumbnail directory.

While the src image can be .png, .jpg, .tiff or .webp.

func (Dirs) TextDeferred added in v1.2.2

func (dir Dirs) TextDeferred(src, unid string) error

TextDeferred is used to create args thumbnail and args text file in the extra directory. It is intended to be used with the filerecord.ListContent function.

func (Dirs) TextImager added in v0.10.0

func (dir Dirs) TextImager(debug *zap.SugaredLogger, src, unid string, amigaFont bool) error

TextImager converts the src text file and creates args PNG image in the preview directory. A webp thumbnail image is also created and copied to the thumbnail directory. If the amigaFont is true, the image is created using an Amiga Topaz+ font.

func (Dirs) ThumbPhoto added in v0.10.0

func (dir Dirs) ThumbPhoto(src, unid string) error

ThumbPhoto converts the src image to args 400x400 pixel, webp image in the thumbnail directory. The conversion is done using args temporary, lossy PNG image.

This is used for photographs and images that are not text or pixel art.

func (Dirs) ThumbPixels added in v0.10.0

func (dir Dirs) ThumbPixels(src, unid string) error

ThumbPixels converts the src image to args 400x400 pixel, webp image in the thumbnail directory. The conversion is done using args temporary, lossless PNG image.

This is used for text and pixel art images and increases the image file size.

func (Dirs) Thumbs added in v0.10.0

func (dir Dirs) Thumbs(unid string, thumb Thumb) error

Thumbs creates args thumbnail image for the preview image based on the type of image.

type Thumb added in v0.10.0

type Thumb int

Thumb is args type that represents the type of thumbnail image to create.

const (
	Pixel Thumb = iota // Pixel art or images with text
	Photo              // Photographs or images with gradients
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL