Exemple #1
0
func Mkfile(
	c rpc.Client, l rpc.Logger, ret interface{}, key string, hasKey bool, fsize int64, extra *PutExtra) (err error) {

	url := UP_HOST + "/mkfile/" + strconv.FormatInt(fsize, 10)

	if extra.MimeType != "" {
		url += "/mimeType/" + encode(extra.MimeType)
	}
	if hasKey {
		url += "/key/" + encode(key)
	}
	for k, v := range extra.Params {
		url += fmt.Sprintf("/%s/%s", k, encode(v))
	}

	buf := make([]byte, 0, 176*len(extra.Progresses))
	for _, prog := range extra.Progresses {
		buf = append(buf, prog.Ctx...)
		buf = append(buf, ',')
	}
	if len(buf) > 0 {
		buf = buf[:len(buf)-1]
	}

	return c.CallWith(l, ret, url, "application/octet-stream", bytes.NewReader(buf), len(buf))
}
Exemple #2
0
func parseIndex(b []byte) (idx *index, err error) {
	if binary.LittleEndian.Uint32(b) != crc32.ChecksumIEEE(b[4:]) {
		err = ErrBrokenIndex
		return
	}
	idx = &index{}
	r := bytes.NewReader(b[4:])
	binary.Read(r, binary.LittleEndian, idx)
	return
}
Exemple #3
0
func Qiniu() {
	data["Content"] = "Qiniu"
	buf := make([]byte, 100, 100)
	r := qby.NewReader(buf)
	w := qby.NewWriter(buf)
	err := tpl.Execute(w, data)
	fmt.Println("error:", err)
	bs := r.Bytes()
	fmt.Println("reader bytes:", string(bs), "|")
	fmt.Println("origin bytes:", string(buf), "|")
	data["Content"] = "Ya"
	w.Reset()
	tpl.Execute(w, data)
	fmt.Println("reader2 bytes:", string(r.Bytes()), "|", string(w.Bytes()), "|")
}
Exemple #4
0
func New(req *http.Request) (r SeekableCloser, err error) {
	if req.Body == nil {
		return nil, ErrNoBody
	}
	var ok bool
	if r, ok = req.Body.(SeekableCloser); ok {
		return
	}
	b, err2 := ReadAll(req)
	if err2 != nil {
		return nil, err2
	}
	r = bytes.NewReader(b)
	req.Body = readCloser{r, req.Body}
	return
}
func (this *RoundPicer) Do(req ufop.UfopRequest) (result interface{}, resultType int, contentType string, err error) {
	//parse cmd
	cmdParams, pErr := this.parse(req.Cmd)
	if pErr != nil {
		err = pErr
		return
	}

	//check src image
	if matched, _ := regexp.MatchString("image/(png|jpeg)", req.Src.MimeType); !matched {
		err = errors.New("unsupported mimetype, only 'image/png' and 'image/jpeg' supported")
		return
	}

	if req.Src.Fsize > this.maxFileSize {
		err = errors.New("src image size too large, exceeds the limit")
		return
	}

	//download the image
	resp, respErr := http.Get(req.Src.Url)
	if respErr != nil || resp.StatusCode != http.StatusOK {
		if respErr != nil {
			err = errors.New(fmt.Sprintf("get image data failed, %s", respErr.Error()))
		} else {
			err = errors.New(fmt.Sprintf("get image data failed, %s", resp.Status))
			if resp.Body != nil {
				resp.Body.Close()
			}
		}
		return
	}
	defer resp.Body.Close()

	srcImgData, readErr := ioutil.ReadAll(resp.Body)
	if readErr != nil {
		err = errors.New(fmt.Sprintf("read image data failed, %s", readErr.Error()))
		return
	}

	var srcImg image.Image
	var decodeErr error

	switch req.Src.MimeType {
	case "image/png":
		srcImg, decodeErr = png.Decode(bytes.NewReader(srcImgData))
	case "image/jpeg":
		srcImg, decodeErr = jpeg.Decode(bytes.NewReader(srcImgData))
	}

	if decodeErr != nil {
		err = errors.New(fmt.Sprintf("decode image failed, %s", decodeErr.Error()))
		return
	}

	srcImgWidth := srcImg.Bounds().Dx()
	srcImgHeight := srcImg.Bounds().Dy()

	//parse cmd params, radius can be pixels or percentage
	radiusX, radiusY := getRadius(cmdParams, srcImgWidth, srcImgHeight)

	//init imagick
	imagick.Initialize()
	defer imagick.Terminate()

	//create mask
	maskDraw := imagick.NewMagickWand()
	defer maskDraw.Destroy()

	backDraw := imagick.NewPixelWand()
	defer backDraw.Destroy()
	backDraw.SetColor("none")

	//draw mask
	nErr := maskDraw.NewImage(uint(srcImgWidth), uint(srcImgHeight), backDraw)
	if nErr != nil {
		err = errors.New(fmt.Sprintf("create mask image failed, %s", nErr.Error()))
		return
	}

	backDraw.SetColor("white")
	roundDraw := imagick.NewDrawingWand()
	defer roundDraw.Destroy()
	roundDraw.SetFillColor(backDraw)
	roundDraw.RoundRectangle(0, 0, float64(srcImgWidth-1), float64(srcImgHeight-1), radiusX, radiusY)

	//draw round pic
	dErr := maskDraw.DrawImage(roundDraw)
	if dErr != nil {
		err = errors.New(fmt.Sprintf("draw mask image failed, %s", dErr.Error()))
		return
	}

	//load src image
	srcDraw := imagick.NewMagickWand()
	defer srcDraw.Destroy()
	rErr := srcDraw.ReadImageBlob(srcImgData)
	if rErr != nil {
		err = errors.New(fmt.Sprintf("read src image failed, %s", rErr.Error()))
		return
	}

	//composite the mask and the src image
	cErr := maskDraw.CompositeImage(srcDraw, imagick.COMPOSITE_OP_SRC_IN, 0, 0)
	if cErr != nil {
		err = errors.New(fmt.Sprintf("composite mask and src image failed, %s", cErr.Error()))
		return
	}

	//write dest image
	oTmpFpath := filepath.Join(os.TempDir(), fmt.Sprintf("roundpic_tmp_result_%d.png", time.Now().UnixNano()))
	wErr := maskDraw.WriteImage(oTmpFpath)
	if wErr != nil {
		err = errors.New(fmt.Sprintf("write dest image failed, %s", wErr.Error()))
		defer os.Remove(oTmpFpath)
		return
	}

	//write result
	result = oTmpFpath
	resultType = ufop.RESULT_TYPE_OCTECT_FILE
	contentType = "image/png"

	return
}