command

package
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: GPL-3.0 Imports: 16 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 X400 = "400x400" // X400 returns a  400 x 400 pixel image size

Variables

View Source
var (
	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 a 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 a 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 a preview of text files in the 80x29 format that can be used by the ANSILOVE command to create a PNG image. 80 columns and 29 rows are works well with a 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 a 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, previewDir, thumbnailDir string) error

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

type Args

type Args []string

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

func (*Args) AnsiAmiga

func (a *Args) AnsiAmiga()

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

func (*Args) AnsiMsDos added in v0.10.0

func (a *Args) AnsiMsDos()

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

func (*Args) Bottomx400 added in v0.10.0

func (a *Args) Bottomx400()

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

func (*Args) CWebp

func (a *Args) CWebp()

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

func (*Args) CWebpText added in v0.10.0

func (a *Args) CWebpText()

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

func (*Args) CropTop added in v0.10.0

func (a *Args) CropTop()

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

func (*Args) FourThree added in v0.10.0

func (a *Args) FourThree()

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

func (*Args) GWebp

func (a *Args) GWebp()

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

func (*Args) JpegPhoto added in v0.10.0

func (a *Args) JpegPhoto()

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

func (*Args) Leftx400 added in v0.10.0

func (a *Args) Leftx400()

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

func (*Args) Middlex400 added in v0.10.0

func (a *Args) Middlex400()

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

func (*Args) OneTwo added in v0.10.0

func (a *Args) OneTwo()

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

func (*Args) Pixelate added in v0.10.0

func (a *Args) Pixelate()

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

func (*Args) PortablePixel added in v0.10.0

func (a *Args) PortablePixel()

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

func (*Args) Rightx400 added in v0.10.0

func (a *Args) Rightx400()

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

func (*Args) Thumbnail added in v0.10.0

func (a *Args) Thumbnail()

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

func (*Args) Topx400 added in v0.10.0

func (a *Args) Topx400()

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

type Crop added in v0.10.0

type Crop int

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

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

func (Crop) Images added in v0.10.0

func (crop Crop) Images(unid, previewDir string) error

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

type Dirs

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

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

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 a image in the preview directory and a thumbnail image in the thumbnail directory.

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

func (Dirs) PreviewGIF

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

PreviewGIF converts the src GIF image to a 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 a 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 a 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 a 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 a thumbnail and a 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 a 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 a 400x400 pixel, webp image in the thumbnail directory. The conversion is done using a 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 a 400x400 pixel, webp image in the thumbnail directory. The conversion is done using a 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 a thumbnail image for the preview image based on the type of image.

type Thumb added in v0.10.0

type Thumb int

Thumb is a 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