2023-07-04 23:47:01 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2025-06-02 06:20:16 +08:00
|
|
|
|
"strings"
|
2025-08-14 00:28:29 +08:00
|
|
|
|
"sort"
|
2023-07-04 23:47:01 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-14 00:28:29 +08:00
|
|
|
|
// GetCategoryList 获取分类列表
|
2025-06-02 21:29:31 +08:00
|
|
|
|
func GetCategoryList(c *gin.Context) {
|
|
|
|
|
response := gin.H{
|
2025-08-14 00:28:29 +08:00
|
|
|
|
"code": http.StatusOK,
|
|
|
|
|
"message": "Success",
|
|
|
|
|
"data": Categories,
|
2025-06-02 21:29:31 +08:00
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 23:47:01 +08:00
|
|
|
|
// MovieList 电影列表
|
|
|
|
|
func MovieList(c *gin.Context) {
|
|
|
|
|
response := gin.H{
|
|
|
|
|
"code": http.StatusInternalServerError,
|
2025-06-02 06:20:16 +08:00
|
|
|
|
"message": "An unexpected error occurred.",
|
2025-06-02 03:19:36 +08:00
|
|
|
|
"data": gin.H{"items": []Movie{}, "total": 0},
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 21:29:31 +08:00
|
|
|
|
var moviesToPaginate []*Movie
|
2025-06-02 06:20:16 +08:00
|
|
|
|
categoryName := c.Query("category")
|
|
|
|
|
searchQuery := c.Query("search") // 获取搜索参数
|
2025-06-02 03:19:36 +08:00
|
|
|
|
|
2025-06-02 06:20:16 +08:00
|
|
|
|
// === 第一步:基础过滤(类别)===
|
|
|
|
|
if categoryName != "" && categoryName != "最新添加" {
|
|
|
|
|
// 普通类别:直接过滤
|
2025-06-02 21:29:31 +08:00
|
|
|
|
for _, m := range Movies {
|
2025-06-02 03:19:36 +08:00
|
|
|
|
if m.TimeCategory == categoryName {
|
|
|
|
|
moviesToPaginate = append(moviesToPaginate, m)
|
|
|
|
|
}
|
2023-07-08 17:44:51 +08:00
|
|
|
|
}
|
2025-06-02 03:19:36 +08:00
|
|
|
|
} else {
|
2025-06-02 06:20:16 +08:00
|
|
|
|
// 最新添加/所有电影:使用全局movies
|
2025-06-02 21:29:31 +08:00
|
|
|
|
moviesToPaginate = make([]*Movie, len(Movies))
|
|
|
|
|
copy(moviesToPaginate, Movies)
|
2025-06-02 06:20:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// === 第二步:搜索过滤 ===
|
|
|
|
|
if searchQuery != "" {
|
2025-06-02 21:29:31 +08:00
|
|
|
|
var searchResults []*Movie
|
2025-06-02 06:20:16 +08:00
|
|
|
|
lowerSearch := strings.ToLower(searchQuery)
|
|
|
|
|
|
|
|
|
|
for _, m := range moviesToPaginate {
|
|
|
|
|
// 检查文件名是否包含搜索词(不区分大小写)
|
|
|
|
|
if strings.Contains(strings.ToLower(m.FileName), lowerSearch) {
|
|
|
|
|
searchResults = append(searchResults, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
moviesToPaginate = searchResults
|
2023-07-08 17:44:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 06:20:16 +08:00
|
|
|
|
// === 第三步:排序处理 ===
|
|
|
|
|
sortBy := c.Query("sort")
|
|
|
|
|
order := c.Query("order")
|
|
|
|
|
if sortBy == "" {
|
2025-06-03 00:44:35 +08:00
|
|
|
|
sortBy = "duration" // 默认按创建时间排序
|
2025-06-02 06:20:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 应用排序
|
|
|
|
|
sort.Slice(moviesToPaginate, func(i, j int) bool {
|
|
|
|
|
switch sortBy {
|
|
|
|
|
case "created_time":
|
|
|
|
|
if order == "desc" {
|
|
|
|
|
return moviesToPaginate[i].CreatedTime > moviesToPaginate[j].CreatedTime
|
|
|
|
|
}
|
|
|
|
|
return moviesToPaginate[i].CreatedTime < moviesToPaginate[j].CreatedTime
|
|
|
|
|
case "duration":
|
|
|
|
|
if order == "desc" {
|
|
|
|
|
return moviesToPaginate[i].Duration > moviesToPaginate[j].Duration
|
|
|
|
|
}
|
|
|
|
|
return moviesToPaginate[i].Duration < moviesToPaginate[j].Duration
|
|
|
|
|
default:
|
|
|
|
|
// 默认按创建时间降序
|
|
|
|
|
return moviesToPaginate[i].CreatedTime > moviesToPaginate[j].CreatedTime
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// === 第四步:分页处理 ===
|
|
|
|
|
page := 0
|
2023-07-04 23:47:01 +08:00
|
|
|
|
spage := c.Query("page")
|
|
|
|
|
if spage != "" {
|
|
|
|
|
p, err := strconv.Atoi(spage)
|
2025-06-02 06:20:16 +08:00
|
|
|
|
if err != nil || p < 1 {
|
2025-06-02 03:19:36 +08:00
|
|
|
|
log.Println("Invalid page number:", spage, err)
|
|
|
|
|
response["code"] = http.StatusBadRequest
|
|
|
|
|
response["message"] = "Invalid page number. Must be a positive integer."
|
2025-06-02 06:20:16 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, response)
|
2023-07-08 17:44:51 +08:00
|
|
|
|
return
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
2025-06-02 06:20:16 +08:00
|
|
|
|
page = p - 1
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 06:20:16 +08:00
|
|
|
|
limit := 12
|
2023-07-04 23:47:01 +08:00
|
|
|
|
slimit := c.Query("limit")
|
|
|
|
|
if slimit != "" {
|
|
|
|
|
l, err := strconv.Atoi(slimit)
|
2025-06-02 06:20:16 +08:00
|
|
|
|
if err != nil || l < 1 {
|
2025-06-02 03:19:36 +08:00
|
|
|
|
log.Println("Invalid limit number:", slimit, err)
|
|
|
|
|
response["code"] = http.StatusBadRequest
|
|
|
|
|
response["message"] = "Invalid limit number. Must be a positive integer."
|
2025-06-02 06:20:16 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, response)
|
2023-07-08 17:44:51 +08:00
|
|
|
|
return
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
|
|
|
|
limit = l
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start := page * limit
|
2025-06-02 03:19:36 +08:00
|
|
|
|
end := start + limit
|
2025-06-02 06:20:16 +08:00
|
|
|
|
total := len(moviesToPaginate)
|
2025-06-02 03:19:36 +08:00
|
|
|
|
|
2025-06-02 06:20:16 +08:00
|
|
|
|
// 处理超出范围的情况
|
|
|
|
|
if start >= total {
|
2025-06-02 03:19:36 +08:00
|
|
|
|
response["data"] = gin.H{
|
|
|
|
|
"items": []Movie{},
|
2025-06-02 06:20:16 +08:00
|
|
|
|
"total": total,
|
2025-06-02 03:19:36 +08:00
|
|
|
|
}
|
|
|
|
|
response["code"] = http.StatusOK
|
|
|
|
|
response["message"] = "Success"
|
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-08 17:44:51 +08:00
|
|
|
|
|
2025-06-02 06:20:16 +08:00
|
|
|
|
if end > total {
|
|
|
|
|
end = total
|
2023-07-08 17:44:51 +08:00
|
|
|
|
}
|
2025-06-02 03:19:36 +08:00
|
|
|
|
|
|
|
|
|
responseData := gin.H{
|
|
|
|
|
"items": moviesToPaginate[start:end],
|
2025-06-02 06:20:16 +08:00
|
|
|
|
"total": total,
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response["code"] = http.StatusOK
|
2025-06-02 03:19:36 +08:00
|
|
|
|
response["message"] = "Success"
|
|
|
|
|
response["data"] = responseData
|
|
|
|
|
c.JSON(http.StatusOK, response)
|
2023-07-04 23:47:01 +08:00
|
|
|
|
}
|
2025-08-14 00:28:29 +08:00
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|