update
This commit is contained in:
@@ -3,102 +3,20 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sort"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 重命名电影的处理函数
|
||||
func PostRename(c *gin.Context) {
|
||||
response := gin.H{
|
||||
"code": http.StatusInternalServerError,
|
||||
"message": "An unexpected error occurred.",
|
||||
}
|
||||
|
||||
// 定义请求结构体
|
||||
type RenameRequest struct {
|
||||
OldName string `json:"old_name" binding:"required"`
|
||||
NewName string `json:"new_name" binding:"required"`
|
||||
}
|
||||
|
||||
var req RenameRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response["code"] = http.StatusBadRequest
|
||||
response["message"] = "Invalid request body: " + err.Error()
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
oldName := req.OldName
|
||||
newName := req.NewName
|
||||
|
||||
if oldName == "" || newName == "" {
|
||||
response["code"] = http.StatusBadRequest
|
||||
response["message"] = "Old name and new name are required."
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
if oldName == newName {
|
||||
response["code"] = http.StatusBadRequest
|
||||
response["message"] = "Old name and new name cannot be the same."
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
MovieDictLock.Lock()
|
||||
defer MovieDictLock.Unlock()
|
||||
|
||||
// 检查新名称是否已存在
|
||||
if _, exists := MovieDict[newName]; exists {
|
||||
response["code"] = http.StatusConflict
|
||||
response["message"] = "New name already exists."
|
||||
c.JSON(http.StatusConflict, response)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找旧名称对应的电影
|
||||
movie, exists := MovieDict[oldName]
|
||||
if exists {
|
||||
// 更新电影信息
|
||||
err := movie.RenameVideo(newName) // 重命名视频文件
|
||||
if err != nil {
|
||||
response["code"] = http.StatusInternalServerError
|
||||
response["message"] = "Failed to rename video file."
|
||||
c.JSON(http.StatusInternalServerError, response)
|
||||
return
|
||||
}
|
||||
|
||||
delete(MovieDict, oldName) // 删除旧名称的条目
|
||||
MovieDict[newName] = movie // 添加新名称的条目
|
||||
|
||||
response["code"] = http.StatusOK
|
||||
response["message"] = "Movie renamed successfully."
|
||||
response["data"] = gin.H{"old_name": oldName, "new_name": newName}
|
||||
c.JSON(http.StatusOK, response)
|
||||
log.Printf("Movie renamed from %s to %s successfully", oldName, newName)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果旧名称不存在,返回404错误
|
||||
response["code"] = http.StatusNotFound
|
||||
response["message"] = "Movie not found."
|
||||
c.JSON(http.StatusNotFound, response)
|
||||
log.Printf("Movie rename failed: %s not found", oldName)
|
||||
}
|
||||
|
||||
// GetCategoryList 获取分类列表
|
||||
func GetCategoryList(c *gin.Context) {
|
||||
response := gin.H{
|
||||
"code": http.StatusInternalServerError,
|
||||
"message": "An unexpected error occurred.",
|
||||
"data": []string{},
|
||||
"code": http.StatusOK,
|
||||
"message": "Success",
|
||||
"data": Categories,
|
||||
}
|
||||
|
||||
response["code"] = http.StatusOK
|
||||
response["message"] = "Success"
|
||||
response["data"] = Categories
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
@@ -227,3 +145,55 @@ func MovieList(c *gin.Context) {
|
||||
response["data"] = responseData
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// PostRename 重命名文件
|
||||
func PostRename(c *gin.Context) {
|
||||
var requestData struct {
|
||||
OldName string `json:"old_name"`
|
||||
NewName string `json:"new_name"`
|
||||
}
|
||||
|
||||
response := gin.H{
|
||||
"code": http.StatusOK,
|
||||
"message": "Success",
|
||||
"data": nil,
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&requestData); err != nil {
|
||||
response["code"] = http.StatusBadRequest
|
||||
response["message"] = "Invalid request data: " + err.Error()
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
if requestData.OldName == "" || requestData.NewName == "" {
|
||||
response["code"] = http.StatusBadRequest
|
||||
response["message"] = "Both old_name and new_name are required"
|
||||
c.JSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
// 查找要重命名的电影
|
||||
movie, exists := MovieDict[requestData.OldName]
|
||||
if !exists {
|
||||
response["code"] = http.StatusNotFound
|
||||
response["message"] = "Movie not found"
|
||||
c.JSON(http.StatusNotFound, response)
|
||||
return
|
||||
}
|
||||
|
||||
// 执行重命名
|
||||
if err := movie.RenameVideo(requestData.NewName); err != nil {
|
||||
response["code"] = http.StatusInternalServerError
|
||||
response["message"] = "Failed to rename video: " + err.Error()
|
||||
c.JSON(http.StatusInternalServerError, response)
|
||||
return
|
||||
}
|
||||
|
||||
// 更新全局字典
|
||||
delete(MovieDict, requestData.OldName)
|
||||
MovieDict[requestData.NewName] = movie
|
||||
|
||||
response["message"] = "Rename successful"
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import (
|
||||
func main() {
|
||||
initMovie()
|
||||
|
||||
// 设置为发布模式
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
eg := gin.Default()
|
||||
eg.Use(Cors())
|
||||
eg.Static("/res", "movie/")
|
||||
@@ -37,4 +40,4 @@ func main() {
|
||||
movie.POST("/rename", PostRename)
|
||||
|
||||
eg.Run("0.0.0.0:4444")
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ var Movies []*Movie // 存储所有电影信息的全局切
|
||||
var MovieDict = make(map[string]*Movie) // 存储需要处理缩略图的视频字典
|
||||
var MovieDictLock sync.Mutex // 保护MovieDict的并发访问
|
||||
|
||||
var IsRemakePNG = true // 是否重新生成所有PNG缩略图
|
||||
var IsRemakePNG = false // 是否重新生成所有PNG缩略图
|
||||
var Categories = []string{ // 分类
|
||||
"15min", "30min", "60min", "大于60min", "最新添加"}
|
||||
|
||||
@@ -331,4 +331,4 @@ func markRecentMovies() {
|
||||
for i := 0; i < len(Movies) && i < 20; i++ {
|
||||
Movies[i].TimeCategory = "最新添加"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user