<commit_before>package models

import (
	"encoding/json"
	"fmt"
	"reflect"
	"time"

	"github.com/dockercn/docker-bucket/utils"
)

type Image struct {
	ImageId    string //
	JSON       string //
	Ancestry   string //
	Checksum   string //
	Payload    string //
	URL        string //
	Backend    string //
	Path       string //文件在服务器的存储路径
	Sign       string //
	Size       int64  //
	Uploaded   bool   //
	Checksumed bool   //
	Encrypted  bool   //是否加密
	Created    int64  //
	Updated    int64  //
}

func (image *Image) Get(imageId, sign string) (bool, []byte, error) {
	var k []byte

	if len(sign) == 0 {
		k = []byte(fmt.Sprintf("%s+", GetObjectKey("image", imageId)))
	} else {
		k = []byte(fmt.Sprintf("%s-?%s", GetObjectKey("image", imageId), sign))
	}

	if key, err := LedisDB.HGet([]byte(GetServerKeys("image")), k); err != nil {
		return false, []byte(""), err
	} else if key != nil {
		if exist, err := LedisDB.Exists(key); err != nil || exist == 0 {
			return false, []byte(""), err
		} else {
			return true, key, nil
		}
	}

	return false, []byte(""), nil
}

func (image *Image) GetPushed(imageId, sign string, uploaded, checksumed bool) (bool, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return false, err
	} else if has == false {
		return false, fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded")); e != nil {
			return false, e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return false, nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return false, nil
			}

			return true, nil
		}
	}

	return false, nil
}

func (image *Image) GetJSON(imageId, sign string, uploaded, checksumed bool) ([]byte, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return []byte(""), err
	} else if has == false {
		return []byte(""), fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded"), []byte("JSON")); e != nil {
			return []byte(""), e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return []byte(""), nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return []byte(""), nil
			}

			return results[2], nil
		}
	}

	return []byte(""), nil

}

func (image *Image) GetChecksum(imageId, sign string, uploaded, checksumed bool) (string, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return "", err
	} else if has == false {
		return "", fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded"), []byte("Checksum")); e != nil {
			return "", e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return "", nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return "", nil
			}

			return string(results[2]), nil
		}
	}

	return "", nil

}

func (image *Image) PutJSON(imageId, sign, json string) error {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return err
	} else if has == false {
		//新建 Image 记录
		key = utils.GeneralKey(fmt.Sprintf("%s+%s", GetObjectKey("image", imageId), sign))

		image.ImageId = imageId
		image.JSON = json

		image.Uploaded = false
		image.Checksumed = false
		image.Encrypted = false

		image.Size = 0

		image.Updated = time.Now().Unix()
		image.Created = time.Now().Unix()

		if len(sign) > 0 {
			image.Sign = sign
			image.Encrypted = true
		}

		if e := image.Save(key); e != nil {
			return e
		} else {
			if len(sign) == 0 {
				if e := LedisDB.Set([]byte(fmt.Sprintf("%s+", GetObjectKey("image", imageId))), key); e != nil {
					return e
				}
			} else {
				if e := LedisDB.Set([]byte(fmt.Sprintf("%s-?", GetObjectKey("image", imageId), sign)), key); e != nil {
					return e
				}
			}
		}
	} else {
		//更新旧 Image 记录

		image.ImageId = imageId
		image.JSON = json

		image.Updated = time.Now().Unix()

		if len(sign) > 0 {
			image.Sign = sign<commit_msg>修改 Image 的全局 Key 存储方案<commit_after>package models

import (
	"encoding/json"
	"fmt"
	"reflect"
	"time"

	"github.com/dockercn/docker-bucket/utils"
)

type Image struct {
	ImageId    string //
	JSON       string //
	Ancestry   string //
	Checksum   string //
	Payload    string //
	URL        string //
	Backend    string //
	Path       string //文件在服务器的存储路径
	Sign       string //
	Size       int64  //
	Uploaded   bool   //
	Checksumed bool   //
	Encrypted  bool   //是否加密
	Created    int64  //
	Updated    int64  //
}

func (image *Image) Get(imageId, sign string) (bool, []byte, error) {
	var k []byte

	if len(sign) == 0 {
		k = []byte(fmt.Sprintf("%s+", GetObjectKey("image", imageId)))
	} else {
		k = []byte(fmt.Sprintf("%s-?%s", GetObjectKey("image", imageId), sign))
	}

	if key, err := LedisDB.HGet([]byte(GetServerKeys("image")), k); err != nil {
		return false, []byte(""), err
	} else if key != nil {
		if image, err := LedisDB.HGet(key, []byte("ImageId")); err != nil {
			return false, []byte(""), err
		} else if image != nil {
			if string(image) != imageId {
				return true, key, fmt.Errorf("存在 Image 数据，但是 ImageID 不相同: %s", string(image))
			}
			return true, key, nil
		}
	}

	return false, []byte(""), nil
}

func (image *Image) GetPushed(imageId, sign string, uploaded, checksumed bool) (bool, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return false, err
	} else if has == false {
		return false, fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded")); e != nil {
			return false, e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return false, nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return false, nil
			}

			return true, nil
		}
	}

	return false, nil
}

func (image *Image) GetJSON(imageId, sign string, uploaded, checksumed bool) ([]byte, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return []byte(""), err
	} else if has == false {
		return []byte(""), fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded"), []byte("JSON")); e != nil {
			return []byte(""), e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return []byte(""), nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return []byte(""), nil
			}

			return results[2], nil
		}
	}

	return []byte(""), nil

}

func (image *Image) GetChecksum(imageId, sign string, uploaded, checksumed bool) (string, error) {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return "", err
	} else if has == false {
		return "", fmt.Errorf("没有在数据库中查询到要更新的 Image 数据")
	} else {
		if results, e := LedisDB.HMget(key, []byte("Checksumed"), []byte("Uploaded"), []byte("Checksum")); e != nil {
			return "", e
		} else {

			if utils.BytesToBool(results[0]) != checksumed {
				return "", nil
			}

			if utils.BytesToBool(results[1]) != uploaded {
				return "", nil
			}

			return string(results[2]), nil
		}
	}

	return "", nil

}

func (image *Image) PutJSON(imageId, sign, json string) error {
	if has, key, err := image.Get(imageId, sign); err != nil {
		return err
	} else if has == false {
		//新建 Image 记录
		key = utils.GeneralKey(fmt.Sprintf("%s+%s", GetObjectKey("image", imageId), sign))

		image.ImageId = imageId
		image.JSON = json

		image.Uploaded = false
		image.Checksumed = false
		image.Encrypted = false

		image.Size = 0

		image.Updated = time.Now().Unix()
		image.Created = time.Now().Unix()

		if len(sign) > 0 {
			image.Sign = sign
			image.Encrypted = true
		}

		if e := image.Save(key); e != nil {
			return e
		} else {
			if len(sign) == 0 {
				if _, e := LedisDB.HSet([]byte(GetServerKeys("image")), []byte(fmt.Sprintf("%s+", GetObjectKey("image", imageId))), key); e != nil {
					return e
				}
			} else {
				if _, e := LedisDB.HSet([]byte(GetServerKeys("image")), []byte(fmt.Sprintf("%s-?", GetObjectKey("image", imageId), sign)), key); e != nil {
					return e
				}
			}
		}
	} else {
		//更新旧 Image 记录

		image.ImageId = imageId
		image.JSON = json

		image.Updated = time.Now().Unix()

		if len(sign) > 0 {
			image.Sign = sign
{'fraction_non_alphanumeric': 0.17596305747964516, 'fraction_numerical': 0.004739336492890996, 'mean_word_length': 3.05618531296205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3255002', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3042, 'n_words': 794}