Example #1
0
func (this ImageRequest) getStorageBySource(path string, isSource bool) (Storage, util.Error) {
	path = strings.Replace(path, "/", "\\", -1)
	var (
		storagePath string
		storageType string
	)
	var storage Storage
	if isNewUri(path) {
		imagename := util.Substr(path, 1, NEWIMAGENAMELENGTH)
		imgIndex := models.ImageIndex{Cat: this.Cat}
		if e := imgIndex.Parse(imagename); e.Err != nil {
			return nil, e
		}
		storagePath = imgIndex.StoragePath

		storageType = imgIndex.StorageType
		storage = CreateStorage(storagePath, storageType, this.Cat)
		if storage == nil {
			util.LogErrorEvent(this.Cat, ERRORTYPE_STORAGETYPENOSUPPORTE, util.JoinString("Can't supporte storagetype[", storageType, "]"))
			return nil, util.Error{IsNormal: false, Err: errors.New(util.JoinString("Can't supporte storagetype[", storageType, "]")), Type: ERRORTYPE_STORAGETYPENOSUPPORTE}
		}
	} else {
		storageType = STORAGETYPE_NFS
		if isFdfs(path) {
			storageType = STORAGETYPE_FDFS
		}
		storage = CreateStorage(path, storageType, this.Cat)
		if e := storage.ConvertFilePath(isSource); e.Err != nil {
			return nil, e
		}
	}
	return storage, util.Error{}
}
Example #2
0
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}
}
Example #3
0
func getShortImagePath(imagePath string) string {
	segments := strings.Split(imagePath, "/")
	if len(segments) < 4 {
		return imagePath
	}
	if segments[2] == "fd" || segments[2] == "t1" {
		return util.JoinString("/", segments[1], "/", segments[2], "/", segments[3])
	} else {
		return util.JoinString("/", segments[1], "/", segments[2])
	}
}
Example #4
0
func (this *Config) GetGroups() ([]string, util.Error) {
	configs, e := this.GetChannelConfigs(CONFIG_DEFAULTCHANNEL)
	if e.Err != nil {
		return []string{}, e
	}
	groups, exists := configs[CONFIG_FDFSGROUPS]
	if !exists {
		return nil, util.Error{IsNormal: false, Err: errors.New(util.JoinString("channel[", CONFIG_DEFAULTCHANNEL, "] Config[", CONFIG_FDFSGROUPS, "] is't exists!")), Type: ERRORTYPE_CONFIGNOEXISTS}
	}

	if len(groups) < 1 {
		return nil, util.Error{IsNormal: false, Err: errors.New(util.JoinString("channel[", CONFIG_DEFAULTCHANNEL, "] Config[", CONFIG_FDFSGROUPS, "] is't exists!")), Type: ERRORTYPE_CONFIGNOEXISTS}
	}
	return strings.Split(groups, SPILT_1), util.Error{}
}
Example #5
0
func (this *Config) getValue(channel, key string) (string, util.Error) {
	configs, e := this.GetConfigs()
	if e.Err != nil {
		return "", e
	}

	var (
		value  string
		exists bool
	)
	channelConfigs, exists := configs[channel]
	if exists {
		value, exists = channelConfigs[key]
		if exists {
			return value, util.Error{}
		}
	}
	if channel != CONFIG_DEFAULTCHANNEL {
		defaultConfigs, exists := configs[CONFIG_DEFAULTCHANNEL]
		if exists {
			value, exists = defaultConfigs[key]
			if exists {
				return value, util.Error{}
			}
		}
	}
	return "", util.Error{IsNormal: false, Err: errors.New(util.JoinString("Channel[", channel, "] Key[", key, "] is't exists!")), Type: ERRORTYPE_CONFIGNOEXISTS}
}
Example #6
0
func (this ImageIndex) GetImageName() string {
	ext := getExt(this.StoragePath)                              //扩展名
	zone := strconv.FormatInt(int64(this.TableZone), 36)         //转36进制
	partition := strconv.FormatInt(int64(this.PartitionKey), 36) //转36进制
	//1~2 频道 3~4 分区 5~6 时间 7 版本号 8~17 索引 18~21 检验码
	tmp := util.JoinString(this.ChannelCode, util.Cover(zone, "0", 2), util.Cover(partition, "0", 2), DEFAULTVERSION, util.Cover(strconv.FormatInt(this.Id, 36), "0", 10))
	return util.JoinString("\\", tmp, util.Compute(tmp), ".", ext)
}
Example #7
0
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)
}
Example #8
0
func (this *Config) GetFdfsPort() (string, util.Error) {
	configs, e := this.GetChannelConfigs(CONFIG_DEFAULTCHANNEL)
	if e.Err != nil {
		return "", e
	}
	port, exists := configs[CONFIG_FDFSPORT]
	if !exists {
		return "", util.Error{IsNormal: false, Err: errors.New(util.JoinString("channel[", CONFIG_DEFAULTCHANNEL, "] Config[", CONFIG_FDFSPORT, "] is't exists!")), Type: ERRORTYPE_CONFIGNOEXISTS}
	}
	return port, util.Error{}
}
Example #9
0
func (this *Config) GetChannelConfigs(channelCode string) (map[string]string, util.Error) {
	configs, e := this.GetConfigs()
	if e.Err != nil {
		return nil, e
	}
	config, exists := configs[channelCode]
	if !exists {
		return nil, util.Error{IsNormal: false, Err: errors.New(util.JoinString("channelCode[", channelCode, "] Config is't exists!")), Type: ERRORTYPE_CONFIGNOEXISTS}
	}
	return config, util.Error{}
}
Example #10
0
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{}
}
Example #11
0
func (m *Master) checkWorkerHealth(w *Worker) {
	val, err := doHttpCall(util.JoinString(
		"http://127.0.0.1:", strconv.Itoa(w.host.GetPort()), "/heartbeat/"))
	if err != nil {
		w.sick()
		log.WithFields(log.Fields{
			"port": w.host.GetPort(),
			"type": "WorkerProcess.HeartbeatError",
		}).Error(err.Error())
		util.LogErrorEvent(cat.Instance(), "WorkerProcess.HeartbeatError", err.Error())
	} else {
		w.healthy()
		url, err := url.Parse(string(val))
		if err != nil {
			log.Errorf("parse heartbeat from worker=%d failed.", w.host.GetPort())
		} else {
			w.heartbeatInfo = heartbeatValues{mapValues: nil, urlValues: url.Query()}
		}
	}
}
Example #12
0
func killPort(port int) error {
	cmd := exec.Command("sh", "-c", util.JoinString("lsof -i:", strconv.Itoa(port), "|grep LISTEN|awk '{print $2}'"))
	bts, err := cmd.Output()
	if err != nil {
		return err
	}
	pid := strings.TrimSpace(string(bts))
	if pid == "" {
		return nil
	}
	id, err := strconv.Atoi(pid)
	if err != nil {
		return err
	}
	p, err := os.FindProcess(id)
	if err != nil {
		return err
	}
	return p.Kill()
}
Example #13
0
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
}
Example #14
0
func (this *ImageIndex) SaveToDB(plan string) util.Error {
	var (
		res sql.Result
		err error
		id  int64
	)
	if this.Cat != nil {
		tran := this.Cat.NewTransaction(DBTITLE, util.JoinString("ImageIndex_"+strconv.Itoa(this.TableZone)+".Insert"))
		defer func() {
			if err != nil {
				tran.SetStatus(err)
			} else {
				tran.SetStatus("0")
			}
			tran.Complete()
		}()
	}

	o := orm.NewOrm()
	o.Using(getDBString(this.TableZone))
	o.Begin()
	partitionKey := util.GetPartitionKey(time.Now())
	res, err = o.Raw("INSERT INTO `imageindex_"+strconv.Itoa(this.TableZone)+"` (`channelCode`,`storagePath`,`storageType`,`profile`,`createtime`,`partitionKey`)VALUES(?,?,?,?,NOW(),?)", this.ChannelCode, this.StoragePath, this.StorageType, this.Profile, partitionKey).Exec()

	if err != nil {
		o.Rollback()
		util.LogErrorEvent(this.Cat, ERRORTYPE_INSERTIMAGEINDEX, err.Error())
		return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_INSERTIMAGEINDEX}
	}
	id, err = res.LastInsertId()
	if err != nil {
		o.Rollback()
		util.LogErrorEvent(this.Cat, ERRORTYPE_INSERTIMAGEINDEX, err.Error())
		return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_INSERTIMAGEINDEX}
	}
	if plan != "" {
		if this.Cat != nil {
			tran := this.Cat.NewTransaction(DBTITLE, util.JoinString("ImagePlan_"+strconv.Itoa(this.TableZone)+".Insert"))
			defer func() {
				if err != nil {
					tran.SetStatus(err)
				} else {
					tran.SetStatus("0")
				}
				tran.Complete()
			}()
		}
		res, err = o.Raw("INSERT INTO `imageplan_"+strconv.Itoa(this.TableZone)+"`(imgId,plan,partitionKey)VALUES(?,?,?)", id, plan, partitionKey).Exec()
		if err != nil {
			o.Rollback()
			util.LogErrorEvent(this.Cat, ERRORTYPE_INSERTIMAGEPLAN, err.Error())
			return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_INSERTIMAGEPLAN}
		}
		if _, err = res.RowsAffected(); err != nil {
			o.Rollback()
			util.LogErrorEvent(this.Cat, ERRORTYPE_INSERTIMAGEPLAN, err.Error())
			return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_INSERTIMAGEPLAN}
		}
	}
	if err = o.Commit(); err != nil {
		return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_INSERTIMAGEINDEX}
	}
	this.Id = id
	this.PartitionKey = partitionKey
	return util.Error{}
}
Example #15
0
func combineHeartbeat(h cat.Heartbeat, port string, val heartbeatValues) {
	h.Set("System", util.JoinString("Alloc_", port), val.Get("Alloc"))
	h.Set("System", util.JoinString("TotalAlloc_", port), val.Get("TotalAlloc"))
	h.Set("System", util.JoinString("Sys_", port), val.Get("Sys"))
	h.Set("System", util.JoinString("Mallocs_", port), val.Get("Mallocs"))
	h.Set("System", util.JoinString("Frees_", port), val.Get("Frees"))
	h.Set("System", util.JoinString("OtherSys_", port), val.Get("OtherSys"))
	h.Set("System", util.JoinString("PauseNs_", port), val.Get("PauseNs"))
	h.Set("HeapUsage", util.JoinString("HeapAlloc_", port), val.Get("HeapAlloc"))
	h.Set("HeapUsage", util.JoinString("HeapSys_", port), val.Get("HeapSys"))
	h.Set("HeapUsage", util.JoinString("HeapIdle_", port), val.Get("HeapIdle"))
	h.Set("HeapUsage", util.JoinString("HeapInuse_", port), val.Get("HeapInuse"))
	h.Set("HeapUsage", util.JoinString("HeapReleased_", port), val.Get("HeapReleased"))
	h.Set("HeapUsage", util.JoinString("HeapObjects_", port), val.Get("HeapObjects"))
	h.Set("GC", util.JoinString("NextGC_", port), val.Get("NextGC"))
	h.Set("GC", util.JoinString("LastGC_", port), val.Get("LastGC"))
	h.Set("GC", util.JoinString("NumGC_", port), val.Get("NumGC"))
}