فهرست منبع

视频格式扩展

kobe6258 1 روز پیش
والد
کامیت
48685651fb
4فایلهای تغییر یافته به همراه113 افزوده شده و 11 حذف شده
  1. 20 6
      controllers/video.go
  2. 76 5
      services/minio.go
  3. 9 0
      services/oss.go
  4. 8 0
      utils/config.go

+ 20 - 6
controllers/video.go

@@ -19,6 +19,13 @@ import (
 	"time"
 )
 
+var (
+	videoType = map[string]int{
+		"mp4": 1,
+		"m4a": 1,
+	}
+)
+
 type VideoController struct {
 	BaseAuthController
 }
@@ -51,11 +58,12 @@ func (this *VideoController) UploadVideo() {
 		return
 	}
 	ext := path.Ext(h.Filename)
-	if ext != ".mp4" {
+	if _, ok := videoType[ext]; !ok {
 		br.Msg = "视频格式不正确"
 		br.ErrMsg = "视频上传失败,Err:" + err.Error()
 		return
 	}
+
 	dateDir := time.Now().Format("20060102")
 	uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir
 	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
@@ -499,11 +507,7 @@ func (this *VideoController) UploadFile() {
 		return
 	}
 	ext := path.Ext(h.Filename)
-	//if ext != ".mp3" {
-	//	br.Msg = "音频格式不正确"
-	//	br.ErrMsg = "音频上传失败,Err:" + err.Error()
-	//	return
-	//}
+
 	dateDir := time.Now().Format("20060102")
 	uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir
 	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
@@ -552,6 +556,16 @@ func (this *VideoController) UploadFile() {
 	br.Success = true
 }
 
+type Chunk struct {
+	Filename    string
+	UniqueCode  string
+	ChunkNumber int
+	TotalChunks int
+	Data        []byte
+}
+
+var chunksMap = make(map[string][]Chunk)
+
 // getFourccType 获取信息头类型
 func getFourccType(boxHeader BoxHeader) (fourccType string) {
 	return string(boxHeader.FourccType[:])

+ 76 - 5
services/minio.go

@@ -5,11 +5,9 @@ import (
 	"errors"
 	"eta/eta_mini_crm_ht/utils"
 	"fmt"
-	"log"
-	"time"
-
 	"github.com/minio/minio-go/v7"
 	"github.com/minio/minio-go/v7/pkg/credentials"
+	"time"
 )
 
 type MinioOss struct{}
@@ -37,7 +35,7 @@ func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, erro
 		Secure: useSSL,
 	})
 	if err != nil {
-		log.Fatalln(err)
+		utils.FileLog.Error(err.Error())
 		return "1", err
 	}
 	bucketName := utils.MinIoBucketname
@@ -53,7 +51,7 @@ func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, erro
 	}
 	_, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
 	if err != nil {
-		log.Fatalln(err)
+		utils.FileLog.Error(err.Error())
 		return "3", err
 	}
 	resourceUrl := utils.MinIoPdfhost + path
@@ -71,3 +69,76 @@ func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
 	token.Port = utils.MinIoPort
 	return
 }
+
+func (m *MinioOss) GetFile(filePath, savePath string) (err error) {
+	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
+		err = errors.New("MinIo信息未配置")
+		return
+	}
+	ctx := context.Background()
+	// 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
+	endpoint := utils.MinIoEndpoint
+	if utils.MinIoBackEndpoint != "" {
+		endpoint = utils.MinIoBackEndpoint
+	}
+	accessKeyID := utils.MinIoAccessKeyId
+	secretAccessKey := utils.MinIoAccessKeySecret
+	useSSL := false
+	if utils.MinIoUseSSL == "true" {
+		useSSL = true
+	}
+	minioClient, err := minio.New(endpoint, &minio.Options{
+		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
+		Secure: useSSL,
+	})
+	if err != nil {
+		utils.FileLog.Error(err.Error())
+		return
+	}
+	bucketName := utils.MinIoBucketname
+	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
+	if errBucketExists != nil || !exists {
+		err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
+		return
+	}
+	err = minioClient.FGetObject(ctx, bucketName, filePath, savePath, minio.GetObjectOptions{})
+	if err != nil {
+		utils.FileLog.Error(err.Error())
+		return
+	}
+	return
+}
+func (m *MinioOss) MultiUploadFile() (err error) {
+	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
+		err = errors.New("MinIo信息未配置")
+		return
+	}
+	ctx := context.Background()
+	// 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
+	endpoint := utils.MinIoEndpoint
+	if utils.MinIoBackEndpoint != "" {
+		endpoint = utils.MinIoBackEndpoint
+	}
+	accessKeyID := utils.MinIoAccessKeyId
+	secretAccessKey := utils.MinIoAccessKeySecret
+	useSSL := false
+	if utils.MinIoUseSSL == "true" {
+		useSSL = true
+	}
+	minioClient, err := minio.New(endpoint, &minio.Options{
+		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
+		Secure: useSSL,
+	})
+	if err != nil {
+		utils.FileLog.Error(err.Error())
+		return
+	}
+	bucketName := utils.MinIoBucketname
+	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
+	if errBucketExists != nil || !exists {
+		err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
+		return
+	}
+
+	return
+}

+ 9 - 0
services/oss.go

@@ -26,6 +26,9 @@ type STSToken struct {
 type OssClient interface {
 	UploadFile(string, string, string) (string, error)
 	GetUploadToken() (OssToken, error)
+	GetFile(filePath, savePath string) (err error)
+
+	MultiUploadFile() error
 }
 
 func NewOssClient() OssClient {
@@ -202,3 +205,9 @@ func (m *AliOss) GetUploadToken() (token OssToken, err error) {
 	token.Imghost = stsToken.Imghost
 	return
 }
+func (m *AliOss) MultiUploadFile() (err error) {
+	return
+}
+func (m *AliOss) GetFile(filePath, savePath string) (err error) {
+	return
+}

+ 8 - 0
utils/config.go

@@ -66,6 +66,8 @@ var (
 	UPLOAD_IMG_SIZE   string
 	UPLOAD_AUDIO_SIZE string
 	UPLOAD_VIDEO_SIZE string
+
+	UPLOAD_MULTI_VIDEO_SIZE string
 )
 
 // ES配置
@@ -238,6 +240,12 @@ func init() {
 	if UPLOAD_VIDEO_SIZE == "" {
 		UPLOAD_VIDEO_SIZE = "1000"
 	}
+
+	UPLOAD_MULTI_VIDEO_SIZE = config["upload_multi_video_size"]
+	if UPLOAD_MULTI_VIDEO_SIZE == "" {
+		UPLOAD_MULTI_VIDEO_SIZE = "1"
+	}
+
 	// 初始化缓存
 	redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
 	if err != nil {