Compare commits
10 Commits
2c7d0d3b09
...
75fdf7c1d8
| Author | SHA1 | Date |
|---|---|---|
|
|
75fdf7c1d8 | |
|
|
27972c1619 | |
|
|
d33597766a | |
|
|
9e5ca5e897 | |
|
|
5228a4270e | |
|
|
a295d750cc | |
|
|
c943ced9f6 | |
|
|
db726b078e | |
|
|
0b503558ef | |
|
|
743ac6a55f |
16
dao/file.go
16
dao/file.go
|
|
@ -59,3 +59,19 @@ func UpdateFileByID(id, auth_id int, fileStoreName, fileName, fileType, filePath
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteFileByAuthID(auth_id int) bool {
|
||||||
|
res := DB.Debug().Model(&File{}).Where("auth_id = ?", auth_id).Delete(&File{})
|
||||||
|
if res.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteFileById(id int) bool {
|
||||||
|
res := DB.Debug().Model(&File{}).Where("id = ?", id).Delete(&File{})
|
||||||
|
if res.Error != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
"videoplayer/service"
|
"videoplayer/service"
|
||||||
|
|
@ -22,7 +26,36 @@ func SetUpToolGroup(router *gin.Engine) {
|
||||||
|
|
||||||
//文件上传、下载
|
//文件上传、下载
|
||||||
toolGroup.POST("/upload", UploadFile)
|
toolGroup.POST("/upload", UploadFile)
|
||||||
toolGroup.GET("/download/:filename", DownloadFile)
|
toolGroup.GET("/download", DownloadFile)
|
||||||
|
//文件管理
|
||||||
|
toolGroup.POST("/file_del", DelFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DelFile(c *gin.Context) {
|
||||||
|
//先查看是否有权限
|
||||||
|
id, _ := c.Get("id")
|
||||||
|
id1 := int(id.(float64))
|
||||||
|
|
||||||
|
file_id, _ := strconv.Atoi(c.PostForm("id"))
|
||||||
|
|
||||||
|
file_ := dao.FindFileByID(file_id, id1)
|
||||||
|
if file_.ID == 0 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//删除文件
|
||||||
|
err := os.Remove(file_.FilePath + "/" + file_.FileStoreName)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "delete file failed", "code": proto.DeleteFileFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//删除文件信息
|
||||||
|
if res := dao.DeleteFileById(file_id); !res {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "delete file info failed", "code": proto.DeleteFileInfoFailed, "message": "failed"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success"})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadFile(c *gin.Context) {
|
func UploadFile(c *gin.Context) {
|
||||||
|
|
@ -30,7 +63,7 @@ func UploadFile(c *gin.Context) {
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
id1 := int(id.(float64))
|
id1 := int(id.(float64))
|
||||||
//从请求头获取upload_type
|
//从请求头获取upload_type
|
||||||
uploadType := c.GetHeader("upload_type")
|
uploadType := c.PostForm("upload_type")
|
||||||
if uploadType == "" {
|
if uploadType == "" {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
@ -49,7 +82,7 @@ func UploadFile(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//保存文件
|
//保存文件
|
||||||
filePath, fileStoreName, err := service.SaveFile(file, uploadType)
|
filePath, fileStoreName, err := service.SaveFile(c, file, uploadType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "save file failed", "code": proto.SaveFileFailed, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "save file failed", "code": proto.SaveFileFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
|
|
@ -63,22 +96,43 @@ func UploadFile(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileID})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DownloadFile(c *gin.Context) {
|
func DownloadFile(c *gin.Context) {
|
||||||
//参数
|
//参数
|
||||||
filename := c.Param("filename")
|
//filename := c.Param("filename")
|
||||||
//file_id, _ := strconv.Atoi(c.Query("id"))
|
file_id, _ := strconv.Atoi(c.Query("id"))
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
//查询文件信息
|
//查询文件信息
|
||||||
file := dao.FindFileByNames(filename, int(id.(float64)))
|
//file := dao.FindFileByNames(file_id, int(id.(float64)))
|
||||||
if file.ID == 0 {
|
file_ := dao.FindFileByID(file_id, int(id.(float64)))
|
||||||
|
if file_.ID == 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
c.JSON(http.StatusOK, gin.H{"error": "file not found", "code": proto.FileNotFound, "message": "failed"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//下载文件
|
//下载文件
|
||||||
c.File(file.FilePath + file.FileStoreName)
|
// 打开文件
|
||||||
|
file, err := os.Open(file_.FilePath + "/" + file_.FileStoreName)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to open file"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// 设置响应头
|
||||||
|
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file_.FileName))
|
||||||
|
|
||||||
|
// 发送文件内容
|
||||||
|
_, err = io.Copy(c.Writer, file)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error", "message": "Failed to send file"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Status(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetRedis(c *gin.Context) {
|
func SetRedis(c *gin.Context) {
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -50,10 +50,11 @@ func writeLogger(c *gin.Context) {
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
params := ""
|
params := ""
|
||||||
|
|
||||||
if method == "GET" {
|
if method == "GET" {
|
||||||
params = c.Request.URL.RawQuery
|
params = c.Request.URL.RawQuery
|
||||||
}
|
}
|
||||||
if method == "POST" {
|
if method == "POST" && !strings.Contains(c.Request.URL.Path, "/upload") {
|
||||||
params = c.Request.PostForm.Encode()
|
params = c.Request.PostForm.Encode()
|
||||||
if params == "" {
|
if params == "" {
|
||||||
// 请求体
|
// 请求体
|
||||||
|
|
@ -62,6 +63,9 @@ func writeLogger(c *gin.Context) {
|
||||||
params = string(bodyBytes)
|
params = string(bodyBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if strings.Contains(c.Request.URL.Path, "/upload") {
|
||||||
|
params = "upload file"
|
||||||
|
}
|
||||||
go dao.InsertLogToDB(path, ip, method, params)
|
go dao.InsertLogToDB(path, ip, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,10 +51,12 @@ const (
|
||||||
MsgSendFailed = 61 // 消息发送失败
|
MsgSendFailed = 61 // 消息发送失败
|
||||||
|
|
||||||
//文件错误码
|
//文件错误码
|
||||||
FileNotFound = 71 // 文件不存在
|
FileNotFound = 71 // 文件不存在
|
||||||
FileUploadFailed = 72 // 文件上传失败
|
FileUploadFailed = 72 // 文件上传失败
|
||||||
SaveFileInfoFailed = 73 // 保存文件信息失败
|
SaveFileInfoFailed = 73 // 保存文件信息失败
|
||||||
SaveFileFailed = 74 // 保存文件失败
|
SaveFileFailed = 74 // 保存文件失败
|
||||||
UploadFileFailed = 75 // 上传文件失败
|
UploadFileFailed = 75 // 上传文件失败
|
||||||
NoUploadPermissions = 76 // 无上传权限
|
NoUploadPermissions = 76 // 无上传权限
|
||||||
|
DeleteFileFailed = 77 // 删除文件失败
|
||||||
|
DeleteFileInfoFailed = 78 // 删除文件信息失败
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"io"
|
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -26,7 +26,7 @@ func getFilePath(path string) string {
|
||||||
return filePath
|
return filePath
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveFile(file *multipart.FileHeader, uploadType string) (string, string, error) {
|
func SaveFile(c *gin.Context, file *multipart.FileHeader, uploadType string) (string, string, error) {
|
||||||
//获取文件后缀
|
//获取文件后缀
|
||||||
fileSuffix := path.Ext(file.Filename)
|
fileSuffix := path.Ext(file.Filename)
|
||||||
//生成文件名
|
//生成文件名
|
||||||
|
|
@ -35,23 +35,10 @@ func SaveFile(file *multipart.FileHeader, uploadType string) (string, string, er
|
||||||
path_ := getFilePath(proto.FILE_BASE_DIR)
|
path_ := getFilePath(proto.FILE_BASE_DIR)
|
||||||
filePath := path_ + "/" + fileStoreName
|
filePath := path_ + "/" + fileStoreName
|
||||||
|
|
||||||
//保存文件file
|
//保存文件
|
||||||
dst, err := os.Create(filePath)
|
if err := c.SaveUploadedFile(file, filePath); err != nil {
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
// 复制文件内容
|
|
||||||
file_, err := file.Open()
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
_, err = io.Copy(dst, file_)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if uploadType == "2" {
|
if uploadType == "2" {
|
||||||
worker.PushRedisList("video_need_handle", filePath)
|
worker.PushRedisList("video_need_handle", filePath)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue