media

package
v0.0.0-...-0bbd9cc Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2012 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = Configuration{
	DummyImageColor: "#a8a8a8",
	ImagesAdmin: ImagesAdminConfiguration{
		ImageEditorClass:    "media-image-editor",
		ThumbnailSize:       150,
		ThumbnailFrameClass: "thumbnail-frame",
		ActionsClass:        "actions",
	},
	ImageRefEditor: ImageRefEditorConfiguration{
		Class:               "media-imageref-editor",
		ThumbnailSize:       50,
		ThumbnailFrameClass: "thumbnail-frame",
		ActionsClass:        "actions",
	},
}
View Source
var ImageView = view.NewViewURLWrapper(view.RenderView(
	func(ctx *view.Context) error {
		reader, contentType, err := Config.Backend.ImageVersionReader(ctx.URLArgs[0])
		if err != nil {
			if _, ok := err.(ErrInvalidImageID); ok {
				return view.NotFound(ctx.URLArgs[0] + "/" + ctx.URLArgs[1] + " not found")
			}
			return err
		}
		_, err = io.Copy(ctx.Response, reader)
		if err != nil {
			return err
		}
		err = reader.Close()
		if err != nil {
			return err
		}
		ctx.Response.Header().Set("Content-Type", contentType)
		return nil
	},
))
View Source
var UploadImage = view.NewViewURLWrapper(view.RenderViewBindURLArgs(
	func(ctx *view.Context, thumbnailSize int) error {
		formatError := func(err error) error {
			return fmt.Errorf(`{success: false, error: "%s"}`, err.Error())
		}

		filename := ctx.Request.Header.Get("X-File-Name")
		var r io.ReadCloser
		if filename != "" {
			r = ctx.Request.Body
		} else {
			f, h, err := ctx.Request.FormFile("qqfile")
			if err != nil {
				return formatError(err)
			}
			filename = h.Filename
			r = f
		}
		defer r.Close()

		b, err := ioutil.ReadAll(r)
		if err != nil {
			return formatError(err)
		}
		i, err := NewImage(filename, b)
		if err != nil {
			return formatError(err)
		}
		v, err := i.Thumbnail(thumbnailSize)
		if err != nil {
			return formatError(err)
		}

		ctx.Response.Printf(`{success: true, imageID: "%s", thumbnailURL: "%s", thumbnailSize: %d}`, i.ID, v.URL().URL(ctx), thumbnailSize)
		return nil
	},
))

Functions

func ColoredImageDataURL

func ColoredImageDataURL(c color.Color) string

ColoredImageDataURL creates a 1 pixel image with the given color and encodes it as data URL.

func ImageDataURL

func ImageDataURL(imageURL string) (dataURL string, err error)

ImageDataURL downloads an image and encodes it as a data URL.

func ImagesAdmin

func ImagesAdmin() view.View

func MakePrettyUrlFilename

func MakePrettyUrlFilename(filename string) string

MakePrettyUrlFilename modifies a filename so it looks good as part on an URL.

func ResampleImage

func ResampleImage(m image.Image, r image.Rectangle, w, h int) image.Image

Resample returns a resampled copy of the image slice r of m. The returned image has width w and height h.

func UploadImageButton

func UploadImageButton(thumbnailSize int, onComplete string) view.View

Uses https://github.com/valums/file-uploader

func ViewPath

func ViewPath(name string) view.ViewPath

ViewPath returns the view.ViewPath for all media URLs.

Types

type Backend

type Backend interface {
	LoadImage(id string) (*Image, error)

	// SaveImage saves image and updates its ID if it is empty.
	SaveImage(image *Image) error

	DeleteImage(image *Image) error
	DeleteImageVersion(id string) error

	// ImageVersionReader returns an io.ReadCloser to read the image-data
	// with the given id from the backend.
	// If there is no image with the given id,
	// an error of type ErrInvalidImageID will be returned.
	ImageVersionReader(id string) (reader io.ReadCloser, ctype string, err error)

	// ImageVersionWriter returns an io.WriteCloser to write the image-data
	// to the backend. version.ID can be empty for a new image or the id
	// of an existing image. version.ID can be changed by the function call
	// regardless of the former value
	ImageVersionWriter(version *ImageVersion) (writer io.WriteCloser, err error)

	ImageIterator() model.Iterator
}

type Configuration

type Configuration struct {
	Backend                 Backend
	NoDynamicStyleAndScript bool
	DummyImageColor         string // web color

	ImagesAdmin    ImagesAdminConfiguration
	ImageRefEditor ImageRefEditorConfiguration
	// contains filtered or unexported fields
}

func (*Configuration) Close

func (self *Configuration) Close() error

func (*Configuration) Init

func (self *Configuration) Init() error

func (*Configuration) Name

func (self *Configuration) Name() string

type ErrInvalidImageID

type ErrInvalidImageID string

func (ErrInvalidImageID) Error

func (self ErrInvalidImageID) Error() string

type FormFieldFactory

type FormFieldFactory struct {
	view.FormFieldFactoryWrapper
}

func NewFormFieldFactory

func NewFormFieldFactory(wrapped view.FormFieldFactory, imageWidgetClass string, thumbnailsize int) *FormFieldFactory

func (*FormFieldFactory) CanCreateInput

func (self *FormFieldFactory) CanCreateInput(metaData *model.MetaData, form *view.Form) bool

func (*FormFieldFactory) NewInput

func (self *FormFieldFactory) NewInput(withLabel bool, metaData *model.MetaData, form *view.Form) (view.View, error)

type HorAlignment

type HorAlignment int
const (
	HorCenter HorAlignment = iota
	Left
	Right
)

type Image

type Image struct {
	ID       model.String `bson:",omitempty"`
	Title    model.String
	Link     model.Url
	Versions []ImageVersion
}

func LoadImage

func LoadImage(id string) (*Image, error)

LoadImage loads an existing image from Config.Backend.

func NewImage

func NewImage(filename string, data []byte) (*Image, error)

NewImage creates a new Image and saves the original version to Config.Backend. GIF, TIFF, BMP images will be read, but saved as PNG.

func NewImageFromReader

func NewImageFromReader(filename string, reader io.Reader) (*Image, error)

NewImageFromReader creates a new Image and saves the original version to Config.Backend. GIF, TIFF, BMP images will be read, but saved as PNG.

func NewImageFromURL

func NewImageFromURL(url string) (*Image, error)

NewImageFromURL creates a new Image by downloading it from url. GIF, TIFF, BMP images will be read, but saved as PNG.

func (*Image) AspectRatio

func (self *Image) AspectRatio() float64

AspectRatio returns Width / Height

func (*Image) ContentType

func (self *Image) ContentType() string

func (*Image) Delete

func (self *Image) Delete() error

func (*Image) DeleteVersion

func (self *Image) DeleteVersion(index int) error

func (*Image) Filename

func (self *Image) Filename() string

func (*Image) Grayscale

func (self *Image) Grayscale() bool

func (*Image) Height

func (self *Image) Height() int

func (*Image) Init

func (self *Image) Init()

func (*Image) Rectangle

func (self *Image) Rectangle() image.Rectangle

func (*Image) Save

func (self *Image) Save() error

func (*Image) Thumbnail

func (self *Image) Thumbnail(size int) (im *ImageVersion, err error)

func (*Image) TitleOrFilename

func (self *Image) TitleOrFilename() string

TitleOrFilename returns Title if not empty, or else Filename().

func (*Image) URL

func (self *Image) URL() view.URL

func (*Image) Version

func (self *Image) Version(width, height int, horAlign HorAlignment, verAlign VerAlignment, grayscale bool) (im *ImageVersion, err error)

func (*Image) VersionCentered

func (self *Image) VersionCentered(width, height int, grayscale bool) (im *ImageVersion, err error)

func (*Image) VersionSourceRect

func (self *Image) VersionSourceRect(sourceRect image.Rectangle, width, height int, grayscale bool, outsideColor color.Color) (im *ImageVersion, err error)

SourceRectVersion searches and returns an existing matching version, or a new one will be created and saved.

func (*Image) VersionTouchOrigFromOutside

func (self *Image) VersionTouchOrigFromOutside(width, height int, horAlign HorAlignment, verAlign VerAlignment, grayscale bool, outsideColor color.Color) (im *ImageVersion, err error)

func (*Image) VersionTouchOrigFromOutsideCentered

func (self *Image) VersionTouchOrigFromOutsideCentered(width, height int, grayscale bool, outsideColor color.Color) (im *ImageVersion, err error)

func (*Image) Width

func (self *Image) Width() int

type ImageRef

type ImageRef string

func (*ImageRef) GetImage

func (self *ImageRef) GetImage() (*Image, error)

GetImage loads the referenced image, or returns nil if the reference is empty.

func (*ImageRef) IsEmpty

func (self *ImageRef) IsEmpty() bool

func (*ImageRef) Required

func (self *ImageRef) Required(metaData *model.MetaData) bool

func (*ImageRef) SetImage

func (self *ImageRef) SetImage(image *Image)

SetImage sets the ID of image, or an empty reference if image is nil.

func (*ImageRef) SetString

func (self *ImageRef) SetString(str string) error

func (*ImageRef) String

func (self *ImageRef) String() string

func (*ImageRef) Validate

func (self *ImageRef) Validate(metaData *model.MetaData) error

type ImageRefEditorConfiguration

type ImageRefEditorConfiguration struct {
	Class               string
	ThumbnailFrameClass string
	ThumbnailSize       int
	ActionsClass        string
}

type ImageVersion

type ImageVersion struct {
	ID           model.String `bson:",omitempty"`
	Filename     model.String
	ContentType  model.String
	SourceRect   ModelRect
	OutsideColor model.Color
	Width        model.Int
	Height       model.Int
	Grayscale    model.Bool
	// contains filtered or unexported fields
}

func (*ImageVersion) AspectRatio

func (self *ImageVersion) AspectRatio() float64

AspectRatio returns Width / Height

func (*ImageVersion) LoadImage

func (self *ImageVersion) LoadImage() (image.Image, error)

func (*ImageVersion) SaveImage

func (self *ImageVersion) SaveImage(im image.Image) error

func (*ImageVersion) SaveImageData

func (self *ImageVersion) SaveImageData(data []byte) error

func (*ImageVersion) URL

func (self *ImageVersion) URL() view.URL

func (*ImageVersion) ViewImage

func (self *ImageVersion) ViewImage(class string) *view.Image
func (self *ImageVersion) ViewImageLink(imageClass, linkClass string) *view.Link

type ImagesAdminConfiguration

type ImagesAdminConfiguration struct {
	ImageEditorClass    string
	ThumbnailSize       int
	ThumbnailFrameClass string
	ActionsClass        string
	ButtonClass         string
}

type ModelRect

type ModelRect struct {
	MinX model.Int
	MinY model.Int
	MaxX model.Int
	MaxY model.Int
}

func (*ModelRect) Rectangle

func (self *ModelRect) Rectangle() image.Rectangle

func (*ModelRect) SetRectangle

func (self *ModelRect) SetRectangle(r image.Rectangle)

type VerAlignment

type VerAlignment int
const (
	VerCenter VerAlignment = iota
	Top
	Bottom
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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