Example #1
0
func (this *FdfsStorage) Upload(bts []byte, fileExt string) (string, util.Error) {
	if e := this.initFdfsClient(); e.Err != nil {
		return "", e
	}
	config := models.Config{Cat: this.Cat}
	groups, e := config.GetGroups()
	if e.Err != nil {
		return "", e
	}
	if uploadcount == 99999999 {
		uploadcount = 0
	}
	i := uploadcount % len(groups)
	uploadcount = 0
	g := groups[i]

	var result util.Error = util.Error{}
	if this.Cat != nil {
		tran := this.Cat.NewTransaction(CATTITLE, "Fdfs.Upload")
		util.LogEvent(this.Cat, "Size", util.GetImageSizeDistribution(len(bts)), map[string]string{"size": strconv.Itoa(len(bts))})

		defer func() {
			if result.Err != nil && result.IsNormal {
				tran.SetStatus(result.Err)
			} else {
				tran.SetStatus("0")
			}
			tran.Complete()
		}()
	}
	path, err := fdfsClient.UploadByBuffer(g, bts, fileExt)
	if err != nil {
		util.LogErrorEvent(this.Cat, ERRORTYPE_FDFSUPLOADERR, err.Error())
		result = util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_FDFSUPLOADERR}
		return "", result
	}
	return path, result
}