Esempio n. 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{}
}
Esempio n. 2
0
func (this *NfsStorage) Delete(isDeleteAll bool) util.Error {
	var (
		cmd *exec.Cmd
		err error
	)
	if this.Cat != nil {
		tran := this.Cat.NewTransaction(CATTITLE, "Nfs.Delete")
		tran.AddData("path", this.Path)
		defer func() {
			if err != nil {
				tran.SetStatus(err)
			} else {
				tran.SetStatus("0")
			}
			tran.Complete()
		}()
	}
	if !isDeleteAll {
		cmd = exec.Command("rm", this.Path)
	} else {
		cmd = exec.Command("rm", util.Substr(this.Path, 4, len(this.Path)-4)+"_*")
	}
	err = cmd.Run()
	if err != nil {
		util.LogErrorEvent(this.Cat, ERRORTYPE_NFSDELETEERR, err.Error())
		return util.Error{IsNormal: false, Err: err, Type: ERRORTYPE_NFSDELETEERR}
	}
	return util.Error{}
}
Esempio n. 3
0
func isNewUri(uri string) bool {
	index := strings.Index(uri, ".")
	if index < 1 {
		return false
	}
	s := util.Substr(uri, 1, index-1)
	return len(s) == NEWIMAGENAMELENGTH
}
Esempio n. 4
0
func (this ImageRequest) GetChannel(path string) string {
	path = strings.Replace(path, "/", "\\", -1)
	if isNewUri(path) {
		channelCode := util.Substr(path, 1, 2)
		channel := models.Channel{Cat: this.Cat}
		channels, _ := channel.GetAll()
		for k, v := range channels {
			if v == channelCode {
				return k
			}
		}
		return ""
	}
	if isFdfs(path) {
		s := util.Substr(path, 4, len(path)-4)
		i := strings.Index(s, "\\")
		return util.Substr(s, 0, i)
	}
	if isT1(path) {
		s := util.Substr(path, 4, len(path)-4)
		i := strings.Index(s, "\\")
		return util.Substr(s, 0, i)
	}
	s := util.Substr(path, 1, len(path)-4)
	i := strings.Index(s, "\\")
	return util.Substr(s, 0, i)
}
Esempio n. 5
0
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{}
}
Esempio n. 6
0
func isT1(uri string) bool {
	return util.Substr(uri, 0, 4) == "\\t1\\"
}
Esempio n. 7
0
func isFdfs(uri string) bool {
	return util.Substr(uri, 0, 4) == "\\fd\\"
}
Esempio n. 8
0
func (this NfsStorage) isT1() bool {
	return util.Substr(this.Path, 0, 4) == "\\t1\\"
}