attachment

package
v0.0.0-...-4ecb35b Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2017 License: MIT Imports: 12 Imported by: 0

README

image

Overview

Wraps Google's cloud storage lib allowing easy saving and lazy resizing of images upon request using appengine. This library was created to allow for each personal code re-use when uploading images to GCS and was not intended to be super flexible.

No tests have been written yet, so use at your own risk.

Usage

image provides methods to obtain reader and writer methods.

Examples

Note: Error handling is omitted in the examples below

Read
reader, err := image.NewReader(ctx, "filename.jpg")
defer reader.Close()
var b []bytes
count, err := reader.Read(b)
Write
writer, err := image.NewWriter(ctx, "filename.jpg", "image/jpeg")
defer writer.Close()
count, err := writer.Write(dataFromRequest)
ResizedURL
http.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    name := r.URL.Query().Get("name")       // GCS file name
    w := r.URL.Query().Get("width")
    h := r.URL.Query().Get("height")

    width, err := strconv.Atoi(w)
    height, err := strconv.Atoi(h)

    url, err := image.ResizedURL(ctx, name, width, height)
    http.Redirect(w, r, url, http.StatusMovedPermanently)
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReader

func NewReader(c context.Context, filename string) (io.ReadCloser, string, error)

NewReader returns a reader that reads the image from GCS

func NewWriter

func NewWriter(c context.Context, filename, contentType string) (io.WriteCloser, error)

NewWriter returns a writer that writes to GCS.

Types

type File

type File struct {
	Name string `json:"name"`
	Type string `json:"type"`

	// base64 encoded data passed up from client
	Data string `json:"data,omitempty" datastore:"-"`
}

File links data saved in an external storage

func (*File) Bytes

func (ra *File) Bytes() ([]byte, error)

Bytes trims the meta data from the encoded string and converts the data to []byte

type Store

type Store struct{}

Store provides the methods to save to the external storage service

func NewStore

func NewStore() Store

func (Store) CreateWithData

func (as Store) CreateWithData(c context.Context, data []byte, contentType string) (*File, error)

CreateWithData saves the passed in data as an attachment

func (Store) CreateWithURL

func (as Store) CreateWithURL(c context.Context, url string) (*File, error)

CreateWithURL performs an external fetch of the data with the URL and saves the returned data as an attachment

type Storer

type Storer interface {
	CreateWithData(c context.Context, data []byte, contentType string) (*File, error)
	CreateWithURL(c context.Context, url string) (*File, error)
}

Storer makes testing easier

Jump to

Keyboard shortcuts

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