rubbishclass-srv/admin/video-handler.go

117 lines
2.2 KiB
Go

package admin
import (
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/prometheus/common/log"
"yyjishu.com/rubbish-class/video"
)
func VideoStatHandler(c *gin.Context) {
}
func VideoListHandler(c *gin.Context) {
}
func VideoDownloadHandler(c *gin.Context) {
}
//HistoryVideoHandle 视频(文件)下载
func HistoryVideoHandler(c *gin.Context) {
openid := c.Param("openid")
p := "./video/" + openid
files, err := ioutil.ReadDir(p)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
return
}
var videos []string
for _, f := range files {
if f.IsDir() {
continue
}
if !strings.HasSuffix(f.Name(), ".mp4") {
continue
}
videos = append(videos, f.Name())
}
c.JSON(http.StatusOK, gin.H{
"videos": videos,
})
}
type newForm struct {
File *multipart.FileHeader `form:"file"`
Openid string `form:"openid"`
}
//UploadfileAndFormHandle 上传带form
func UploadfileAndFormHandler(c *gin.Context) {
var data newForm
if err := c.ShouldBind(&data); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
video := &video.Video{}
filepath, err := video.Upload(data.File)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"filepath": filepath,
"openid": data.Openid,
},
})
}
//UploadHandle 小程序中文件上传,正式环境中使用云存储
func UploadHandler(c *gin.Context) {
header, err := c.FormFile("file")
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
dst := header.Filename
src, err := header.Open()
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer src.Close()
out, err := os.Create(`video/` + dst)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer out.Close()
n, err := io.Copy(out, src)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": n,
})
}