func (this ImageRequest) DownloadZip(r *request.LoadZipRequest) (response.LoadZipResponse, util.Error) { t := "ParamInvalid" if len(r.Files.LoadFiles) < 1 { util.LogEvent(this.Cat, t, "NoLoadFiles", nil) return response.LoadZipResponse{}, util.Error{IsNormal: true, Err: errors.New("No files in request"), Type: "NoFilesInRequest"} } files := make(map[string][]byte) for _, file := range r.Files.LoadFiles { name := path.Base(strings.Replace(file.FilePath, "\\", "/", -1)) if len(file.Rename) > 0 { name = file.Rename + path.Ext(file.FilePath) } storage, e := this.getStorageBySource(file.FilePath, file.IsSource) if e.Err != nil { return response.LoadZipResponse{}, e } util.LogEvent(this.Cat, "DownloadZip", this.GetChannel(file.FilePath), map[string]string{"uri": file.FilePath}) bts, e := storage.Download() if e.Err != nil { return response.LoadZipResponse{}, e } files[name] = bts } bts, e := util.Zip(files) if e.Err != nil { return response.LoadZipResponse{}, e } bts = []byte(soapparse.B64.EncodeToString(bts)) return response.LoadZipResponse{FileBytes: bts}, util.Error{} }
func (this ImageRequest) checkSaveCheckItem(r *request.SaveRequest) util.Error { t := "CheckFail" //if r.CheckItem == nil { // return util.Error{} //} if r.CheckItem.IsOtherImage { if !isSvg(r.FileBytes) { util.LogEvent(this.Cat, t, "FormatInvalid", map[string]string{"detail": "image isn't svg!"}) return util.Error{IsNormal: true, Err: errors.New("image isn't svg!"), Type: t} } } else { img, _, err := image.Decode(bytes.NewReader(r.FileBytes)) if err != nil { util.LogEvent(this.Cat, t, "FormatInvalid", map[string]string{"detail": err.Error()}) return util.Error{IsNormal: true, Err: err, Type: t} } //todo check img format if r.CheckItem.MinWidth > 0 && r.CheckItem.MinWidth > img.Bounds().Dx() { util.LogEvent(this.Cat, t, "LessMinWidth", map[string]string{"detail": util.JoinString("MinWidth:"+strconv.Itoa(r.CheckItem.MinWidth), " ImageWidth:", strconv.Itoa(img.Bounds().Dx()))}) return util.Error{IsNormal: true, Err: errors.New("image width is less minwidth!"), Type: t} } if r.CheckItem.MinHeight > 0 && r.CheckItem.MinHeight > img.Bounds().Dy() { util.LogEvent(this.Cat, t, "LessMinHeight", map[string]string{"detail": util.JoinString("MinHeight:"+strconv.Itoa(r.CheckItem.MinHeight), " ImageHeight:", strconv.Itoa(img.Bounds().Dy()))}) return util.Error{IsNormal: true, Err: errors.New("image heigth is less minheight!"), Type: t} } } if r.CheckItem.MaxBytes > 0 && int(r.CheckItem.MaxBytes) < len(r.FileBytes) { util.LogEvent(this.Cat, t, "BeyondMaxSize", map[string]string{"detail": util.JoinString("MaxSize:"+strconv.Itoa(int(r.CheckItem.MaxBytes)), " ImageSize:", strconv.Itoa(len(r.FileBytes)))}) return util.Error{IsNormal: true, Err: errors.New("image size beyond max size"), Type: t} } return util.Error{Err: nil, IsNormal: true} }
func (h *ImageProcessHandler) recUrlEvt(c cat.Cat, imagePath string, r *http.Request) { util.LogEvent(c, "URL", "URL.Method", map[string]string{ "Http": fmt.Sprintf("%s %s", r.Method, imagePath), }) util.LogEvent(c, "image", fmt.Sprintf("%s:%d", util.LocalIP(), h.listenPort), nil) util.LogEvent(c, "URL", "URL.Client", map[string]string{ "clientip": util.HttpClietIP(r), "serverip": util.LocalIP(), "proto": r.Proto, "referer": r.Referer(), }) }
func View(this *ViewController, loadImgRequest *request.LoadImgRequest) { var result = util.Error{} Cat := cat.Instance() title := "/fdupload/target" if loadImgRequest.IsSource { title = "/fdupload/source" } tran := Cat.NewTransaction("URL", title) defer func() { if p := recover(); p != nil { Cat.LogPanic(p) } if result.Err != nil { tran.SetStatus(result.Err) } else { tran.SetStatus("0") } tran.Complete() }() util.LogEvent(Cat, "URL", "URL.Client", map[string]string{ "clientip": util.GetClientIP(this.Ctx.Request), "serverip": util.GetIP(), "proto": this.Ctx.Request.Proto, "referer": this.Ctx.Request.Referer(), //"agent": request.UserAgent(), }) util.LogEvent(Cat, "URL", "URL.Method", map[string]string{ "Http": this.Ctx.Request.Method + " " + loadImgRequest.FilePath, }) imgRequest := business.ImageRequest{} imgRequest.Cat = Cat resp, e := imgRequest.Download(loadImgRequest) if e.Err != nil { this.Ctx.WriteString(e.Err.(error).Error()) } else { //this.Ctx.Output.ContentType("image/Jpeg") //this.Ctx.Output.Body(resp.FileBytes) bts, err := soapparse.B64.DecodeString(string(resp.FileBytes)) if err != nil { this.Ctx.WriteString(err.Error()) } else { this.Ctx.Output.Header("Content-Type", "image/jpeg") this.Ctx.Output.Body(bts) } } }
func (this *NfsStorage) Download() ([]byte, util.Error) { var ( bts []byte err error ) var messagesize int if this.Cat != nil { tran := this.Cat.NewTransaction(CATTITLE, "Nfs.Download") tran.AddData("path", this.Path) defer func() { if err != nil { tran.SetStatus(err) } else { util.LogEvent(this.Cat, "Size", util.GetImageSizeDistribution(messagesize), map[string]string{"size": strconv.Itoa(messagesize)}) tran.SetStatus("0") } tran.Complete() }() } bts, err = ioutil.ReadFile(this.Path) if err != nil { util.LogErrorEvent(this.Cat, ERRORTYPE_NFSDOWNLOADERR, err.Error()) return []byte{}, util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_NFSDOWNLOADERR} } messagesize = len(bts) return bts, util.Error{} }
func (this *FdfsStorage) Download() ([]byte, util.Error) { if e := this.initFdfsClient(); e.Err != nil { return nil, e } var ( bts []byte err error result = util.Error{} ) var messagesize int if this.Cat != nil { tran := this.Cat.NewTransaction(CATTITLE, "Fdfs.Download") tran.AddData("path", this.Path) defer func() { if result.Err != nil && result.IsNormal { tran.SetStatus(result.Err) } else { util.LogEvent(this.Cat, "Size", util.GetImageSizeDistribution(messagesize), map[string]string{"size": strconv.Itoa(messagesize)}) tran.SetStatus("0") } tran.Complete() }() } bts, err = fdfsClient.DownloadToBuffer(this.Path, this.Cat) if err != nil { util.LogErrorEvent(this.Cat, ERRORTYPE_FDFSDOWNLOADERR, err.Error()) result = util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_FDFSDOWNLOADERR} return []byte{}, result } messagesize = len(bts) return bts, result }
func (m *Master) recRebootEvt() { cat := cat.Instance() tran := cat.NewTransaction("System", "Reboot") defer func() { tran.SetStatus("0") tran.Complete() }() util.LogEvent(cat, "Reboot", util.JoinString(util.GetIP(), ":", strconv.Itoa(m.port)), nil) }
func (this ImageRequest) checkSaveRequest(r *request.SaveRequest) util.Error { t := "ParamInvalid" if r.Channel == "" { util.LogEvent(this.Cat, t, "ChannelEmpty", nil) return util.Error{IsNormal: true, Err: errors.New("Channel can't be empty"), Type: t} } if r.FileBytes == nil || len(r.FileBytes) < 1 { util.LogEvent(this.Cat, t, util.JoinString(r.Channel, "-FileBytesEmpty"), nil) return util.Error{IsNormal: true, Err: errors.New("FileBytes can't be empty"), Type: t} } channel := models.Channel{Cat: this.Cat} channels, err := channel.GetAll() if err.Err != nil { return err } _, exists := channels[r.Channel] if !exists { util.LogEvent(this.Cat, t, util.JoinString(r.Channel, "-NoRegister"), nil) return util.Error{IsNormal: true, Err: errors.New(util.JoinString("channel[", r.Channel, "] isn't register!")), Type: t} } return util.Error{} }
func (this *ImageIndex) ParseName(imgName string) util.Error { imageName := this.DropExtension(imgName) if len(imageName) != NEWIMAGENAMELENGTH { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "LengthInvalid", nil) return util.Error{IsNormal: true, Err: errors.New("imagename length is invalid"), Type: ERRTYPE_IMAGENAMEINVALID} } swithoutcompute := util.Substr(imageName, 0, NEWIMAGENAMELENGTH-4) scompute := util.Substr(imageName, NEWIMAGENAMELENGTH-4, 4) if util.Compute(swithoutcompute) != scompute { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "ComputeFail", nil) return util.Error{IsNormal: true, Err: errors.New("Compute check faile."), Type: ERRTYPE_IMAGENAMEINVALID} } channelCode := util.Substr(imageName, 0, 2) tableZone, err := strconv.ParseInt(util.Substr(imageName, 2, 2), 36, 10) if err != nil { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "TableZoneInvalid", nil) return util.Error{IsNormal: true, Err: errors.New("tablezone is invalid."), Type: ERRTYPE_IMAGENAMEINVALID} } partitionKey, err := strconv.ParseInt(util.Substr(imageName, 4, 2), 36, 10) if err != nil { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "PartitionInvalid", nil) return util.Error{IsNormal: true, Err: errors.New("partition is invalid."), Type: ERRTYPE_IMAGENAMEINVALID} } version := util.Substr(imageName, 6, 1) idx, err := strconv.ParseInt(util.Substr(imageName, 7, 10), 36, 10) if err != nil { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "IndexInvalid", nil) return util.Error{IsNormal: true, Err: errors.New("index is invalid."), Type: ERRTYPE_IMAGENAMEINVALID} } this.ChannelCode = channelCode this.Id = idx this.PartitionKey = int16(partitionKey) this.TableZone = int(tableZone) this.Version = version return util.Error{} }
func (this ImageRequest) Download(r *request.LoadImgRequest) (response.LoadImgResponse, util.Error) { storage, e := this.getStorageBySource(r.FilePath, r.IsSource) if e.Err != nil { return response.LoadImgResponse{}, e } util.LogEvent(this.Cat, "Download", this.GetChannel(r.FilePath), map[string]string{"uri": r.FilePath}) bts, e := storage.Download() if e.Err != nil { return response.LoadImgResponse{}, e } bts = []byte(soapparse.B64.EncodeToString(bts)) return response.LoadImgResponse{FileBytes: bts}, util.Error{} }
func (w *ImageProcessWorker) Start(opt StartOpt) <-chan error { if !opt.NewHosting { opts := &web.ImageProcessOptions{ListenAddress: fmt.Sprintf("127.0.0.1:%d", w.Port)} webHandler := web.NewImageProcessWebHandler(opts) util.LogEvent(cat.Instance(), "Reboot", fmt.Sprintf("%s:%d", util.LocalIP(), w.Port), nil) go webHandler.Run() return webHandler.ListenError() } else { cmd := exec.Command("go", "run", "imgsvrd.go", "-process.worker", "-port", strconv.Itoa(w.Port)) err := cmd.Start() if err != nil { log.Errorf("start worker=%d failed. error=%s", w.Port, err.Error()) util.LogErrorEvent(cat.Instance(), "DaemonProcess.StartWorkerError", err.Error()) w.startErrCh <- err } } return w.startErrCh }
func (this *ImageIndex) GetStorage() util.Error { if this.Id < 1 || this.TableZone < 1 || this.PartitionKey < 1 { util.LogErrorEvent(this.Cat, ERRTYPE_GETIMAGEINDEX, "Parameter is Invalid") return util.Error{IsNormal: true, Err: errors.New("getimageindex parameters is invalid"), Type: ERRTYPE_GETIMAGEINDEX} } var result util.Error = util.Error{} if this.Cat != nil { tran := this.Cat.NewTransaction(DBTITLE, "ImageIndex.GetStorage") defer func() { if result.Err != nil { tran.SetStatus(result.Err) } else { tran.SetStatus("0") } tran.AddData("TabeZone", strconv.Itoa(this.TableZone)) tran.AddData("Id", strconv.FormatInt(this.Id, 10)) tran.AddData("PartitionKey", strconv.Itoa(int(this.PartitionKey))) tran.Complete() }() } o := orm.NewOrm() o.Using(getDBString(this.TableZone)) res := make(orm.Params) nums, err := o.Raw(util.JoinString("SELECT storagepath,storagetype FROM imageindex_", strconv.Itoa(this.TableZone), " WHERE id=? AND partitionKey=?"), this.Id, this.PartitionKey).RowsToMap(&res, "storagepath", "storagetype") if err != nil { util.LogErrorEvent(this.Cat, ERRTYPE_GETIMAGEINDEX, err.Error()) result = util.Error{IsNormal: false, Err: err, Type: ERRTYPE_GETIMAGEINDEX} return result } if nums < 1 { util.LogEvent(this.Cat, ERRTYPE_IMAGENAMEINVALID, "IdNoFound", nil) result = util.Error{IsNormal: true, Err: errors.New("image id is't exists"), Type: ERRTYPE_IMAGENAMEINVALID} return result } for k, v := range res { this.StoragePath = k this.StorageType = fmt.Sprintf("%v", v) } return result }
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 }
func (this ImageRequest) Save(r *request.SaveRequest) (response.SaveResponse, util.Error) { r.TargetFormat = strings.ToLower(r.TargetFormat) r.Channel = strings.ToLower(r.Channel) util.LogEvent(this.Cat, "Save", r.Channel, nil) this.checkPlanID(r) if err := this.checkSaveRequest(r); err.Err != nil { return response.SaveResponse{}, err } if err := this.checkSaveCheckItem(r); err.Err != nil { return response.SaveResponse{}, err } storage, storageType := NewStorage(this.Cat) path, e := storage.Upload(r.FileBytes, r.TargetFormat) if e.Err != nil { return response.SaveResponse{}, e } tableZone := sharding() channel := models.Channel{Cat: this.Cat} imgIndex := models.ImageIndex{ChannelCode: channel.GetChannelCode(r.Channel), StoragePath: path, StorageType: storageType, TableZone: tableZone, Cat: this.Cat} plan := "" if r.Process.AnyTypes != nil && len(r.Process.AnyTypes) > 0 { bts, err := r.Process.MarshalJSON() if err != nil { util.LogErrorEvent(this.Cat, ERRORTYPE_MARSHALJSON, err.Error()) return response.SaveResponse{}, util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_MARSHALJSON} } plan = string(bts) } if e := imgIndex.SaveToDB(plan); e.Err != nil { return response.SaveResponse{}, e } uri := imgIndex.GetImageName() return response.SaveResponse{CheckPass: true, OriginalPath: uri, TargetPath: uri}, util.Error{} }
func (this ImageRequest) Delete(r *request.DeleteRequest) (response.DeleteResponse, util.Error) { storage, e := this.getStorage(r.FilePath) if e.Err != nil { return response.DeleteResponse{}, e } util.LogEvent(this.Cat, "Delete", this.GetChannel(r.FilePath), map[string]string{"uri": r.FilePath, "IsDeleteAll": strconv.FormatBool(r.IsDeleteAll)}) if isNewUri(r.FilePath) { imgIndex := models.ImageIndex{Cat: this.Cat} e = imgIndex.ParseName(r.FilePath) if e.Err != nil { return response.DeleteResponse{}, e } e = imgIndex.Delete() if e.Err != nil { return response.DeleteResponse{}, e } } e = storage.Delete(r.IsDeleteAll) if e.Err != nil { return response.DeleteResponse{}, e } return response.DeleteResponse{}, util.Error{} }
func (handler *ImageWS) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() w.Header().Set("Connection", "keep-alive") w.Header().Set("Content-Type", "text/xml") Cat := cat.Instance() tran := Cat.NewTransaction("URL", "ImageWs.asmx") util.LogEvent(Cat, "URL", "URL.Client", map[string]string{ "clientip": util.GetClientIP(r), "serverip": util.GetIP(), "proto": r.Proto, "referer": r.Referer(), //"agent": request.UserAgent(), }) var result util.Error defer func() { if p := recover(); p != nil { Cat.LogPanic(p) } if result.Err != nil && !result.IsNormal { tran.SetStatus(result.Err) } else { tran.SetStatus("0") } tran.Complete() }() bts, err := ioutil.ReadAll(r.Body) if err != nil { util.LogErrorEvent(Cat, "RequestReadError", err.Error()) result = util.Error{IsNormal: false, Err: err, Type: "RequestReadError"} msg := []byte(err.Error()) writeResponse(w, msg) return } req := request.Request{} result = EncodeRequest(Cat, bts, &req) if result.Err != nil && !result.IsNormal { writeResponse(w, []byte(result.Type)) return } var ( resp interface{} header *response.Header e util.Error imgRequest = business.ImageRequest{Cat: Cat} ) requestTran := Cat.NewTransaction("Request", strings.Replace(req.Header.RequestType, "Arch.Base.ImageWS.", "", -1)) defer func() { if p := recover(); p != nil { Cat.LogPanic(p) } if result.Err != nil && !result.IsNormal { requestTran.SetStatus(result.Err) } else { requestTran.SetStatus(CATTRANSUCCESS) } requestTran.Complete() }() switch req.Header.RequestType { case REQUESTTYPE_SAVEIMAGE: resp, e = imgRequest.Save(&req.SaveRequest) case REQUESTTYPE_LOADIMAGE: resp, e = imgRequest.Download(&req.LoadImgRequest) case REQUESTTYPE_LOADZIP: resp, e = imgRequest.DownloadZip(&req.LoadZipRequest) case REQUESTTYPE_DELETEIMAGE: resp, e = imgRequest.Delete(&req.DeleteRequest) default: util.LogErrorEvent(Cat, "RequestTypeInvalid", req.Header.RequestType) e = util.Error{IsNormal: true, Err: errors.New("requesttype is invalid!"), Type: "RequestTypeInvalid"} } if e.Err != nil { result = e header = createFailHeader(req.Header, fmt.Sprintf("%v", e.Err)) } else { header = transformHeader(req.Header, RESULTCODE_SUCCESS, "") } content, e := DecodeResponse(Cat, header, resp) if e.Err != nil { result = e writeResponse(w, []byte(result.Err.(error).Error())) } else { writeResponse(w, content) } }
func (handler *LogoWS) ServeHTTP(w http.ResponseWriter, r *http.Request) { Cat := cat.Instance() w.Header().Set("Connection", "keep-alive") w.Header().Set("Content-Type", "text/xml") tran := Cat.NewTransaction("URL", "LogoWs.asmx") util.LogEvent(Cat, "URL", "URL.Client", map[string]string{ "clientip": util.GetClientIP(r), "serverip": util.GetIP(), "proto": r.Proto, "referer": r.Referer(), //"agent": request.UserAgent(), }) var result util.Error defer func() { if p := recover(); p != nil { Cat.LogPanic(p) } if result.Err != nil && !result.IsNormal { tran.SetStatus(result.Err) } else { tran.SetStatus("0") } tran.Complete() }() bts, err := ioutil.ReadAll(r.Body) if err != nil { util.LogErrorEvent(Cat, "RequestReadError", err.Error()) result = util.Error{IsNormal: false, Err: err, Type: "RequestReadError"} return } req := request.Request{} result = EncodeRequest(Cat, bts, &req) if result.Err != nil && !result.IsNormal { //writeResponse(w, []byte(result.Type)) return } var ( resp interface{} header *response.Header e util.Error logoRequest = business.LogoRequest{ ImageRequest: business.ImageRequest{ Cat: Cat, }, FontSizes: []int{14, 16, 18, 20}, } ) requestTran := Cat.NewTransaction("Request", "SaveLogo") defer func() { if p := recover(); p != nil { Cat.LogPanic(p) } if result.Err != nil && !result.IsNormal { requestTran.SetStatus(result.Err) } else { requestTran.SetStatus("0") } requestTran.Complete() }() resp, result = logoRequest.Save(&req.SaveRequest) if result.Err != nil { header = createFailHeader(req.Header, fmt.Sprintf("%v", result.Err)) } else { header = transformHeader(req.Header, RESULTCODE_SUCCESS, "") } content, e := DecodeResponse(Cat, header, resp) if e.Err != nil { result = e writeResponse(w, []byte(result.Err.(error).Error())) } else { writeResponse(w, content) } //content, err := soapparse.DecResp(header, resp) // //if err != nil { // return //} //writeResponse(w, content) }