regular update

master 2.0.0
兔子 5 years ago
parent d9e5206ba9
commit 7df7c90d14

@ -8,6 +8,8 @@ import (
"os" "os"
) )
// Unzip 读取位于src的zip文件并解压到dst文件夹中
// shell传入当前解压的文件名称
func Unzip(src, dst string, shell func(string)) error { func Unzip(src, dst string, shell func(string)) error {
if !IsFile(src) { if !IsFile(src) {
return errors.New(src + " Not Exists") return errors.New(src + " Not Exists")

@ -29,6 +29,7 @@ import (
"time" "time"
) )
// StarCrypto 存储了单个校验方法的非编码元数据
type StarCrypto struct { type StarCrypto struct {
Sha1 []byte Sha1 []byte
Sha224 []byte Sha224 []byte
@ -39,74 +40,58 @@ type StarCrypto struct {
Md5 []byte Md5 []byte
} }
/* // Base64Encode 输出格式化后的Base64字符串
Base64 func (starcpto StarCrypto) Base64Encode(bstr []byte) string {
*/
func (this StarCrypto) Base64Encode(bstr []byte) string {
return base64.StdEncoding.EncodeToString(bstr) return base64.StdEncoding.EncodeToString(bstr)
} }
/* // Base64Decode 输出解密前的Base64数据
Base64 func (starcpto StarCrypto) Base64Decode(str string) ([]byte, error) {
*/
func (this StarCrypto) Base64Decode(str string) ([]byte, error) {
return base64.StdEncoding.DecodeString(str) return base64.StdEncoding.DecodeString(str)
} }
/* // MD5 输出MD5校验值
MD5 func (starcpto *StarCrypto) MD5(bstr []byte) string {
*/
func (this *StarCrypto) MD5(bstr []byte) string {
md5sum := md5.New() md5sum := md5.New()
md5sum.Write(bstr) md5sum.Write(bstr)
this.Md5 = md5sum.Sum(nil) starcpto.Md5 = md5sum.Sum(nil)
return hex.EncodeToString(this.Md5) return hex.EncodeToString(starcpto.Md5)
} }
/* // CRC32 输出CRC32校验值
CRC32 func (starcpto *StarCrypto) CRC32(bstr []byte) string {
*/
func (this *StarCrypto) CRC32(bstr []byte) string {
crcsum := crc32.NewIEEE() crcsum := crc32.NewIEEE()
crcsum.Write(bstr) crcsum.Write(bstr)
this.Crc32 = crcsum.Sum(nil) starcpto.Crc32 = crcsum.Sum(nil)
return hex.EncodeToString(this.Crc32) return hex.EncodeToString(starcpto.Crc32)
} }
/* // SHA512 输出SHA512校验值
SHA512 func (starcpto *StarCrypto) SHA512(bstr []byte) string {
*/
func (this *StarCrypto) SHA512(bstr []byte) string {
shasum := sha512.New() shasum := sha512.New()
shasum.Write(bstr) shasum.Write(bstr)
this.Sha512 = shasum.Sum(nil) starcpto.Sha512 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha512) return hex.EncodeToString(starcpto.Sha512)
} }
/* // SHA256 输出SHA256校验值
SHA256 func (starcpto *StarCrypto) SHA256(bstr []byte) string {
*/
func (this *StarCrypto) SHA256(bstr []byte) string {
shasum := sha256.New() shasum := sha256.New()
shasum.Write(bstr) shasum.Write(bstr)
this.Sha256 = shasum.Sum(nil) starcpto.Sha256 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha256) return hex.EncodeToString(starcpto.Sha256)
} }
/* // SHA1 输出SHA1校验值
SHA1 func (starcpto *StarCrypto) SHA1(bstr []byte) string {
*/
func (this *StarCrypto) SHA1(bstr []byte) string {
shasum := sha1.New() shasum := sha1.New()
shasum.Write(bstr) shasum.Write(bstr)
this.Sha1 = shasum.Sum(nil) starcpto.Sha1 = shasum.Sum(nil)
return hex.EncodeToString(this.Sha1) return hex.EncodeToString(starcpto.Sha1)
} }
/* // SumAll 可以对同一数据进行多种校验
func (starcpto StarCrypto) SumAll(data []byte, method []string) (map[string]string, error) {
*/
func (this StarCrypto) SumAll(data []byte, method []string) (map[string]string, error) {
result := make(map[string]string) result := make(map[string]string)
methods := make(map[string]hash.Hash) methods := make(map[string]hash.Hash)
var iscrc bool var iscrc bool
@ -154,17 +139,15 @@ func (this StarCrypto) SumAll(data []byte, method []string) (map[string]string,
return result, nil return result, nil
} }
/* // FileSum 输出文件内容校验值method为单个校验方法,小写
method, //例FileSum("./test.txt","md5",shell(pect float64){fmt.Sprintf("已完成 %f\r",pect)})
FileSum("./test.txt","md5",shell(pect float64){fmt.Sprintf("已完成 %f\r",pect)}) func (starcpto StarCrypto) FileSum(filepath, method string, shell func(float64)) (string, error) {
*/
func (this StarCrypto) FileSum(filepath, method string, shell func(float64)) (string, error) {
var sum hash.Hash var sum hash.Hash
var sum32 hash.Hash32 var sum32 hash.Hash32
var issum32 bool var issum32 bool
var result string var result string
if !Exists(filepath) { if !Exists(filepath) {
return "", errors.New("File Not Exists!") return "", errors.New("file not exists")
} }
fp, err := os.Open(filepath) fp, err := os.Open(filepath)
if err != nil { if err != nil {
@ -229,10 +212,8 @@ func (this StarCrypto) FileSum(filepath, method string, shell func(float64)) (st
return result, nil return result, nil
} }
/* // FileSumAll 可以对同一文件进行多种校验
func (starcpto StarCrypto) FileSumAll(filepath string, method []string, shell func(float64)) (map[string]string, error) {
*/
func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(float64)) (map[string]string, error) {
result := make(map[string]string) result := make(map[string]string)
methods := make(map[string]hash.Hash) methods := make(map[string]hash.Hash)
var iscrc bool var iscrc bool
@ -241,7 +222,7 @@ func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(f
method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5"} method = []string{"sha512", "sha256", "sha384", "sha224", "sha1", "crc32", "md5"}
} }
if !Exists(filepath) { if !Exists(filepath) {
return result, errors.New("File Not Exists!") return result, errors.New("file not exists")
} }
fp, err := os.Open(filepath) fp, err := os.Open(filepath)
defer fp.Close() defer fp.Close()
@ -305,12 +286,13 @@ func (this StarCrypto) FileSumAll(filepath string, method []string, shell func(f
return result, nil return result, nil
} }
func (this StarCrypto) Attach(src, dst, output string) error { // Attach 合并src与dst文件并输出到output中
func (starcpto StarCrypto) Attach(src, dst, output string) error {
if !Exists(src) { if !Exists(src) {
return errors.New("Source File Not Exists!") return errors.New("source file not exists")
} }
if !Exists(dst) { if !Exists(dst) {
return errors.New("Dest File Not Exists!") return errors.New("dst file not exists")
} }
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
@ -344,9 +326,10 @@ func (this StarCrypto) Attach(src, dst, output string) error {
return nil return nil
} }
func (this StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error { // Detach 按bytenum字节大小分割src文件到dst1与dst2两个新文件中去
func (starcpto StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error {
if !Exists(src) { if !Exists(src) {
return errors.New("Source File Not Exists!") return errors.New("source file not exists")
} }
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
@ -395,9 +378,10 @@ func (this StarCrypto) Detach(src string, bytenum int, dst1, dst2 string) error
return nil return nil
} }
func (this StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) error { // Base64EncodeFile 用base64方法编码src文件到dst文件中去shell传入当前进度
func (starcpto StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) error {
if !Exists(src) { if !Exists(src) {
return errors.New("Source File Not Exists!") return errors.New("source file not exists")
} }
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
@ -429,9 +413,10 @@ func (this StarCrypto) Base64EncodeFile(src, dst string, shell func(float64)) er
return nil return nil
} }
func (this StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) error { // Base64DecodeFile 用base64方法解码src文件到dst文件中去shell传入当前进度
func (starcpto StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) error {
if !Exists(src) { if !Exists(src) {
return errors.New("Source File Not Exists!") return errors.New("source file not exists")
} }
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
@ -464,13 +449,12 @@ func (this StarCrypto) Base64DecodeFile(src, dst string, shell func(float64)) er
return nil return nil
} }
/* // SplitFile 把src文件按要求分割到dst中去,dst应传入带*号字符串
bynum=true num // 如果bynum=true 则把文件分割成num份
bynum=false num // 如果bynum=false 则把文件按num字节分成多份
*/ func (starcpto StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell func(float64)) error {
func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell func(float64)) error {
if !Exists(src) { if !Exists(src) {
return errors.New("Source File Not Exists!") return errors.New("source file not exists")
} }
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
@ -481,7 +465,7 @@ func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell fun
filebig := float64(stat.Size()) filebig := float64(stat.Size())
if bynum { if bynum {
if int(filebig) < num { if int(filebig) < num {
return errors.New("File is too small to split") return errors.New("file is too small to split")
} }
} }
balance := int(filebig/float64(num)) + 1 balance := int(filebig/float64(num)) + 1
@ -526,7 +510,8 @@ func (this StarCrypto) SplitFile(src, dst string, num int, bynum bool, shell fun
return nil return nil
} }
func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error { // MergeFile 合并src文件到dst文件中去src文件应传入带*号字符串
func (starcpto StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
tmp := strings.Replace(src, "*", "0", -1) tmp := strings.Replace(src, "*", "0", -1)
dir, err := ioutil.ReadDir(filepath.Dir(tmp)) dir, err := ioutil.ReadDir(filepath.Dir(tmp))
if err != nil { if err != nil {
@ -536,7 +521,7 @@ func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
tmp = strings.Replace(base, "*", "(\\d+)", -1) tmp = strings.Replace(base, "*", "(\\d+)", -1)
reg := regexp.MustCompile(tmp) reg := regexp.MustCompile(tmp)
count := 0 count := 0
var filebig float64 = 0 var filebig float64
for _, v := range dir { for _, v := range dir {
if reg.MatchString(v.Name()) { if reg.MatchString(v.Name()) {
count++ count++
@ -572,10 +557,11 @@ func (this StarCrypto) MergeFile(src, dst string, shell func(float64)) error {
return nil return nil
} }
func (this StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) { // RSAEncrypt RSA公钥加密
func (starcpto StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
blk, _ := pem.Decode(public) blk, _ := pem.Decode(public)
if blk == nil { if blk == nil {
return []byte{}, errors.New("public key error") return []byte{}, errors.New("public key error")
} }
pubkey, err := x509.ParsePKIXPublicKey(blk.Bytes) pubkey, err := x509.ParsePKIXPublicKey(blk.Bytes)
if err != nil { if err != nil {
@ -584,7 +570,8 @@ func (this StarCrypto) RSAEncrypt(data, public []byte) ([]byte, error) {
return rsa.EncryptPKCS1v15(rand.Reader, pubkey.(*rsa.PublicKey), data) return rsa.EncryptPKCS1v15(rand.Reader, pubkey.(*rsa.PublicKey), data)
} }
func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) { // RSADecrypt RSA私钥解密
func (starcpto StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte, error) {
var prikey *rsa.PrivateKey var prikey *rsa.PrivateKey
var err error var err error
var bytes []byte var bytes []byte
@ -612,7 +599,8 @@ func (this StarCrypto) RSADecrypt(data, private []byte, password string) ([]byte
return rsa.DecryptPKCS1v15(rand.Reader, prikey, data) return rsa.DecryptPKCS1v15(rand.Reader, prikey, data)
} }
func (this StarCrypto) RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) { // RSASign RSA私钥签名加密
func (starcpto StarCrypto) RSASign(hashdata, private []byte, password string, hashtype crypto.Hash) ([]byte, error) {
var prikey *rsa.PrivateKey var prikey *rsa.PrivateKey
var err error var err error
var bytes []byte var bytes []byte
@ -640,7 +628,8 @@ func (this StarCrypto) RSASign(hashdata, private []byte, password string, hashty
return rsa.SignPKCS1v15(rand.Reader, prikey, hashtype, hashdata) return rsa.SignPKCS1v15(rand.Reader, prikey, hashtype, hashdata)
} }
func (this StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error { // RSAVerify RSA公钥签名验证
func (starcpto StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.Hash) error {
blk, _ := pem.Decode(public) blk, _ := pem.Decode(public)
if blk == nil { if blk == nil {
return errors.New("public key error") return errors.New("public key error")
@ -652,7 +641,8 @@ func (this StarCrypto) RSAVerify(data, hashdata, public []byte, hashtype crypto.
return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashtype, hashdata, data) return rsa.VerifyPKCS1v15(pubkey.(*rsa.PublicKey), hashtype, hashdata, data)
} }
func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte { // VicqueEncodeV1 Best
func (starcpto StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {
var keys []int var keys []int
var saku, piku uint8 var saku, piku uint8
data := make([]byte, len(srcdata)) data := make([]byte, len(srcdata))
@ -699,7 +689,8 @@ func (this StarCrypto) VicqueEncodeV1(srcdata []byte, key string) []byte {
return data return data
} }
func (this StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte { // VicqueDecodeV1 Best
func (starcpto StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte {
var keys []int var keys []int
var saku, piku int var saku, piku int
data := make([]byte, len(srcdata)) data := make([]byte, len(srcdata))
@ -746,7 +737,8 @@ func (this StarCrypto) VicqueDecodeV1(srcdata []byte, key string) []byte {
return data[:lens] return data[:lens]
} }
func (this StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float64)) error { // VicqueEncodeV1File best
func (starcpto StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float64)) error {
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
return err return err
@ -772,13 +764,14 @@ func (this StarCrypto) VicqueEncodeV1File(src, dst, pwd string, shell func(float
} }
sum += n sum += n
go shell(float64(sum) / filebig * 100) go shell(float64(sum) / filebig * 100)
data := this.VicqueEncodeV1(buf[0:n], pwd) data := starcpto.VicqueEncodeV1(buf[0:n], pwd)
fpdst.Write(data) fpdst.Write(data)
} }
return nil return nil
} }
func (this StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float64)) error { // VicqueDecodeV1File best
func (starcpto StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float64)) error {
fpsrc, err := os.Open(src) fpsrc, err := os.Open(src)
if err != nil { if err != nil {
return err return err
@ -804,12 +797,13 @@ func (this StarCrypto) VicqueDecodeV1File(src, dst, pwd string, shell func(float
} }
sum += n sum += n
go shell(float64(sum) / filebig * 100) go shell(float64(sum) / filebig * 100)
data := this.VicqueDecodeV1(buf[0:n], pwd) data := starcpto.VicqueDecodeV1(buf[0:n], pwd)
fpdst.Write(data) fpdst.Write(data)
} }
return nil return nil
} }
// FillWithRandom 随机写filesize大小的文件每次buf大小为bufcap随机bufnum个字符
func FillWithRandom(filepath string, filesize int, bufcap int, bufnum int, shell func(float64)) error { func FillWithRandom(filepath string, filesize int, bufcap int, bufnum int, shell func(float64)) error {
var buf [][]byte var buf [][]byte
var buftmp []byte var buftmp []byte

@ -46,25 +46,76 @@ func (this *StarResultCol) MustBytes() [][]byte {
func (this *StarResultCol) MustBool() []bool { func (this *StarResultCol) MustBool() []bool {
var res []bool var res []bool
var tmp bool
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(bool)) switch vtype := v.(type) {
case nil:
tmp = false
case bool:
tmp = vtype
case float64, float32:
if vtype.(float64) > 0 {
tmp = true
} else {
tmp = false
}
case int, int32, int64:
if vtype.(int) > 0 {
tmp = true
} else {
tmp = false
}
case string:
tmp, _ = strconv.ParseBool(vtype)
default:
tmp, _ = strconv.ParseBool(string(vtype.([]byte)))
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResultCol) MustFloat32() []float32 { func (this *StarResultCol) MustFloat32() []float32 {
var res []float32 var res []float32
var tmp float32
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(float32)) switch vtype := v.(type) {
case nil:
tmp = 0
case float64, float32:
tmp = float32(vtype.(float64))
case string:
tmps, _ := strconv.ParseFloat(vtype, 32)
tmp = float32(tmps)
case int, int32, int64:
tmp = float32(vtype.(int64))
default:
tmp = v.(float32)
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResultCol) MustFloat64() []float64 { func (this *StarResultCol) MustFloat64() []float64 {
var res []float64 var res []float64
var tmp float64
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(float64)) switch vtype := v.(type) {
case nil:
tmp = 0
case float64, float32:
tmp = vtype.(float64)
case string:
tmp, _ = strconv.ParseFloat(vtype, 64)
case int, int32, int64:
tmp = float64(vtype.(int64))
default:
tmp = v.(float64)
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResultCol) MustString() []string { func (this *StarResultCol) MustString() []string {
var res []string var res []string
var tmp string var tmp string
@ -74,8 +125,20 @@ func (this *StarResultCol) MustString() []string {
tmp = "" tmp = ""
case string: case string:
tmp = vtype tmp = vtype
case int64:
tmp = strconv.FormatInt(vtype, 10)
case int32:
tmp = strconv.Itoa(int(vtype))
case bool:
tmp = strconv.FormatBool(vtype)
case float64:
tmp = strconv.FormatFloat(vtype, 'f', 10, 64)
case float32:
tmp = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
case int:
tmp = strconv.Itoa(vtype)
default: default:
tmp = v.(string) tmp = string(vtype.([]byte))
} }
res = append(res, tmp) res = append(res, tmp)
} }
@ -84,40 +147,109 @@ func (this *StarResultCol) MustString() []string {
func (this *StarResultCol) MustInt32() []int32 { func (this *StarResultCol) MustInt32() []int32 {
var res []int32 var res []int32
var tmp int32
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(int32)) switch vtype := v.(type) {
case nil:
tmp = 0
case float64, float32:
tmp = int32(vtype.(float64))
case string:
tmps, _ := strconv.ParseInt(vtype, 10, 32)
tmp = int32(tmps)
case int, int32, int64:
tmp = int32(vtype.(int64))
default:
tmp = v.(int32)
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResultCol) MustInt64() []int64 { func (this *StarResultCol) MustInt64() []int64 {
var res []int64 var res []int64
var tmp int64
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(int64)) switch vtype := v.(type) {
case nil:
tmp = 0
case float64, float32:
tmp = int64(vtype.(float64))
case string:
tmp, _ = strconv.ParseInt(vtype, 10, 64)
case int, int32, int64:
tmp = (vtype.(int64))
default:
tmp = v.(int64)
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResultCol) MustInt() []int { func (this *StarResultCol) MustInt() []int {
var res []int var res []int
var tmp int
for _, v := range this.Result { for _, v := range this.Result {
res = append(res, v.(int)) switch vtype := v.(type) {
case nil:
tmp = 0
case float64, float32:
tmp = int(vtype.(float64))
case string:
tmps, _ := strconv.ParseInt(vtype, 10, 64)
tmp = int(tmps)
case int, int32, int64:
tmp = int(vtype.(int64))
default:
tmp = int(v.(int64))
}
res = append(res, tmp)
} }
return res return res
} }
func (this *StarResult) MustInt64(name string) int64 { func (this *StarResult) MustInt64(name string) int64 {
var res int64
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return 0 return 0
} }
res := this.Result[num].(int64) tmp := this.Result[num]
switch vtype := tmp.(type) {
case nil:
res = 0
case float64, float32:
res = int64(vtype.(float64))
case string:
res, _ = strconv.ParseInt(vtype, 10, 64)
case int, int32, int64:
res = (vtype.(int64))
default:
res, _ = strconv.ParseInt(string(tmp.([]byte)), 10, 64)
}
return res return res
} }
func (this *StarResult) MustInt32(name string) int32 { func (this *StarResult) MustInt32(name string) int32 {
var res int32
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return 0 return 0
} }
res := this.Result[num].(int32) tmp := this.Result[num]
switch vtype := tmp.(type) {
case nil:
res = 0
case float64, float32:
res = int32(vtype.(float64))
case string:
ress, _ := strconv.ParseInt(vtype, 10, 32)
res = int32(ress)
case int, int32, int64:
res = int32(vtype.(int64))
default:
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 32)
res = int32(ress)
}
return res return res
} }
func (this *StarResult) MustString(name string) string { func (this *StarResult) MustString(name string) string {
@ -131,40 +263,123 @@ func (this *StarResult) MustString(name string) string {
res = "" res = ""
case string: case string:
res = vtype res = vtype
case int64:
res = strconv.FormatInt(vtype, 10)
case int32:
res = strconv.Itoa(int(vtype))
case bool:
res = strconv.FormatBool(vtype)
case float64:
res = strconv.FormatFloat(vtype, 'f', 10, 64)
case float32:
res = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
case int:
res = strconv.Itoa(vtype)
default:
res = string(vtype.([]byte))
} }
return res return res
} }
func (this *StarResult) MustFloat64(name string) float64 { func (this *StarResult) MustFloat64(name string) float64 {
var res float64
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return 0 return 0
} }
res := this.Result[num].(float64) switch vtype := this.Result[num].(type) {
case nil:
res = 0
case string:
res, _ = strconv.ParseFloat(vtype, 64)
case float64:
res = vtype
case int, int64, int32, float32:
res = vtype.(float64)
default:
res, _ = strconv.ParseFloat(string(vtype.([]byte)), 64)
}
return res return res
} }
func (this *StarResult) MustFloat32(name string) float32 { func (this *StarResult) MustFloat32(name string) float32 {
var res float32
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return 0 return 0
} }
res := this.Result[num].(float32) switch vtype := this.Result[num].(type) {
case nil:
res = 0
case string:
tmp, _ := strconv.ParseFloat(vtype, 32)
res = float32(tmp)
case float64:
res = float32(vtype)
case float32:
res = vtype
case int, int64, int32:
res = vtype.(float32)
default:
tmp, _ := strconv.ParseFloat(string(vtype.([]byte)), 32)
res = float32(tmp)
}
return res return res
} }
func (this *StarResult) MustInt(name string) int { func (this *StarResult) MustInt(name string) int {
var res int
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return 0 return 0
} }
res := this.Result[num].(int) tmp := this.Result[num]
switch vtype := tmp.(type) {
case nil:
res = 0
case float64, float32:
res = int(vtype.(float64))
case string:
ress, _ := strconv.ParseInt(vtype, 10, 64)
res = int(ress)
case int, int32, int64:
res = int(vtype.(int64))
default:
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 64)
res = int(ress)
}
return res return res
} }
func (this *StarResult) MustBool(name string) bool { func (this *StarResult) MustBool(name string) bool {
var res bool
num, ok := this.columnref[name] num, ok := this.columnref[name]
if !ok { if !ok {
return false return false
} }
res := this.Result[num].(bool) tmp := this.Result[num]
switch vtype := tmp.(type) {
case nil:
res = false
case bool:
res = vtype
case float64, float32:
if vtype.(float64) > 0 {
res = true
} else {
res = false
}
case int, int32, int64:
if vtype.(int) > 0 {
res = true
} else {
res = false
}
case string:
res, _ = strconv.ParseBool(vtype)
default:
res, _ = strconv.ParseBool(string(vtype.([]byte)))
}
return res return res
} }
func (this *StarResult) MustBytes(name string) []byte { func (this *StarResult) MustBytes(name string) []byte {
@ -212,7 +427,7 @@ func (this *StarRows) Close() error {
func (this *StarRows) parserows() { func (this *StarRows) parserows() {
this.result = [][]interface{}{} this.result = [][]interface{}{}
this.columnref = make(map[string]int) this.columnref = make(map[string]int)
this.StringResult = []map[string]string{make(map[string]string)} this.StringResult = []map[string]string{}
this.Columns, _ = this.Rows.Columns() this.Columns, _ = this.Rows.Columns()
types, _ := this.Rows.ColumnTypes() types, _ := this.Rows.ColumnTypes()
for _, v := range types { for _, v := range types {

@ -116,6 +116,32 @@ func (this *StarCurl) CurlWithFile(url string, postdata map[string]string, formn
return return
} }
func (this *StarCurl) CurlWithFileByBytes(url string, postdata map[string]string, formname, fname string, data []byte, savepath string, tofile bool) (result []byte, err error) {
buf := &bytes.Buffer{}
bufwriter := multipart.NewWriter(buf)
if postdata != nil {
for k, v := range postdata {
bufwriter.WriteField(k, v)
}
}
fpdst, err := bufwriter.CreateFormFile(formname, fname)
if err != nil {
return
}
fpdst.Write(data)
this.PostBuffer = buf
this.ReqHeader.Set("Content-Type", "multipart/form-data;boundary="+bufwriter.Boundary())
bufwriter.Close()
if tofile {
err = this.CurlDataToFile(url, []byte{}, "POST", savepath, func(float64) {})
this.ResetReqHeader()
} else {
result, err = this.Curl(url, []byte{}, "POST")
}
this.ResetReqHeader()
return
}
func (this *StarCurl) CurlWithFileByMemory(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) { func (this *StarCurl) CurlWithFileByMemory(url string, postdata map[string]string, formname, fpath, savepath string, tofile bool, shell func(float64)) (result []byte, err error) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
bufwriter := multipart.NewWriter(buf) bufwriter := multipart.NewWriter(buf)

@ -5,6 +5,9 @@ import (
"errors" "errors"
) )
/*
SecretKey Key
*/
const SecretKey string = "1996victorique1127B612BTXL" const SecretKey string = "1996victorique1127B612BTXL"
var header []byte = []byte{11, 27, 19, 96} var header []byte = []byte{11, 27, 19, 96}
@ -59,10 +62,10 @@ type MsgUsed struct {
ID uint16 ID uint16
Msg string Msg string
Crc32 string Crc32 string
Ip string Conn interface{}
} }
func (this *StarQueue) ParseMessage(msg []byte, ip string) int { func (this *StarQueue) ParseMessage(msg []byte, conn interface{}) int {
var buffer bytes.Buffer var buffer bytes.Buffer
buffer.Write(this.UnFinMsg) buffer.Write(this.UnFinMsg)
buffer.Write(msg) buffer.Write(msg)
@ -99,7 +102,7 @@ func (this *StarQueue) ParseMessage(msg []byte, ip string) int {
if this.Encode { if this.Encode {
strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey) strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey)
} }
msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), ip} msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), conn}
this.LastID = int(msgs.ID) this.LastID = int(msgs.ID)
this.MsgPool = append(this.MsgPool, msgs) this.MsgPool = append(this.MsgPool, msgs)
} }
@ -107,7 +110,7 @@ func (this *StarQueue) ParseMessage(msg []byte, ip string) int {
return -2 return -2
} }
msg = msg[length+6:] msg = msg[length+6:]
return this.ParseMessage(msg, ip) return this.ParseMessage(msg, conn)
} }
return -2 return -2
} }

@ -37,7 +37,7 @@ func NewCircleByteBuffer(len int) *CircleByteBuffer {
return e return e
} }
//Exits返回指定文件夹/文件是否存在 // Exists 返回指定文件夹/文件是否存在
func Exists(filepath string) bool { func Exists(filepath string) bool {
_, err := os.Stat(filepath) _, err := os.Stat(filepath)
if err != nil && os.IsNotExist(err) { if err != nil && os.IsNotExist(err) {
@ -46,7 +46,7 @@ func Exists(filepath string) bool {
return true return true
} }
//IsFile返回给定文件地址是否是一个文件 // IsFile 返回给定文件地址是否是一个文件,
//True为是一个文件,False为不是文件或路径无效 //True为是一个文件,False为不是文件或路径无效
func IsFile(fpath string) bool { func IsFile(fpath string) bool {
s, err := os.Stat(fpath) s, err := os.Stat(fpath)
@ -56,7 +56,7 @@ func IsFile(fpath string) bool {
return !s.IsDir() return !s.IsDir()
} }
//IsFolder返回给定文件地址是否是一个文件夹 // IsFolder 返回给定文件地址是否是一个文件夹,
//True为是一个文件夹,False为不是文件夹或路径无效 //True为是一个文件夹,False为不是文件夹或路径无效
func IsFolder(fpath string) bool { func IsFolder(fpath string) bool {
s, err := os.Stat(fpath) s, err := os.Stat(fpath)

Loading…
Cancel
Save