Пример #1
0
func (this *ProcChainBuilder) DigimarkProcChain(params map[string]string) (*proc.ProcessorChain, *buildError) {
	procChain := &proc.ProcessorChain{Chain: make([]proc.ImageProcessor, 0, 10)}
	_, channel, _ := ParseUri(params[":1"])
	stripProcessor, e := this.getStripProcessor(channel, params)
	if e != nil {
		return nil, &buildError{e, "UrlStripCmdError"}
	}
	procChain.Chain = append(procChain.Chain, stripProcessor)
	log.Debug("add strip processor")
	dwmProcessor, e := this.getDigitalWatermarkProcessor(channel, params)
	if e != nil {
		return nil, &buildError{e, "UrlDigitalWatermarkCmdError"}
	}
	if dwmProcessor != nil {
		procChain.Chain = append(procChain.Chain, dwmProcessor)
		log.Debug("add digital watermark processor")
	}
	formatProcessor, e := this.getFormatProcessor(channel, params)
	if e != nil {
		return nil, &buildError{e, "UrlFormatCmdError"}
	}
	if formatProcessor != nil {
		procChain.Chain = append(procChain.Chain, formatProcessor)
		log.Debug("add format processor")
	}

	return procChain, nil
}
Пример #2
0
func (this *WaterMarkProcessor) Process(img *img4g.Image) error {
	log.Debug("process watermark")
	var err error = nil
	tran := this.Cat.NewTransaction("Command", this.WaterMarkType)

	defer func() {
		this.Logo.DestoryWand()
		tran.SetStatus(err)
		tran.Complete()
	}()
	this.Logo.CreateWand()
	if this.Location == 0 {
		this.Location = 9
	}
	if this.Location < 1 || this.Location > 9 {
		err = errors.New("Logo location(" + string(this.Location) + ") isn't right!")
		return err
	}
	var x, y int64
	x, y, err = getLocation(this.Location, img, this.Logo)
	if err != nil {
		return err
	}

	if this.Dissolve > 0 && this.Dissolve < 100 {
		this.Logo.Dissolve(this.Dissolve)
	}

	err = img.Composite(this.Logo, x, y)
	return err
}
Пример #3
0
func (this *ResizeRProcessor) Process(img *img4g.Image) error {
	log.Debug("process resize r")
	var err error
	tran := this.Cat.NewTransaction("Command", "ResizeR")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	var width, height int64
	width, height, err = img.Size()
	if err != nil {
		return err
	}
	if width <= this.Width && height <= this.Height {
		return nil
	}
	var (
		x int64 = 0
		y int64 = 0
		w int64 = width
		h int64 = height
	)
	if width > this.Width && height > this.Height {
		p1 := float64(this.Width) / float64(this.Height)
		p2 := float64(width) / float64(height)
		w = 0
		h = 0
		if math.Abs(p1-p2) > 0.0001 {
			if p2 > p1 { //以高缩小
				h = height
				w = int64(math.Floor(float64(h) * p1))
				x = (width - w) / 2
			}
			if p2 < p1 { //以宽缩小
				w = width
				h = int64(math.Floor(float64(w) / p1))
				y = (height - h) / 2
			}
			err = img.Crop(w, h, x, y)
			if err != nil {
				return err
			}
		}
		err = img.Resize(this.Width, this.Height)
		return err
	} else {
		if width > this.Width {
			x = (w - this.Width) / 2
			w = this.Width
		}
		if height > this.Height {
			y = (h - this.Height) / 2
			h = this.Height
		}
		err = img.Crop(w, h, x, y)
		return err
	}

}
Пример #4
0
func (p *ScaleProcessor) Process(img *img4g.Image) error {
	log.Debug("process scale")
	var err error
	tran := cat.Instance().NewTransaction("Command", "Scale")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	err = img.Resize(p.Width, p.Height)
	return err
}
Пример #5
0
func (this *RotateProcessor) Process(img *img4g.Image) error {
	log.Debug("process rotate ")
	var err error
	tran := this.Cat.NewTransaction("Command", "Rotate")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	err = img.Rotate(this.Degress)
	return err
}
func (this *DigitalWatermarkProcessor) Process(img *img4g.Image) error {
	log.Debug("process digitalwatermark")
	var err error = nil

	format, err := img.GetFormat()
	if err != nil {
		return err
	}
	if strings.ToLower(format) != "jpg" && strings.ToLower(format) != "jpeg" {
		info := make(map[string]string)
		info["format"] = format
		logEvent(this.Cat, "DigitalWatermarkRefuse", "NotSupportFormat", info)
		return nil
	}

	width, err := img.GetWidth()
	if err != nil {
		return err
	}
	if width < 256 {
		info := make(map[string]string)
		info["width"] = strconv.Itoa(int(width))
		logEvent(this.Cat, "DigitalWatermarkRefuse", "NotSupportSize", info)
		return nil
	}

	height, err := img.GetHeight()
	if err != nil {
		return err
	}
	if height < 256 {
		info := make(map[string]string)
		info["height"] = strconv.Itoa(int(height))
		logEvent(this.Cat, "DigitalWatermarkRefuse", "NotSupportSize", info)
		return nil
	}

	upr := ((int(math.Min(float64(width), float64(height))) / 100.0) + 1) * 100
	tran := this.Cat.NewTransaction("DigitalWatermark", "Min(width, height)<"+strconv.Itoa(int(upr)))
	tran.AddData("size", "width: "+strconv.Itoa(int(width))+"height: "+strconv.Itoa(int(height)))
	defer func() {
		this.Copyright.DestoryWand()
		tran.SetStatus(err)
		tran.Complete()
	}()
	if err = this.Copyright.CreateWand(); err != nil {
		return err
	}
	err = img.DigitalWatermark(this.Copyright)

	return err
}
Пример #7
0
func (this *ProcChainBuilder) getWaterMarkProcessors(sourceType string, channel string, path string, params map[string]string) ([]proc.ImageProcessor, error) {
	processors := make([]proc.ImageProcessor, 2)
	//processors
	logoprocessor, err := this.getLogoWaterMarkProcessor(channel, params)
	if err != nil {
		return nil, err
	}
	if logoprocessor != nil {
		processors = append(processors, logoprocessor)
		log.Debug("add logo watermark processor")
	}
	nameprocessor, err := this.getNameWaterMarkProcessor(sourceType, channel, path, params)
	if err != nil {
		return nil, err
	}
	if nameprocessor != nil {
		processors = append(processors, nameprocessor)
		log.Debug("add name watermark processor")
	}

	return processors, nil
}
Пример #8
0
func (this *HostProcessor) monitorWorkerProcesses() {
	defer func() {
		if p := recover(); p != nil {
			log.WithFields(log.Fields{
				"type": "DaemonProcess.MonitorPanic",
			}).Error(fmt.Sprintf("%v", p))
			LogErrorEvent(CatInstance, "DaemonProcess.MonitorPanic", fmt.Sprintf("%v", p))
			this.monitorWorkerProcesses()
		}
	}()
	time.Sleep(8 * time.Second) //sleep ,wait sub process run
	for {
		time.Sleep(2 * time.Second)
		log.Debug("monitor......")
		for port, countor := range ports {
			if port == "" {
				continue
			}

			if countor > 2 {
				log.WithFields(log.Fields{
					"port": port,
				}).Debug("restart port")
				err := KillProcessByPort(port)
				if err != nil {
					log.WithFields(log.Fields{
						"port": port,
						"type": "DaemonProcess.KillProcessError",
					}).Error(err.Error())
					LogErrorEvent(CatInstance, "DaemonProcess.KillProcessError", err.Error())
				}
				this.startWorkerProcess(port)
				ports[port] = 0
			} else {
				_, err := GetHttp(JoinString("http://127.0.0.1:", port, "/heartbeat/"))
				if err != nil {
					ports[port] = ports[port] + 1
					log.WithFields(log.Fields{
						"port": port,
						"type": "WorkerProcess.HeartbeatError",
					}).Error(err.Error())
					LogErrorEvent(CatInstance, "WorkerProcess.HeartbeatError", err.Error())
				} else {
					ports[port] = 0
				}
			}
		}
	}
}
Пример #9
0
//高固定,宽(原图比例计算),宽固定,高(原图比例计算) (压缩)
func (this *ResizeWProcessor) Process(img *img4g.Image) error {
	log.Debug("process resize w")
	var err error
	tran := this.Cat.NewTransaction("Command", "ResizeW")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	var width, height int64
	width, height, err = img.Size()
	if err != nil {
		return err
	}

	if (width <= this.Width && height <= this.Height && this.Width != 0 && this.Height != 0) || (this.Width == 0 && height <= this.Height) || (this.Height == 0 && width <= this.Width) {
		return nil
	}

	w, h := this.Width, this.Height
	if w == 0 {
		w = width * h / height
		err = img.Resize(w, h)
		return err
	}
	if h == 0 {
		h = height * w / width
		err = img.Resize(w, h)
		return err
	}

	p1 := float64(this.Width) / float64(this.Height)
	p2 := float64(width) / float64(height)

	if p2 > p1 {
		h = int64(math.Floor(float64(this.Width) / p2))
		if int64(math.Abs(float64(h-this.Height))) < 3 {
			h = this.Height
		}
	} else {
		w = int64(math.Floor(float64(this.Height) * p2))
		if int64(math.Abs(float64(w-this.Width))) < 3 {
			w = this.Width
		}
	}
	err = img.Resize(w, h)
	return err
}
Пример #10
0
func (this *ResizeCProcessor) Process(img *img4g.Image) error {
	log.Debug("process resize c")
	var err error
	tran := this.Cat.NewTransaction("Command", "ResizeC")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()

	width, height, err1 := img.Size()
	if err1 != nil {
		err = err1
		return err1
	}

	p1 := float64(this.Width) / float64(this.Height)
	p2 := float64(width) / float64(height)
	var (
		x int64 = 0
		y int64 = 0
		w int64 = 0
		h int64 = 0
	)
	if math.Abs(p1-p2) > 0.0001 {
		if p2 > p1 { //以高缩小
			h = height
			w = int64(math.Floor(float64(h) * p1))
			x = (width - w) / 2
		}
		if p2 < p1 { //以宽缩小
			w = width
			h = int64(math.Floor(float64(w) / p1))
			y = (height - h) / 2
		}
		err = img.Crop(w, h, x, y)
		if err != nil {
			return err
		}
	}
	err = img.Resize(this.Width, this.Height)
	return err
}
Пример #11
0
func (this *ProcChainBuilder) Build(params map[string]string) (*proc.ProcessorChain, *buildError) {
	procChain := &proc.ProcessorChain{Chain: make([]proc.ImageProcessor, 0, 10)}

	sourceType, channel, path := ParseUri(params[":1"])
	//_, channel, _ := ParseUri(params[":1"])
	sequences, e := data.GetSequenceofoperation(channel)
	if e != nil {
		return nil, &buildError{e, "UrlSequenceCmdError"}
	}
	for _, t := range sequences {
		switch t {
		case CmdStrip:
			stripProcessor, e := this.getStripProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlStripCmdError"}
			}
			procChain.Chain = append(procChain.Chain, stripProcessor)
			log.Debug("add strip processor")
		case CmdResize:
			resizeProcessor, e := this.getResizeProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlResizeCmdError"}
			}
			procChain.Chain = append(procChain.Chain, resizeProcessor)
			log.Debug("add resize processor")
		case CmdQuality:
			qualityProcessor, e := this.getQualityProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlQualityCmdError"}
			}
			if qualityProcessor != nil {
				procChain.Chain = append(procChain.Chain, qualityProcessor)
				log.Debug("add quality processor")
			}
		case CmdRotate:
			rotateProcessor, e := this.getRotateProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlRotateCmdError"}
			}
			if rotateProcessor != nil {
				procChain.Chain = append(procChain.Chain, rotateProcessor)
				log.Debug("add rotate processor")
			}
		case CmdWaterMark:
			waterMarkProcessors, e := this.getWaterMarkProcessors(sourceType, channel, path, params)
			if e != nil {
				return nil, &buildError{e, "UrlWaterMarkCmdError"}
			}
			if waterMarkProcessors != nil {
				for _, p := range waterMarkProcessors {
					if p != nil {
						procChain.Chain = append(procChain.Chain, p)
					}
				}
			}
		case CmdFormat:
			formatProcessor, e := this.getFormatProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlFormatCmdError"}
			}
			if formatProcessor != nil {
				procChain.Chain = append(procChain.Chain, formatProcessor)
				log.Debug("add format processor")
			}
		case CmdDigitalWatermark:
			dwmProcessor, e := this.getDigitalWatermarkProcessor(channel, params)
			if e != nil {
				return nil, &buildError{e, "UrlDigitalWatermarkCmdError"}
			}
			if dwmProcessor != nil {
				procChain.Chain = append(procChain.Chain, dwmProcessor)
				log.Debug("add digital watermark processor")
			}
		}
	}
	return procChain, nil
}
Пример #12
0
func (this *StripProcessor) Process(img *img4g.Image) error {
	log.Debug("process strip")
	err := img.Strip()
	return err
}
Пример #13
0
func (this *HostProcessor) sendCatHeartBeat() {
	defer func() {
		if err := recover(); err != nil {
			log.Error(fmt.Sprintf("%v", err))
			this.sendCatHeartBeat()
		}
	}()
	ip := GetIP()
	second := time.Now().Second()
	if second < 29 {
		sleep := time.Duration((29 - second) * 1000000000)
		time.Sleep(sleep)
	}

	catinstance := cat.Instance()
	for {
		log.Debug("send cat heartbeat")
		stats1 := GetStatus()
		data := url.Values{}
		data.Add("port", hostPort)
		for k, v := range stats1 {
			data.Add(k, v)
		}
		portstats[hostPort] = data

		tran := catinstance.NewTransaction("System", "Status")
		h := catinstance.NewHeartbeat("HeartBeat", ip)
		for _, heart := range portstats {
			if heart == nil {
				continue
			}
			port := heart.Get("port")
			if port == "" {
				continue
			}
			h.Set("System", JoinString("Alloc_", port), heart.Get("Alloc"))
			h.Set("System", JoinString("TotalAlloc_", port), heart.Get("TotalAlloc"))
			h.Set("System", JoinString("Sys_", port), heart.Get("Sys"))
			h.Set("System", JoinString("Mallocs_", port), heart.Get("Mallocs"))
			h.Set("System", JoinString("Frees_", port), heart.Get("Frees"))
			h.Set("System", JoinString("OtherSys_", port), heart.Get("OtherSys"))
			h.Set("System", JoinString("PauseNs_", port), heart.Get("PauseNs"))

			h.Set("HeapUsage", JoinString("HeapAlloc_", port), heart.Get("HeapAlloc"))
			h.Set("HeapUsage", JoinString("HeapSys_", port), heart.Get("HeapSys"))
			h.Set("HeapUsage", JoinString("HeapIdle_", port), heart.Get("HeapIdle"))
			h.Set("HeapUsage", JoinString("HeapInuse_", port), heart.Get("HeapInuse"))
			h.Set("HeapUsage", JoinString("HeapReleased_", port), heart.Get("HeapReleased"))
			h.Set("HeapUsage", JoinString("HeapObjects_", port), heart.Get("HeapObjects"))

			h.Set("GC", JoinString("NextGC_", port), heart.Get("NextGC"))
			h.Set("GC", JoinString("LastGC_", port), heart.Get("LastGC"))
			h.Set("GC", JoinString("NumGC_", port), heart.Get("NumGC"))
			portstats[port] = nil
		}
		h.SetStatus("0")
		h.Complete()
		tran.SetStatus("0")
		tran.Complete()
		second = time.Now().Second()
		sleep := time.Duration((90 - second) * 1000000000)
		time.Sleep(sleep)
	}
}