var UploadBlob = view.NewViewURLWrapper(view.RenderViewBindURLArgs( func(ctx *view.Context, thumbnailSize int) error { formatError := func(err error) error { config.Logger.Println("UploadBlob:", err) 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() blob, err := NewBlobFromReader(filename, r) if err != nil { return formatError(err) } ctx.Response.Printf(`{success: true, blobID: "%s"}`, blob.ID) return nil }, ))
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() // debug.Print("UploadImage", filename, thumbnailSize) 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.GetURL().URL(ctx), thumbnailSize) return nil }, ))
"github.com/ungerik/go-start/utils" "github.com/ungerik/go-start/view" "io" ) var View view.ViewWithURL = view.NewViewURLWrapper(view.RenderView( func(context *view.Context, writer *utils.XMLWriter) error { reader, contentType, err := Config.Backend.ImageVersionReader(context.PathArgs[0]) if err != nil { if _, ok := err.(ErrInvalidImageID); ok { return view.NotFound(context.PathArgs[0] + "/" + context.PathArgs[1] + " not found") } return err } _, err = io.Copy(writer, reader) if err != nil { return err } err = reader.Close() if err != nil { return err } context.Header().Set("Content-Type", contentType) return nil }, )) func ViewPath(name string) view.ViewPath { return view.ViewPath{Name: name, Args: 2, View: View} }
var AllThumbnailsAPI = view.NewViewURLWrapper(view.RenderViewBindURLArgs( func(ctx *view.Context, thumbnailSize int) error { // ctx.Response.SetContentType("application/json") ctx.Response.SetContentTypeByExt(".json") ctx.Response.WriteString("[\n") first := true i := Config.Backend.ImageIterator() for doc := i.Next(); doc != nil; doc = i.Next() { image := doc.(*Image) thumbnail, err := image.Thumbnail(thumbnailSize) if err != nil { return err } if first { first = false } else { ctx.Response.WriteString(",\n") } ctx.Response.Printf( `{"id": "%s", "title": "%s", "url": "%s"}`, image.ID, image.TitleOrFilename(), thumbnail.GetURL().URL(ctx), ) } if i.Err() != nil { return i.Err() } ctx.Response.WriteString("\n]") return nil }, ))
"io" "github.com/ungerik/go-start/view" ) var View = 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 }, )) func ViewPath(name string) view.ViewPath { return view.ViewPath{Name: name, Args: 2, View: View} }
package media import ( "io" "github.com/ungerik/go-start/config" "github.com/ungerik/go-start/view" ) var FileView = view.NewViewURLWrapper(view.RenderView( func(ctx *view.Context) error { reader, _, contentType, err := Config.Backend.FileReader(ctx.URLArgs[0]) if err != nil { if _, ok := err.(ErrNotFound); ok { err = view.NotFound(ctx.URLArgs[0] + "/" + ctx.URLArgs[1] + " not found") config.Logger.Println("FileView:", err) return err } return err } defer reader.Close() _, err = io.Copy(ctx.Response, reader) if err != nil { config.Logger.Println("FileView:", err) return err } ctx.Response.Header().Set("Content-Type", contentType) return nil }, ))