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