示例#1
0
func (widget WidgetController) EditorPost() revel.Result {
	revel.TRACE.Printf("POST >> widget.editor ...")

	var callback string
	widget.Params.Bind(&callback, "CKEditorFuncNum")

	revel.INFO.Printf("POST >> widget.editor ...callback(%s)", callback)

	widget.Validation.Required(widget.Params.Files["upload"])
	if widget.Validation.HasErrors() {
		return widget.RenderJson(WidgetResponse{Code: 401, Message: "files absent"})
	}

	file := widget.Params.Files["upload"][0]
	fname := file.Filename
	ftype := file.Header.Get("Content-Type")
	flength := file.Header.Get("Content-Length")

	revel.INFO.Printf("POST >> widget.upload file(%s:%s:%s) ...", fname, ftype, flength)

	fd, err := file.Open()
	if err != nil {
		html := fmt.Sprintf("<html><body><script type=\"text/javascript\">alert('%s')</script></body></html>", err.Error())
		return widget.RenderHtml(html)
	}
	defer fd.Close()

	var url string
	if !QiniuEnable {
		sub := fmt.Sprintf("%d", time.Now().Unix()%31)
		dir := path.Join(revel.BasePath, "upload", sub)
		os.MkdirAll(dir, os.ModePerm)

		wr, err := os.Create(path.Join(dir, fname))
		if err != nil {
			return widget.RenderJson(WidgetResponse{Code: 402, Message: err.Error()})
		}
		defer wr.Close()

		if _, err := goio.Copy(wr, fd); err != nil {
			return widget.RenderJson(WidgetResponse{Code: 403, Message: err.Error()})
		}

		url = fmt.Sprintf("/upload/%s/%s", sub, fname)
	} else {
		var ret io.PutRet
		var extra = &io.PutExtra{}

		uptoken := QiniuToken(QiniuBucket)
		err = io.PutWithoutKey(nil, &ret, uptoken, fd, extra)
		if err != nil {
			return widget.RenderJson(WidgetResponse{Code: 405, Message: err.Error()})
		}
		url = fmt.Sprintf("//%s/%s", QiniuDomain, ret.Key)
	}

	html := fmt.Sprintf("<html><body><script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(%s, '%s')</script></body></html>",
		callback, url)
	return widget.RenderHtml(html)
}
示例#2
0
func uploadBufWithoutKeyDemo(r gio.Reader, key, uptoken string) {
	// @gist uploadBufWithoutKey
	var err error
	var ret io.PutRet
	var extra = &io.PutExtra{
	// Params:   params,
	// MimeType: mieType,
	// Crc32:    crc32,
	// CheckCrc: CheckCrc,
	}

	// ret       变量用于存取返回的信息,详情见 io.PutRet
	// uptoken   为业务服务器端生成的上传口令
	// r         为io.Reader类型,用于从其读取数据
	// extra     为上传文件的额外信息,可为空, 详情见 io.PutExtra, 可选
	err = io.PutWithoutKey(nil, &ret, uptoken, r, extra)

	if err != nil {
		//上传产生错误
		log.Print("io.Put failed:", err)
		return
	}

	//上传成功,处理返回值
	log.Print(ret.Hash, ret.Key)
	// @endgist
}
示例#3
0
func (widget WidgetController) UploadPost() revel.Result {
	revel.TRACE.Printf("POST >> widget.upload ...")

	widget.Validation.Required(widget.Params.Files["upload"])
	if widget.Validation.HasErrors() {
		return widget.RenderJson(WidgetResponse{Code: 401, Message: "files absent"})
	}

	file := widget.Params.Files["upload"][0]
	fname := file.Filename
	ftype := file.Header.Get("Content-Type")
	flength := file.Header.Get("Content-Length")

	revel.INFO.Printf("POST >> widget.upload file(%s:%s:%s) ...", fname, ftype, flength)

	fd, err := file.Open()
	if err != nil {
		return widget.RenderJson(WidgetResponse{Code: 402, Message: err.Error()})
	}
	defer fd.Close()

	var url string
	if !QiniuEnable {
		sub := fmt.Sprintf("%d", time.Now().Unix()%31)
		dir := path.Join(revel.BasePath, "upload", sub)
		os.MkdirAll(dir, os.ModePerm)

		wr, err := os.Create(path.Join(dir, fname))
		if err != nil {
			return widget.RenderJson(WidgetResponse{Code: 402, Message: err.Error()})
		}
		defer wr.Close()

		if _, err := goio.Copy(wr, fd); err != nil {
			return widget.RenderJson(WidgetResponse{Code: 403, Message: err.Error()})
		}

		url = fmt.Sprintf("/upload/%s/%s", sub, fname)
	} else {
		var ret io.PutRet
		var extra = &io.PutExtra{}

		uptoken := QiniuToken(QiniuBucket)
		err = io.PutWithoutKey(nil, &ret, uptoken, fd, extra)
		if err != nil {
			return widget.RenderJson(WidgetResponse{Code: 405, Message: err.Error()})
		}
		url = fmt.Sprintf("//%s/%s", QiniuDomain, ret.Key)
	}

	return widget.RenderJson(WidgetResponse{Code: 0, Data: map[string]interface{}{
		"url": url,
	}})
}
示例#4
0
func main() {
	c := (&rs.PutPolicy{Scope: "test"}).Token(nil)
	fmt.Println(c)
	var ret io.PutRet
	s := "fdsfdsgdfsg"
	for i := 0; i < 100; i++ {
		s = s + s
		io.PutWithoutKey(nil, &ret, c, strings.NewReader(s), nil)
		fmt.Println(len(s), ret)
	}

}
示例#5
0
func Upload(file_name string, rd goio.Reader) (string, error) {
	var url string
	if !QiniuEnable {
		sub := fmt.Sprintf("%d", time.Now().Unix()%31)
		dir := path.Join(revel.BasePath, "upload", sub)
		os.MkdirAll(dir, os.ModePerm)

		fname := fmt.Sprintf("%s%s", time.Now().Format("20060102150405"), file_name)
		wr, err := os.Create(path.Join(dir, fname))
		if err != nil {
			return "", err
		}
		defer wr.Close()

		if _, err := goio.Copy(wr, rd); err != nil {
			return "", err
		}

		url = fmt.Sprintf("/upload/%s/%s", sub, fname)
	} else {
		var ret io.PutRet
		var extra = &io.PutExtra{}

		uptoken := QiniuToken(QiniuBucket)
		if file_name != "" {
			if err := io.Put(nil, &ret, uptoken, file_name, rd, extra); err != nil {
				return "", err
			}
		} else {
			if err := io.PutWithoutKey(nil, &ret, uptoken, rd, extra); err != nil {
				return "", err
			}
		}
		url = fmt.Sprintf("http://%s/%s", QiniuDomain, ret.Key)
	}
	return url, nil
}
示例#6
0
文件: auth.go 项目: varding/wetalk
//upload user avatar
func UploadUserAvatarToQiniu(r io.ReadSeeker, filename string, mime string, bucketName string, user *models.User) error {
	var ext string

	// test image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var err error
	switch ext {
	case ".jpg":
		_, err = jpeg.Decode(r)
	case ".png":
		_, err = png.Decode(r)
	case ".gif":
		_, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	//reset reader pointer
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}
	var data []byte
	if data, err = ioutil.ReadAll(r); err != nil {
		return err
	}

	if len(data) > setting.AvatarImageMaxLength {
		return fmt.Errorf("avatar image size too large", filename)
	}

	//reset reader pointer again
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	//save to qiniu
	var uptoken = utils.GetQiniuUptoken(bucketName)
	var putRet qio.PutRet
	var putExtra = &qio.PutExtra{
		MimeType: mime,
	}

	err = qio.PutWithoutKey(nil, &putRet, uptoken, r, putExtra)
	if err != nil {
		return err
	}

	//update user
	user.AvatarKey = putRet.Key
	if err := user.Update("AvatarKey", "Updated"); err != nil {
		return err
	}
	return nil
}