citra

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 22 Imported by: 0

README

Citra

Citra is a simple web server that serves images.

Documentation

Index

Constants

View Source
const (
	ImageTypeJPEG = ImageType("jpeg")
	ImageTypeWEBP = ImageType("webp")
)

List of image types.

View Source
const (
	// ImageFitCover covers the given container with the image. The resulting
	// image may be shrunken, enlarged, and/or cropped.
	ImageFitCover = ImageFit("cover")

	// ImageFitContain fits the image in the container without either enlarging
	// the image or cropping it.
	ImageFitContain = ImageFit("contain")

	ImageFitDefault = ImageFitContain
)

Valid ImageFit values.

View Source
const (
	// MaxImagesPerFolder sets the maximum number of image files (without
	// counting copies) that can be saved in one folder.
	MaxImagesPerFolder = 1000
)

Variables

View Source
var (
	ErrInvalidImageFit  = errors.New("invalid image fit")
	ErrUnsupportedImage = errors.New("unsupported image format")
)

Errors.

View Source
var (
	ErrNoDefaultImage = errors.New("no default image was provided")
)

Errors.

Functions

func ContainInResolution

func ContainInResolution(width, height, w, h int) (int, int)

ContainInResolution returns width and height as they fit into an image of size w and h. Aspect ratio is not changed.

func GetImageSize

func GetImageSize(image []byte) (w int, h int, err error)

GetImageSize returns the size of image.

func RunMigrations

func RunMigrations(db *sql.DB) error

RunMigrations runs all the migrations in migrations folder.

Types

type Config

type Config struct {
	// MariaDB connection.
	Database struct {
		User     string `json:"user"`
		Password string `json:"password"`
		Database string `json:"database"`
	} `json:"database"`

	// Address to listen on.
	Addr string `json:"addr"`

	// All images are saved inside subfolders in this directory.
	RootUploadsDir string `json:"rootUploadsDir"`

	// Deleted images are moved here. If DeletedDir is empty, the images are
	// deleted.
	DeletedDir string `json:"deletedDir"`

	// Requests with images larger than this would be discarded.
	MaxUploadSize int `json:"maxUploadSize"`
}

Config is the configuration for the HTTP server.

func UnmarshalConfigFile

func UnmarshalConfigFile(file string) (*Config, error)

UnmarshalConfigFile reads the config in file and returns it. In case no such file is found, it returns a default config.

type DBImage

type DBImage struct {
	ID luid.ID `json:"id"`

	FolderID int `json:"folderId"`

	// Always JPEG for now.
	Type ImageType `json:"type"`

	// Actual width of image.
	Width int `json:"width"`

	// Actual height of image.
	Height int `json:"height"`

	// This is the MaxWidth that was provided as an argument
	// to addImage API call.
	MaxWidth int `json:"maxWidth"`

	MaxHeight int `json:"maxHeight"`

	// Size of image in bytes.
	Size int `json:"size"`

	// Size of original uploaded image in bytes.
	UploadedSize int `json:"-"`

	AverageColor RGB `json:"averageColor"`

	// Copies are stored on disk (in appropriate folders) with filename
	// {ID}_{MaxWidth}_{MaxHeight}_{ImageFit}.jpg Copies may be nil.
	Copies []*ImageCopy `json:"copies"`

	CreatedAt time.Time  `json:"createdAt"`
	IsDeleted bool       `json:"deleted"`
	DeletedAt *time.Time `json:"deletedAt,omitempty"`

	// URL pathname of the image. Is of the format /images/{FolderID}/{ID}.jpg.
	URL string `json:"url,omitempty"`

	// URL pathnames of the image and all its copies.
	URLs []string `json:"urls,omitempty"`
}

DBImage is a record in the images table.

func DeleteImage

func DeleteImage(db *sql.DB, ID luid.ID, rootDir, deletedDir string) (*DBImage, error)

DeleteImage sets is_deleted field of images to true. If deletedDir is non-empty, images are moved to that directory. Otherwise they are deleted.

func GetImage

func GetImage(db *sql.DB, ID luid.ID) (*DBImage, error)

GetImage returns an image from DB. It may return a deleted image.

func SaveImage

func SaveImage(db *sql.DB, buf []byte, copies []SaveImageArg, rootDir string) (*DBImage, error)

SaveImage saves the image in buf to disk (in a folder inside rootDir) and creates a record in images table. It also creates and stores copies of the image.

All images are saved as JPEGs (for now).

func (*DBImage) GenerateURLs

func (i *DBImage) GenerateURLs()

GenerateURLs populates i.URL and i.URLs fields.

type ImageCopy

type ImageCopy struct {
	Width     int      `json:"w"`
	Height    int      `json:"h"`
	MaxWidth  int      `json:"mw"`
	MaxHeight int      `json:"mh"`
	ImageFit  ImageFit `json:"if"`
	// Size of image in bytes.
	Size int `json:"s"`
}

ImageCopy is a copy of an image.

func (ImageCopy) Filename

func (c ImageCopy) Filename(imageID string) string

Filename returns the basename of the image stored on disk.

type ImageFit

type ImageFit string

ImageFit denotes how an image is to be fitted into a rectangle.

func (*ImageFit) UnmarshalText

func (i *ImageFit) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler interface.

type ImageSize

type ImageSize struct {
	Width, Height int
}

ImageSize represents the size of an image.

func ToJPEG

func ToJPEG(image []byte, maxWidth, maxHeight int, fit ImageFit) ([]byte, ImageSize, error)

ToJPEG converts the image to a JPEG, if it's not already, and fits the image into maxWidth and maxHeight according to fit.

func (ImageSize) MarshalText

func (s ImageSize) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler interface. Output same as String method.

func (ImageSize) String

func (s ImageSize) String() string

String returns, for example, "400" if width and height are both 400px, and "400x600" if width is 400px and height is 600px.

func (*ImageSize) UnmarshalText

func (s *ImageSize) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler interface. It does the reverse of what MarshalText and String do.

type ImageType

type ImageType string

ImageType represents the type of image.

type RGB

type RGB struct {
	R int `json:"r"`
	G int `json:"g"`
	B int `json:"b"`
}

RGB represents color values of range (0, 255).

func AverageColor

func AverageColor(img image.Image) RGB

AverageColor returns the average RGB color of img by averaging the colors of at most 10000 pixels. Each RGB value is in the range of (0,255).

type SaveImageArg

type SaveImageArg struct {
	MaxWidth  int      `json:"maxWidth"`
	MaxHeight int      `json:"maxHeight"`
	ImageFit  ImageFit `json:"imageFit"`

	// If true, this is no longer a copy but the default, or the original,
	// image. There can be only one default copy per image (if multiple
	// arguments are provided as being default the first one is selected and
	// others are discarded).
	IsDefault bool `json:"default"`
}

SaveImageArg is an argument to SaveImage. It describes how a copy of the saving image is to be created.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is an HTTP server that processes and serves images.

func NewServer

func NewServer(db *sql.DB, c *Config) *Server

NewServer returns a new image server.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
pkg
luid
Package luid implements IDs that are (practically always) sequential but are not iterative like auto incrementing IDs.
Package luid implements IDs that are (practically always) sequential but are not iterative like auto incrementing IDs.

Jump to

Keyboard shortcuts

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