Example #1
0
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
	},
))
Example #2
0
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
	},
))
Example #3
0
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
	},
))