|
@@ -1,20 +1,14 @@
|
|
|
package three_party
|
|
package three_party
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "context"
|
|
|
"errors"
|
|
"errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
- uuid "github.com/iris-contrib/go.uuid"
|
|
|
|
|
- "github.com/kataras/iris/v12"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7"
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
- "io"
|
|
|
|
|
"log"
|
|
"log"
|
|
|
- "mime/multipart"
|
|
|
|
|
- "os"
|
|
|
|
|
- "path/filepath"
|
|
|
|
|
- "strings"
|
|
|
|
|
|
|
+ "path"
|
|
|
"sync"
|
|
"sync"
|
|
|
- "time"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var defaultFileBucket = "file"
|
|
var defaultFileBucket = "file"
|
|
@@ -22,16 +16,26 @@ var defaultIconBucket = "icon"
|
|
|
var defaultLocation = "us-east-1"
|
|
var defaultLocation = "us-east-1"
|
|
|
var minioServerPool *sync.Pool
|
|
var minioServerPool *sync.Pool
|
|
|
|
|
|
|
|
|
|
+var defaultConfig = &MinioConfig{
|
|
|
|
|
+ EndPoint: "http://114.116.19.14:9000",
|
|
|
|
|
+ AccessKey: "root",
|
|
|
|
|
+ SecretKey: "Houyaf1!",
|
|
|
|
|
+ UseSSL: false,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// client version and secret, it should be from client side
|
|
// client version and secret, it should be from client side
|
|
|
const (
|
|
const (
|
|
|
UploadDir string = "./public/upload/"
|
|
UploadDir string = "./public/upload/"
|
|
|
UrlBase string = "/upload/"
|
|
UrlBase string = "/upload/"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-// DstFile 文件
|
|
|
|
|
-type DstFile struct {
|
|
|
|
|
- Name string `json:"name"`
|
|
|
|
|
- Src string `json:"src"`
|
|
|
|
|
|
|
+// DestFile 文件
|
|
|
|
|
+type DestFile struct {
|
|
|
|
|
+ FileName string
|
|
|
|
|
+ FileUrl string
|
|
|
|
|
+ FileSize int64
|
|
|
|
|
+ FileExt string
|
|
|
|
|
+ FileMimeType string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
@@ -43,29 +47,23 @@ func init() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type MinioFileObject struct {
|
|
type MinioFileObject struct {
|
|
|
- BucketName string
|
|
|
|
|
ObjectName string
|
|
ObjectName string
|
|
|
FilePath string
|
|
FilePath string
|
|
|
ContentType string
|
|
ContentType string
|
|
|
- Config MinioConfig
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type MinioConfig struct {
|
|
type MinioConfig struct {
|
|
|
- MinioHost string `json:"minioHost"`
|
|
|
|
|
- MinioPort int64 `json:"minioPort"`
|
|
|
|
|
|
|
+ EndPoint string `json:"endPoint"`
|
|
|
AccessKey string `json:"accessKey"`
|
|
AccessKey string `json:"accessKey"`
|
|
|
SecretKey string `json:"secretKey"`
|
|
SecretKey string `json:"secretKey"`
|
|
|
UseSSL bool `json:"useSSL"`
|
|
UseSSL bool `json:"useSSL"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
func (c *MinioConfig) Validate() error {
|
|
func (c *MinioConfig) Validate() error {
|
|
|
- if c.MinioHost == "" {
|
|
|
|
|
- return errors.New("minio host not specified")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if c.MinioPort <= 0 {
|
|
|
|
|
- return errors.New("minio port not specified")
|
|
|
|
|
|
|
+ if c.EndPoint == "" {
|
|
|
|
|
+ return errors.New("EndPoint host not specified")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if c.AccessKey == "" {
|
|
if c.AccessKey == "" {
|
|
@@ -83,16 +81,15 @@ func (c *MinioConfig) GetClient()(*minio.Client, error) {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- endPoint := fmt.Sprintf("%s:%d", c.MinioHost, c.MinioPort)
|
|
|
|
|
minioClient, err := minio.New(
|
|
minioClient, err := minio.New(
|
|
|
- endPoint,
|
|
|
|
|
|
|
+ c.EndPoint,
|
|
|
&minio.Options{
|
|
&minio.Options{
|
|
|
Creds: credentials.NewStaticV4(c.AccessKey, c.SecretKey, ""),
|
|
Creds: credentials.NewStaticV4(c.AccessKey, c.SecretKey, ""),
|
|
|
Secure: c.UseSSL,
|
|
Secure: c.UseSSL,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.Fatalf("connect minio server fail %s url %s", err.Error(), endPoint)
|
|
|
|
|
|
|
+ log.Fatalf("connect minio server fail %s url %s", err.Error(), c.EndPoint)
|
|
|
}
|
|
}
|
|
|
return minioClient, nil
|
|
return minioClient, nil
|
|
|
}
|
|
}
|
|
@@ -115,7 +112,7 @@ func (m *MinioServe) Reset() {
|
|
|
m.Client = nil
|
|
m.Client = nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *MinioServe) checkBucket(ctx iris.Context, bucketName string) bool {
|
|
|
|
|
|
|
+func (m *MinioServe) checkBucket(ctx context.Context, bucketName string) bool {
|
|
|
isExist, err1 := m.Client.BucketExists(ctx, bucketName)
|
|
isExist, err1 := m.Client.BucketExists(ctx, bucketName)
|
|
|
if err1 != nil {
|
|
if err1 != nil {
|
|
|
log.Fatalf("checkBucket error " + err1.Error())
|
|
log.Fatalf("checkBucket error " + err1.Error())
|
|
@@ -132,27 +129,40 @@ func (m *MinioServe) checkBucket(ctx iris.Context, bucketName string) bool {
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *MinioServe) CheckBuckets(ctx iris.Context) bool {
|
|
|
|
|
|
|
+func (m *MinioServe) CheckBuckets(ctx context.Context) bool {
|
|
|
b1 := m.checkBucket(ctx, defaultFileBucket)
|
|
b1 := m.checkBucket(ctx, defaultFileBucket)
|
|
|
b2 := m.checkBucket(ctx, defaultIconBucket)
|
|
b2 := m.checkBucket(ctx, defaultIconBucket)
|
|
|
return b1&&b2
|
|
return b1&&b2
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (m *MinioServe) UploadFile(ctx iris.Context, obj *MinioFileObject)(*minio.UploadInfo, error) {
|
|
|
|
|
|
|
+func (m *MinioServe) UploadFile(objectName, filePath string)(*DestFile, error) {
|
|
|
|
|
+ ctx := context.Background()
|
|
|
bBucketExist := m.CheckBuckets(ctx)
|
|
bBucketExist := m.CheckBuckets(ctx)
|
|
|
if !bBucketExist {
|
|
if !bBucketExist {
|
|
|
- return nil, nil
|
|
|
|
|
|
|
+ return nil , errors.New("Bucket不存在")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- info, err := m.Client.FPutObject(ctx, obj.BucketName, obj.ObjectName, obj.FilePath, minio.PutObjectOptions{
|
|
|
|
|
- ContentType: obj.ContentType,
|
|
|
|
|
|
|
+ contentType := "application/octet-stream"
|
|
|
|
|
+ uinfo, err := m.Client.FPutObject(ctx, defaultFileBucket, objectName, filePath, minio.PutObjectOptions{
|
|
|
|
|
+ ContentType: contentType,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return &info, err
|
|
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return &info, nil
|
|
|
|
|
|
|
+ fileExt := path.Ext(objectName)
|
|
|
|
|
+
|
|
|
|
|
+ url := fmt.Sprintf("%s/%s/%s", defaultConfig.EndPoint, defaultFileBucket, objectName)
|
|
|
|
|
+ targetFile := &DestFile{
|
|
|
|
|
+ FileName: objectName,
|
|
|
|
|
+ FileUrl: url,
|
|
|
|
|
+ FileSize: uinfo.Size,
|
|
|
|
|
+ FileExt: fileExt,
|
|
|
|
|
+ FileMimeType: "",
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return targetFile, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -166,115 +176,17 @@ func releaseMinioServer(m *MinioServe) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-func UploadFile(ctx iris.Context) (*DstFile,error){
|
|
|
|
|
- file ,info ,err := ctx.FormFile("uploadfile")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return nil ,err
|
|
|
|
|
- }
|
|
|
|
|
- defer file.Close()
|
|
|
|
|
-
|
|
|
|
|
- oldFileName := info.Filename
|
|
|
|
|
- destFile, url := GenerateNewFileName(oldFileName)
|
|
|
|
|
-
|
|
|
|
|
- out, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
|
- if err != nil{
|
|
|
|
|
- return nil, err
|
|
|
|
|
- }
|
|
|
|
|
|
|
+func UploadFile(fileName, filePath string) (*DestFile,error){
|
|
|
|
|
+ m := acquireMinioServer()
|
|
|
|
|
|
|
|
- defer out.Close()
|
|
|
|
|
- _ ,err = io.Copy(out, file)
|
|
|
|
|
|
|
+ client, err := defaultConfig.GetClient()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- outfile := &DstFile{Name: oldFileName ,Src:url }
|
|
|
|
|
-
|
|
|
|
|
- return outfile, nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// UploadMultiFiles Upload Multi Files
|
|
|
|
|
-func UploadMultiFiles(ctx iris.Context)(int, []DstFile, error){
|
|
|
|
|
-
|
|
|
|
|
- maxSize := ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()
|
|
|
|
|
-
|
|
|
|
|
- err := ctx.Request().ParseMultipartForm(maxSize)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return 0 , nil, err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- form := ctx.Request().MultipartForm
|
|
|
|
|
- files := form.File["files[]"]
|
|
|
|
|
- count := len(files)
|
|
|
|
|
-
|
|
|
|
|
- failures := 0
|
|
|
|
|
- dstIndex := 1
|
|
|
|
|
-
|
|
|
|
|
- outFiles := []DstFile{}
|
|
|
|
|
- for _, file := range files {
|
|
|
|
|
- url, err := SaveUploadFile(file)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- failures++
|
|
|
|
|
- }else{
|
|
|
|
|
- out := DstFile{Name: file.Filename ,Src:url}
|
|
|
|
|
- outFiles = append(outFiles, out)
|
|
|
|
|
- dstIndex++
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- num := count - failures
|
|
|
|
|
-
|
|
|
|
|
- return num ,outFiles ,nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func SaveUploadFile(fh *multipart.FileHeader)(string, error){
|
|
|
|
|
- src ,err := fh.Open()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return "" ,err
|
|
|
|
|
- }
|
|
|
|
|
- defer src.Close()
|
|
|
|
|
-
|
|
|
|
|
- oldFileName := fh.Filename
|
|
|
|
|
- srcFile, urlFile := GenerateNewFileName(oldFileName)
|
|
|
|
|
-
|
|
|
|
|
- out, err := os.OpenFile(srcFile ,os.O_WRONLY|os.O_CREATE ,os.FileMode(0666))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return "" ,err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- defer out.Close()
|
|
|
|
|
-
|
|
|
|
|
- _, err = io.Copy(out, src)
|
|
|
|
|
-
|
|
|
|
|
- return urlFile, err
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// CreateDateDir Create date directory
|
|
|
|
|
-func CreateDateDir() (srcPath, urlPath string) {
|
|
|
|
|
- folderName := time.Now().Format("20060102")
|
|
|
|
|
- src := filepath.Join(UploadDir, folderName)
|
|
|
|
|
- url := filepath.Join(UrlBase, folderName)
|
|
|
|
|
- _, err := os.Stat(srcPath)
|
|
|
|
|
- if os.IsNotExist(err ){
|
|
|
|
|
- _ = os.Mkdir(src, os.ModePerm)
|
|
|
|
|
- _ = os.Chmod(src, os.ModePerm)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return src ,url
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// GenerateNewFileName Generate new file name
|
|
|
|
|
-func GenerateNewFileName(oldFileName string) (srcPath, urlPath string) {
|
|
|
|
|
- u1, _ := uuid.NewV4()
|
|
|
|
|
- newFileName := u1.String()
|
|
|
|
|
- src , url := CreateDateDir()
|
|
|
|
|
-
|
|
|
|
|
- dotIndex := strings.LastIndex(oldFileName, ".")
|
|
|
|
|
- if dotIndex != -1 && dotIndex != 0 {
|
|
|
|
|
- newFileName += oldFileName[dotIndex:]
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- src = filepath.Join(src, newFileName)
|
|
|
|
|
- url = filepath.Join(url, newFileName)
|
|
|
|
|
-
|
|
|
|
|
- return src, url
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
|
+ releaseMinioServer(m)
|
|
|
|
|
+ }()
|
|
|
|
|
+ m.Client = client
|
|
|
|
|
+ objName := ""
|
|
|
|
|
+ return m.UploadFile(objName, filePath)
|
|
|
}
|
|
}
|