You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.1 KiB
66 lines
1.1 KiB
package video
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golangkit/filetag"
|
|
"os"
|
|
"path/filepath"
|
|
"recook/internal/back"
|
|
"recook/internal/static_path"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
MaxVideoFileMemory = 10 << 20 // 10MB
|
|
)
|
|
|
|
type CategoryParams struct {
|
|
Category string `json:"category"`
|
|
}
|
|
|
|
func UploadVideo(c *gin.Context) {
|
|
f, err := c.FormFile("video")
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
if f.Size > MaxVideoFileMemory {
|
|
back.Err(c, "视频太大")
|
|
return
|
|
}
|
|
|
|
name, err := filetag.Multipart(f)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
elements := strings.Split(f.Filename, ".")
|
|
ext := elements[len(elements)-1:][0]
|
|
ext = strings.ToLower(ext)
|
|
|
|
path := filepath.Join(static_path.Dir.Video, name+"."+ext)
|
|
|
|
if PathExists(path) == false {
|
|
uri := filepath.Join(static_path.Dir.Root, path)
|
|
err = c.SaveUploadedFile(f, uri)
|
|
if err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
back.Suc(c, "上传成功", &gin.H{
|
|
"url": path,
|
|
})
|
|
}
|
|
|
|
func PathExists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
if err == nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|