func (this *FdfsStorage) initFdfsClient() util.Error { if fdfsClient == nil { lock <- 1 defer func() { <-lock }() if fdfsClient != nil { return util.Error{} } config := models.Config{Cat: this.Cat} fdfsdomain, e := config.GetFdfsDomain() if e.Err != nil { return e } fdfsport, e := config.GetFdfsPort() if e.Err != nil { return e } var err error fdfsClient, err = fdfs.NewFdfsClient([]string{fdfsdomain}, fdfsport) if err != nil { util.LogErrorEvent(this.Cat, ERRORTYPE_FDFSCONNECTIONERR, err.Error()) return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_FDFSCONNECTIONERR} } } return util.Error{} }
// @router /config/get/ [get] func (this *ConfigController) Get() { channel := this.GetString("channel") key := this.GetString("key") config := models.Config{} m, e := config.GetConfigs() if e.Err != nil { this.Ctx.WriteString(fmt.Sprintf("%v", e.Err)) } var result interface{} if channel != "" { m1, exists := m[channel] if !exists { this.Ctx.WriteString("No record") return } if key != "" { m2, exists := m1[key] if !exists { this.Ctx.WriteString("No record") return } result = m2 } else { result = m1 } } else { result = m } bts, err := json.Marshal(result) if err != nil { this.Ctx.WriteString(err.Error()) } else { this.Ctx.WriteString(string(bts)) } }
// @router /config/add/ [get] func (this *ConfigController) Add() { channel := this.GetString("channel") key := this.GetString("key") value := this.GetString("value") config := models.Config{ChannelCode: channel, Key: key, Value: value} err := config.Insert() if err != nil { this.Ctx.WriteString(err.Error()) } else { this.Ctx.WriteString("sucess") } }
// @router /config/update/ [get] func (this *ConfigController) Update() { channel := this.GetString("channel") key := this.GetString("key") value := this.GetString("value") if channel == "" || key == "" { this.Ctx.WriteString("params is invalid") return } config := models.Config{ChannelCode: channel, Key: key, Value: value} err := config.UpdateValue() if err != nil { this.Ctx.WriteString(err.Error()) } else { this.Ctx.WriteString("sucess") } }
// @router /whitelist/add/ [get] func (c *WhitelistController) Add() { channel := c.GetString("channel") isMatch, err := regexp.Match(TgPattern, []byte(channel)) if err != nil { //err c.Ctx.WriteString(err.Error()) return } if !isMatch { //ErrIllegalChannel c.Ctx.WriteString(ErrIllegalChannel.Error()) return } size := c.GetString("size") isMatch, err = regexp.Match(SizePattern, []byte(size)) if err != nil { //err c.Ctx.WriteString(err.Error()) return } if !isMatch { //ErrIllegalSize c.Ctx.WriteString(ErrIllegalSize.Error()) return } var conf models.Config = models.Config{ ChannelCode: channel, Key: "sizes", } err = conf.AddSize(size) if err != nil { //err c.Ctx.WriteString(conf.Value) c.Ctx.WriteString(err.Error()) return } c.Ctx.WriteString(conf.Value) }
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 }
// @router /whitelist/getsizes/ [get] func (c *WhitelistController) GetSizes() { channel := c.GetString("channel") isMatch, err := regexp.Match(TgPattern, []byte(channel)) if err != nil { c.Ctx.WriteString(err.Error()) return } if !isMatch { //ErrIllegalChannel c.Ctx.WriteString(ErrIllegalChannel.Error()) return } var conf models.Config = models.Config{ ChannelCode: channel, Key: "sizes", } sizes, err := conf.GetSizes() if err != nil { c.Ctx.WriteString(err.Error()) return } c.Ctx.WriteString(sizes) }