Parcourir la source

修改视频编辑

kobe6258 il y a 8 mois
Parent
commit
453eb74be8
4 fichiers modifiés avec 135 ajouts et 44 suppressions
  1. 106 27
      controllers/video.go
  2. 19 16
      models/media.go
  3. 1 1
      models/request/media.go
  4. 9 0
      routers/commentsRouter.go

+ 106 - 27
controllers/video.go

@@ -13,11 +13,9 @@ import (
 	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"io"
-	"math/rand"
 	"os"
 	"path"
 	"strconv"
-	"strings"
 	"time"
 )
 
@@ -160,34 +158,34 @@ func (this *VideoController) AddVideo() {
 		return
 	}
 	var err error
-	var coverSrc int
-	permissions := strings.Split(req.PermissionIds, ",")
-	permissionsId, err := strconv.Atoi(permissions[0])
-	if err != nil {
-		coverSrc = 0
-	} else {
-		var ids []int
-		ids, err = models.GetImageIdByPermissionId(permissionsId)
-		if err != nil {
-			br.Msg = "上传视频失败"
-			br.ErrMsg = "获取封面图片失败"
-			return
-		}
-		if ids == nil || len(ids) == 0 {
-			coverSrc = 0
-		} else {
-			rand.Seed(time.Now().UnixNano())
-			// 从切片中随机选择一个元素
-			randomIndex := rand.Intn(len(ids))
-			coverSrc = ids[randomIndex]
-		}
-	}
+	//var coverSrc int
+	//permissions := strings.Split(req.PermissionIds, ",")
+	//permissionsId, err := strconv.Atoi(permissions[0])
+	//if err != nil {
+	//	coverSrc = 0
+	//} else {
+	//	var ids []int
+	//	ids, err = models.GetImageIdByPermissionId(permissionsId)
+	//	if err != nil {
+	//		br.Msg = "上传视频失败"
+	//		br.ErrMsg = "获取封面图片失败"
+	//		return
+	//	}
+	//	if ids == nil || len(ids) == 0 {
+	//		coverSrc = 0
+	//	} else {
+	//		rand.Seed(time.Now().UnixNano())
+	//		// 从切片中随机选择一个元素
+	//		randomIndex := rand.Intn(len(ids))
+	//		coverSrc = ids[randomIndex]
+	//	}
+	//}
 	audioInsert := &models.Media{
 		AuthorId:              req.AnalystId,
 		AuthorName:            req.AnalystName,
 		MediaType:             models.Video,
 		Src:                   req.SrcUrl,
-		CoverSrc:              coverSrc,
+		CoverSrc:              "",
 		MediaName:             req.VideoName,
 		SourceType:            "mp4",
 		MediaPlayMilliseconds: req.DurationMillisecond,
@@ -263,7 +261,7 @@ func (this *VideoController) EditVideo() {
 		br.Msg = "视频时长为空"
 		return
 	}
-	if req.CoverSrcId <= 0 {
+	if req.CoverSrc == "" {
 		br.Msg = "封面为空"
 		return
 	}
@@ -273,7 +271,7 @@ func (this *VideoController) EditVideo() {
 		Id:                    req.VideoId,
 		AuthorId:              req.AnalystId,
 		AuthorName:            req.AnalystName,
-		CoverSrc:              req.CoverSrcId,
+		CoverSrc:              req.CoverSrc,
 		Src:                   req.SrcUrl,
 		MediaName:             req.VideoName,
 		MediaPlayMilliseconds: req.DurationMillisecond,
@@ -486,6 +484,87 @@ func getHeaderBoxInfo(data []byte) (boxHeader BoxHeader) {
 	return
 }
 
+// UploadFile @Title 上传图片
+// @Description 上传视频
+// @Param   File   query   file  true       "文件"
+// @Success 200 {object} models.ReportAuthorResp
+// @router /uploadFile [post]
+func (this *VideoController) UploadFile() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	f, h, err := this.GetFile("File")
+	if err != nil {
+		br.Msg = "获取资源信息失败"
+		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
+		return
+	}
+	defer f.Close()
+	size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
+	if err != nil {
+		size = 100
+	}
+	if h.Size > 1024*1024*int64(size) {
+		br.Msg = fmt.Sprintf("图片大小不能超过%dK", size)
+		br.ErrMsg = "图片上传失败,Err:" + err.Error()
+		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)
+	if err != nil {
+		br.Msg = "存储目录创建失败"
+		br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
+		return
+	}
+	randStr := utils.GetRandStringNoSpecialChar(28)
+	fileName := randStr + ext
+	fpath := uploadDir + "/" + fileName
+	err = this.SaveToFile("File", fpath)
+	if err != nil {
+		br.Msg = "图片上传失败"
+		br.ErrMsg = "图片上传失败,Err:" + err.Error()
+		return
+	}
+	audioUploadDir := utils.RESOURCE_DIR + "img/"
+	savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
+	audioName := utils.GetRandStringNoSpecialChar(28)
+	savePdfToOssPath += audioName + ext
+
+	defer func() {
+		err = os.Remove(fpath)
+		fmt.Sprintf("删除文件失败:%v", err)
+	}()
+	ossClient := services.NewOssClient()
+	if ossClient == nil {
+		br.Msg = "图片上传失败"
+		br.ErrMsg = "初始化OSS服务失败"
+		return
+	}
+	mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
+	if err != nil {
+		br.Msg = "图片上传失败"
+		br.ErrMsg = "图片上传失败,Err:" + err.Error()
+		return
+	}
+	base := path.Base(h.Filename)
+	resp := new(response.MediaUploadResp)
+	resp.Url = mp3Url
+	resp.FileName = base
+	br.Data = resp
+	br.Msg = "上传成功"
+	br.Ret = 200
+	br.Success = true
+}
+
 // getFourccType 获取信息头类型
 func getFourccType(boxHeader BoxHeader) (fourccType string) {
 	return string(boxHeader.FourccType[:])

+ 19 - 16
models/media.go

@@ -25,6 +25,7 @@ func (m *Media) ToView() *ESMedia {
 		AuthorId:              m.AuthorId,
 		AuthorName:            m.AuthorName,
 		MediaType:             m.MediaType,
+		CoverSrc:              m.CoverSrc,
 		Src:                   m.Src,
 		MediaName:             m.MediaName,
 		SourceType:            m.SourceType,
@@ -38,7 +39,7 @@ type ESMedia struct {
 	MediaId               int       `json:"mediaId"`
 	AuthorId              int       `json:"authorId"`
 	AuthorName            string    `json:"authorName"`
-	CoverSrc              int       `json:"coverSrc"`
+	CoverSrc              string    `json:"coverSrc"`
 	MediaType             MediaType `json:"mediaType"`
 	Src                   string    `json:"src"`
 	MediaName             string    `json:"mediaName"`
@@ -69,23 +70,24 @@ func (m *Media) ToMediaView() (message *MediaView) {
 		AuthorName:            m.AuthorName,
 		MediaType:             m.MediaType,
 		Src:                   m.Src,
+		CoverSrc:              m.CoverSrc,
 		MediaName:             m.MediaName,
 		SourceType:            m.SourceType,
 		MediaPlayMilliseconds: m.MediaPlayMilliseconds,
 		PermissionIds:         m.PermissionIds,
 		PublishedTime:         m.PublishedTime.Format(time.DateTime),
 	}
-	image, err := GetImageById(m.CoverSrc)
-	if err != nil || image == nil {
-		message.CoverSrc = ""
-	} else {
-		message.CoverSrc = image.SrcUrl
-	}
+	//image, err := GetImageById(m.CoverSrc)
+	//if err != nil || image == nil {
+	//	message.CoverSrc = ""
+	//} else {
+	//	message.CoverSrc = image.SrcUrl
+	//}
 	ids := strings.Split(m.PermissionIds, ",")
 	var idList []int
 	for _, id := range ids {
 		var idInt int
-		idInt, err = strconv.Atoi(id)
+		idInt, err := strconv.Atoi(id)
 		if err != nil {
 			utils.FileLog.Error("转换品种名称失败:%v", err)
 			break
@@ -108,6 +110,7 @@ func (m *Media) ToMessageView() (message *MessageMedia) {
 		AuthorName:            m.AuthorName,
 		MediaType:             m.MediaType,
 		Src:                   m.Src,
+		CoverSrc:              m.CoverSrc,
 		MediaName:             m.MediaName,
 		SourceType:            m.SourceType,
 		SendStatus:            m.SendStatus,
@@ -115,17 +118,17 @@ func (m *Media) ToMessageView() (message *MessageMedia) {
 		PermissionIds:         m.PermissionIds,
 		PublishedTime:         m.PublishedTime.Format(time.DateTime),
 	}
-	image, err := GetImageById(m.CoverSrc)
-	if err != nil || image == nil {
-		message.CoverSrc = ""
-	} else {
-		message.CoverSrc = image.SrcUrl
-	}
+	//image, err := GetImageById(m.CoverSrc)
+	//if err != nil || image == nil {
+	//	message.CoverSrc = ""
+	//} else {
+	//	message.CoverSrc = image.SrcUrl
+	//}
 	ids := strings.Split(m.PermissionIds, ",")
 	var idList []int
 	for _, id := range ids {
 		var idInt int
-		idInt, err = strconv.Atoi(id)
+		idInt, err := strconv.Atoi(id)
 		if err != nil {
 			utils.FileLog.Error("转换品种名称失败:%v", err)
 			break
@@ -161,7 +164,7 @@ type Media struct {
 	Id                    int        `orm:"pk;auto" description:"id"`
 	AuthorId              int        `description:"author_id"`
 	AuthorName            string     `description:"author_name"`
-	CoverSrc              int        `description:"cover_src"`
+	CoverSrc              string     `description:"cover_src"`
 	MediaType             MediaType  `description:"media_type"`
 	Src                   string     `description:"src"`
 	MediaName             string     `description:"media_name"`

+ 1 - 1
models/request/media.go

@@ -14,7 +14,7 @@ type VideoReq struct {
 	VideoName           string
 	AnalystId           int
 	AnalystName         string
-	CoverSrcId          int
+	CoverSrc            string
 	SrcUrl              string
 	PermissionIds       string
 	DurationMillisecond int

+ 9 - 0
routers/commentsRouter.go

@@ -684,6 +684,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:VideoController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:VideoController"],
+        beego.ControllerComments{
+            Method: "UploadFile",
+            Router: `/uploadFile`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:VideoController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:VideoController"],
         beego.ControllerComments{
             Method: "UploadVideo",