Exemplo n.º 1
0
func uploadBufDemo(r gio.Reader, key, uptoken string) {
	// @gist uploadBuf
	var err error
	var ret io.PutRet
	var extra = &io.PutExtra{
	// Params:   params,
	// MimeType: mieType,
	// Crc32:    crc32,
	// CheckCrc: CheckCrc,
	}

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

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

	//上传成功,处理返回值
	log.Print(ret.Hash, ret.Key)
	// @endgist
}
Exemplo n.º 2
0
func UploadReader(reader io.Reader, name string) error {
	var err error
	var ret qio.PutRet
	ACCESS_KEY = "NPfcHtb0e2EH7lCmmJot21MRr0lCel81S-QlUaJF"
	SECRET_KEY = "6DzF_oVRYhBkq0mqb4txThza_IfQEUey107VXaPq"
	var policy = qrs.PutPolicy{
		Scope: "isaac",
	}
	err = qio.Put(nil, &ret, policy.Token(nil), name, reader, nil)
	return err
}
Exemplo n.º 3
0
//martini Handler
func UploadHandlers() []martini.Handler {
	var upload = func(fileData FileData, data UploadData, w http.ResponseWriter) {
		retJsonp := &UploadRetJsonp{MessengerJs: MessengerJs, UpTime: data.UpTime}

		if strings.ToUpper(data.Dir) != "IMAGE" {
			retJsonp.Err(w, "dir wrong:"+data.Dir)
			return
		}

		imgFile, err := fileData.ImgFile.Open()
		if err != nil {
			retJsonp.Err(w, "ImgFile:"+err.Error())
			return
		}

		//取得bucket
		bucket, err := bucketdb.FindByName(data.Bucket)
		if err != nil {
			retJsonp.Err(w, "FindByName:"+err.Error())
			return
		}

		if bucket.Uptoken == "" {
			if err := bucket.NewUptoken(); err != nil {
				retJsonp.Err(w, "NewUptoken:"+err.Error())
				return
			}
			if err := bucket.Save(); err != nil {
				retJsonp.Err(w, "Save:"+err.Error())
				return
			}
		}

		//上传内容到Qiniu
		var ret qio.PutRet
		// ret       	变量用于存取返回的信息,详情见 qio.PutRet
		// uptoken   	为业务服务器端生成的上传口令
		// key:imgName	为文件存储的标识
		// r:imgFile   	为io.Reader类型,用于从其读取数据
		// extra     	为上传文件的额外信息,可为空, 详情见 qio.PutExtra, 可选
		err = qio.Put(nil, &ret, bucket.Uptoken, fileData.ImgName(), imgFile, nil)
		if err != nil {
			bucket.LogErr()
			retJsonp.Err(w, "Put:"+err.Error())
			return
		}

		//上传成功,返回给KindEditor
		//w.Header().Set("Content-type", "application/json")
		retJsonp.Respose(w, bucket.ImgUrl(ret.Key))
	}
	return []martini.Handler{binding.MultipartForm(FileData{}), binding.Form(UploadData{}), upload}
}
Exemplo n.º 4
0
func TestUpload(t *testing.T) {
	var err error
	var ret io.PutRet
	var extra = &io.PutExtra{}
	r, err := os.Open("/users/xxxx/Desktop/psu.jpeg")
	upToken := upToken("threeperson")
	err = io.Put(nil, &ret, upToken, "heads/1001/psu.jpeg", r, extra)

	if err != nil {
		log.Print("io.Put failed:", err)
		return
	}

	log.Print("result:", ret.Hash, ret.Key)
}
Exemplo n.º 5
0
func (driver *QiniuDriver) PutFile(key string, data io.Reader, appendData bool) (int64, error) {
	var err error
	var ret qio.PutRet
	var extra = &qio.PutExtra{}

	putPolicy := rs.PutPolicy{
		Scope: driver.bucket,
	}
	uptoken := putPolicy.Token(nil)

	rd := CountReader(data)
	err = qio.Put(nil, &ret, uptoken, strings.TrimLeft(key, "/"), rd, extra)
	if err != nil {
		return 0, err
	}

	return int64(rd.Size()), nil
}
Exemplo n.º 6
0
// 上传到七牛,并返回文件名
func uploadAvatarToQiniu(file io.ReadCloser, contentType string) (filename string, err error) {
	isValidateType := false
	for _, imgType := range []string{"image/png", "image/jpeg"} {
		if imgType == contentType {
			isValidateType = true
			break
		}
	}

	if !isValidateType {
		return "", errors.New("文件类型错误")
	}

	filenameExtension := ".jpg"
	if contentType == "image/png" {
		filenameExtension = ".png"
	}

	// 文件名:32位uuid,不带减号和后缀组成
	filename = strings.Replace(uuid.NewUUID().String(), "-", "", -1) + filenameExtension

	key := "avatar/" + filename

	ret := new(qiniuIo.PutRet)

	var policy = rs.PutPolicy{
		Scope: "gopher",
	}

	err = qiniuIo.Put(
		nil,
		ret,
		policy.Token(nil),
		key,
		file,
		nil,
	)

	if err != nil {
		return "", err
	}

	return filename, nil
}
Exemplo n.º 7
0
func (this *UploaderLogic) uploadMemoryFile(r gio.Reader, key string) (err error) {
	this.genUpToken()

	var ret io.PutRet
	var extra = &io.PutExtra{
	// Params:   params,
	// MimeType: mieType,
	// Crc32:    crc32,
	// CheckCrc: CheckCrc,
	}

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

	// 上传产生错误
	if err != nil {
		logger.Errorln("io.Put failed:", err)

		errInfo := make(map[string]interface{})
		err = json.Unmarshal([]byte(err.Error()), &errInfo)
		if err != nil {
			logger.Errorln("io.Put Unmarshal failed:", err)
			return
		}

		code, ok := errInfo["code"]
		if ok && code == 614 {
			err = nil
		}

		return
	}

	// 上传成功,处理返回值
	logger.Debugln(ret.Hash, ret.Key)

	return
}
Exemplo n.º 8
0
func (q Qiniu) Store(url string, option *media_library.Option, reader io.Reader) (err error) {

	var ret qnio.PutRet
	path := strings.Replace(url, "//"+getEndpoint(option), "", -1)
	// ret       变量用于存取返回的信息,详情见 io.PutRet
	// uptoken   为业务服务器端生成的上传口令
	// r         为io.Reader类型,用于从其读取数据
	// extra     为上传文件的额外信息,可为空, 详情见 io.PutExtra, 可选
	err = qnio.Put(nil, &ret, Uptoken, path, reader, nil)

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

	//上传成功,处理返回值
	// log.Print(ret.Hash, ret.Key)
	return
}
Exemplo n.º 9
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
}
Exemplo n.º 10
0
func SaveImageToQiniu(m *models.Image, r goio.ReadSeeker, mime string, filename string, created time.Time, bucketName string) error {
	var ext string

	// check 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 img image.Image
	var err error
	switch ext {
	case ".jpg":
		m.Ext = 1
		img, err = jpeg.Decode(r)
	case ".png":
		m.Ext = 2
		img, err = png.Decode(r)
	case ".gif":
		m.Ext = 3
		img, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	m.Width = img.Bounds().Dx()
	m.Height = img.Bounds().Dy()
	m.Created = created

	//save to database
	if err := m.Insert(); err != nil || m.Id <= 0 {
		return err
	}

	m.Token = m.GetToken()
	if err := m.Update(); err != nil {
		return err
	}

	var key = m.Token

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

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

	err = io.Put(nil, &putRet, uptoken, key, r, putExtra)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 11
0
// URL: /upload/image
// 编辑器上传图片,接收后上传到七牛
func uploadImageHandler(handler *Handler) {
	defer dps.Persist()

	file, header, err := handler.Request.FormFile("editormd-image-file")
	if err != nil {
		panic(err)
		return
	}
	defer file.Close()

	// 检查是否是jpg或png文件
	uploadFileType := header.Header["Content-Type"][0]

	filenameExtension := ""
	if uploadFileType == "image/jpeg" {
		filenameExtension = ".jpg"
	} else if uploadFileType == "image/png" {
		filenameExtension = ".png"
	} else if uploadFileType == "image/gif" {
		filenameExtension = ".gif"
	}

	if filenameExtension == "" {
		handler.renderJson(map[string]interface{}{
			"success": 0,
			"message": "不支持的文件格式,请上传 jpg/png/gif 图片",
		})
		return
	}

	// 上传到七牛
	// 文件名:32位uuid+后缀组成
	filename := strings.Replace(uuid.NewUUID().String(), "-", "", -1) + filenameExtension
	key := "upload/image/" + filename

	ret := new(qiniuIo.PutRet)

	var policy = rs.PutPolicy{
		Scope: "gopher",
	}

	err = qiniuIo.Put(
		nil,
		ret,
		policy.Token(nil),
		key,
		file,
		nil,
	)

	if err != nil {
		panic(err)

		handler.renderJson(map[string]interface{}{
			"success": 0,
			"message": "图片上传到七牛失败",
		})

		return
	}

	handler.renderJson(map[string]interface{}{
		"success": 1,
		"url":     "http://77fkk5.com1.z0.glb.clouddn.com/" + key,
	})
}
Exemplo n.º 12
0
func (this *Unzipper) Do(req ufop.UfopRequest) (result interface{}, resultType int, contentType string, err error) {
	//parse command
	bucket, prefix, overwrite, pErr := this.parse(req.Cmd)
	if pErr != nil {
		err = pErr
		return
	}

	//check mimetype
	if req.Src.MimeType != "application/zip" {
		err = errors.New("unsupported mimetype to unzip")
		return
	}
	//check zip file length
	if req.Src.Fsize > this.maxZipFileLength {
		err = errors.New("src zip file length exceeds the limit")
		return
	}

	//get resource
	resUrl := req.Src.Url
	resResp, respErr := http.Get(resUrl)
	if respErr != nil || resResp.StatusCode != 200 {
		if respErr != nil {
			err = errors.New(fmt.Sprintf("retrieve resource data failed, %s", respErr.Error()))
		} else {
			err = errors.New(fmt.Sprintf("retrieve resource data failed, %s", resResp.Status))
			if resResp.Body != nil {
				resResp.Body.Close()
			}
		}
		return
	}
	defer resResp.Body.Close()

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

	//read zip
	respReader := bytes.NewReader(respData)
	zipReader, zipErr := zip.NewReader(respReader, int64(respReader.Len()))
	if zipErr != nil {
		err = errors.New(fmt.Sprintf("invalid zip file, %s", zipErr.Error()))
		return
	}
	zipFiles := zipReader.File
	//check file count
	zipFileCount := len(zipFiles)
	if zipFileCount > this.maxFileCount {
		err = errors.New("zip files count exceeds the limit")
		return
	}
	//check file size
	for _, zipFile := range zipFiles {
		fileSize := zipFile.UncompressedSize64
		//check file size
		if fileSize > this.maxFileLength {
			err = errors.New("zip file length exceeds the limit")
			return
		}
	}

	//set up host
	conf.UP_HOST = "http://up.qiniu.com"
	rputSettings := rio.Settings{
		ChunkSize: 4 * 1024 * 1024,
		Workers:   1,
	}
	rio.SetSettings(&rputSettings)
	var rputThreshold uint64 = 100 * 1024 * 1024
	policy := rs.PutPolicy{
		Scope: bucket,
	}
	var unzipResult UnzipResult
	unzipResult.Files = make([]UnzipFile, 0)
	var tErr error
	//iterate the zip file
	for _, zipFile := range zipFiles {
		fileInfo := zipFile.FileHeader.FileInfo()
		fileName := zipFile.FileHeader.Name
		fileSize := zipFile.UncompressedSize64

		if !utf8.Valid([]byte(fileName)) {
			fileName, tErr = utils.Gbk2Utf8(fileName)
			if tErr != nil {
				err = errors.New(fmt.Sprintf("unsupported file name encoding, %s", tErr.Error()))
				return
			}
		}

		if fileInfo.IsDir() {
			continue
		}

		zipFileReader, zipErr := zipFile.Open()
		if zipErr != nil {
			err = errors.New(fmt.Sprintf("open zip file content failed, %s", zipErr.Error()))
			return
		}
		defer zipFileReader.Close()

		unzipData, unzipErr := ioutil.ReadAll(zipFileReader)
		if unzipErr != nil {
			err = errors.New(fmt.Sprintf("unzip the file content failed, %s", unzipErr.Error()))
			return
		}
		unzipReader := bytes.NewReader(unzipData)

		//save file to bucket
		fileName = prefix + fileName
		if overwrite {
			policy.Scope = bucket + ":" + fileName
		}
		uptoken := policy.Token(this.mac)
		var unzipFile UnzipFile
		unzipFile.Key = fileName
		if fileSize <= rputThreshold {
			var fputRet fio.PutRet
			fErr := fio.Put(nil, &fputRet, uptoken, fileName, unzipReader, nil)
			if fErr != nil {
				unzipFile.Error = fmt.Sprintf("save unzip file to bucket error, %s", fErr.Error())
			} else {
				unzipFile.Hash = fputRet.Hash
			}

		} else {
			var rputRet rio.PutRet
			rErr := rio.Put(nil, &rputRet, uptoken, fileName, unzipReader, int64(fileSize), nil)
			if rErr != nil {
				unzipFile.Error = fmt.Sprintf("save unzip file to bucket error, %s", rErr.Error())
			} else {
				unzipFile.Hash = rputRet.Hash
			}
		}
		unzipResult.Files = append(unzipResult.Files, unzipFile)
	}

	//write result
	result = unzipResult
	resultType = ufop.RESULT_TYPE_JSON
	contentType = ufop.CONTENT_TYPE_JSON

	return
}