123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- package utils
- import (
- "context"
- "errors"
- "fmt"
- "git.getensh.com/common/gopkgs/cache"
- "github.com/minio/minio-go/v6"
- "io"
- "log"
- "math/rand"
- hurl "net/url"
- "property-applete-gateway/parser"
- "strconv"
- "strings"
- "time"
- pb_v1 "property-applete-gateway/pb/v1"
- "property-applete-gateway/pb"
- )
- const (
- NUmStr = "0123456789"
- CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- SpecStr = "+=-@#~,.[]()!%^*$"
- )
- func GenerateRandomStr(length int, charset string) string {
- time.Sleep(1*time.Microsecond)
- rand.Seed(time.Now().UnixNano())
- //初始化密码切片
- var passwd []byte = make([]byte, length, length)
- //源字符串
- var sourceStr string
- //判断字符类型,如果是数字
- if charset == "num" {
- sourceStr = NUmStr
- //如果选的是字符
- } else if charset == "char" {
- sourceStr = charset
- //如果选的是混合模式
- } else if charset == "mix" {
- sourceStr = fmt.Sprintf("%s%s", NUmStr, CharStr)
- //如果选的是高级模式
- } else if charset == "advance" {
- sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
- } else {
- sourceStr = fmt.Sprintf("%s%s%s", NUmStr, CharStr, SpecStr)
- }
- //遍历,生成一个随机index索引,
- for i := 0; i < length; i++ {
- index := rand.Intn(len(sourceStr))
- passwd[i] = sourceStr[index]
- }
- return string(passwd)
- }
- func UploadToMinio(fileName string, r io.Reader, size int64, imgMine string) (objName string, err error) {
- endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return "", err
- }
- // Make a new bucket called mymusic.
- bucketName := parser.Conf.Oss.PropertyCompanyBucket
- contentType := imgMine
- objectName := ""
- count := 0
- exist := false
- array := strings.Split(fileName, ".")
- tail := ""
- if len(array) > 0 {
- tail = array[len(array)-1]
- }
- rkeyPrefix := "oss_obj_exist_"
- rkey := ""
- defer func() {
- if rkey != "" {
- cache.RedisUnlock(rkey)
- }
- }()
- for ;count < 10;count++ {
- if rkey != "" {
- cache.RedisUnlock(rkey)
- rkey = ""
- }
- objectName = fmt.Sprintf("%d####", time.Now().Unix()) + GenerateRandomStr(32, "mix") +"."+tail
- // 互斥判断文件是否存在
- rkey = rkeyPrefix + objectName
- if !cache.RedisLock(rkey) {
- time.Sleep(200*time.Millisecond)
- rkey = ""
- continue
- }
- exist, err = ObjExist(endpoint, bucketName, objectName)
- if err != nil {
- return "", err
- }
- if exist {
- continue
- }
- // Upload the zip file with FPutObject
- _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType:contentType})
- if err != nil {
- return "", err
- }
- ret := parser.Conf.Oss.Protocol +"://"+endpoint+"/"+bucketName+"/"+hurl.QueryEscape(objectName)
- ObjTaskAdd(ret)
- return ret, nil
- }
- if exist {
- return "", errors.New("文件已存在")
- }
- return "", errors.New("系统繁忙")
- }
- func parseObjUrl(objUrl string) (string, string, string, string) {
- array := strings.Split(objUrl, "://")
- protocol, endpoint, bucketName, objName := "", "", "", ""
- if len(array) < 2 {
- return "", "", "", ""
- }
- protocol = array[0]
- array = strings.Split(array[1], "/")
- if len(array) != 3 {
- return "", "", "", ""
- }
- endpoint = array[0]
- bucketName = array[1]
- objName = array[2]
- return protocol, endpoint, bucketName, objName
- }
- const ObjKey = "applete_minio_obj"
- func ObjTaskAdd(objUrl string) {
- value := fmt.Sprintf("%s", objUrl)
- cache.Redis().SAdd(ObjKey, value)
- }
- func delNotExist(objUrl string) error {
- mreq := pb_v1.OssObjDelNotExistRequest{ObjUrl:objUrl}
- _, err := pb.Common.OssObjDelNotExist(context.Background(), &mreq)
- fmt.Printf("************:%v,%v\n", objUrl, err)
- return err
- }
- func ObjTaskHandle() {
- count := 0
- for count < 20 {
- count++
- objStr, _ := cache.Redis().SPop(ObjKey)
- fmt.Printf("************:%s\n", objStr)
- if objStr == "" {
- break
- }
- _, _, _, objName := parseObjUrl(objStr)
- array := strings.Split(objName, "%23%23%23%23")
- if len(array) != 2 {
- continue
- }
- timeStr := array[0]
- timestamp, _ := strconv.ParseInt(timeStr, 10, 64)
- if false {
- if delNotExist(objStr) != nil {
- cache.Redis().SAdd(ObjKey, objStr)
- }
- continue
- }
- if time.Now().Unix() - timestamp < 600 {
- cache.Redis().SAdd(ObjKey, objStr)
- time.Sleep(1*time.Second)
- continue
- }
- if delNotExist(objStr) != nil {
- cache.Redis().SAdd(ObjKey, objStr)
- }
- }
- }
- func ObjTask(ctx context.Context) {
- if false {
- tmp := ""
- fmt.Printf("input:\n")
- fmt.Scanln(&tmp)
- fmt.Printf("after input\n")
- ObjTaskHandle()
- }
- t := time.NewTicker(600 * time.Second)
- for {
- select {
- case <-t.C:
- ObjTaskHandle()
- case <-ctx.Done():
- return
- }
- }
- }
- func ObjExist(endpoint, bucketName, objName string) (bool, error) {
- if endpoint == "" {
- return false, nil
- }
- //endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return false, err
- }
- // Make a new bucket called mymusic.
- //bucketName := parser.Conf.Oss.PropertyCompanyBucket
- //objName := ""
- obj, err := minioClient.GetObject(bucketName, objName, minio.GetObjectOptions{})
- if err != nil {
- return false, err
- }
- defer obj.Close()
- _, err = obj.Stat()
- if err != nil {
- if strings.Contains(err.Error(), " not exist") {
- return false, nil
- }
- return false, err
- }
- return true, nil
- }
- func RemoveFromMinio(objUrl string) (err error) {
- _, endpoint, bucketName, objName := parseObjUrl(objUrl)
- if endpoint == "" {
- return nil
- }
- //endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return err
- }
- // Make a new bucket called mymusic.
- //bucketName := parser.Conf.Oss.PropertyCompanyBucket
- //objName := ""
- err = minioClient.RemoveObject(bucketName, objName)
- if err != nil {
- return err
- }
- return nil
- }
- func GetFilePath(objName string) (string, error) {
- endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return "", err
- }
- // Make a new bucket called mymusic.
- bucketName := parser.Conf.Oss.PropertyCompanyBucket
- rr, er := minioClient.PresignedGetObject(bucketName, objName, 24*time.Hour, hurl.Values{})
- if er != nil {
- fmt.Printf("获取文件路径失败:%v\n", er)
- return "", er
- }
- return rr.String(), nil
- }
- func MiniTest() {
- endpoint := "47.108.135.38:9000"
- accessKeyID := "minioadmin"
- secretAccessKey := "hly@1353406"
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- log.Fatalln(err)
- }
- // Make a new bucket called mymusic.
- bucketName := "testb"
- location := ""
- err = minioClient.MakeBucket(bucketName, location)
- if err != nil {
- // Check to see if we already own this bucket (which happens if you run this twice)
- exists, errBucketExists := minioClient.BucketExists(bucketName)
- if errBucketExists == nil && exists {
- log.Printf("We already own %s\n", bucketName)
- } else {
- log.Fatalln(err)
- }
- } else {
- log.Printf("Successfully created %s\n", bucketName)
- }
- // Upload the zip file
- objectName := "5.png"
- filePath := "D:\\5.png"
- contentType := ""
- // Upload the zip file with FPutObject
- n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType})
- if err != nil {
- log.Fatalln(err)
- }
- log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
- rr, er := minioClient.PresignedGetObject("testb", objectName, 24 * 365 * 100 *time.Hour, hurl.Values{})
- if er != nil {
- fmt.Printf("xxxx:%v\n", er)
- return
- }
- fmt.Printf("xxxx:%s\n", rr.String())
- }
|