Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: torrent list api #37

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/router/api/v1/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TorrentRouterGroup(api *gin.RouterGroup) {
torrent := api.Group("torrent/")

// 上传种子
// 种子上传
torrent.POST("upload",
jwt.RequireAuth(false),
rbac.RABC(role.ADMIN, role.UPLOADER, role.ADVANCED_USER),
Expand All @@ -38,8 +38,13 @@ func TorrentRouterGroup(api *gin.RouterGroup) {
// 种子首页官种
torrent.GET("official",
jwt.RequireAuth(false),
middleware_cache.Response(6*time.Hour),
middleware_cache.Response(1*time.Hour),
torrent_service.Official)
// 种子列表
torrent.GET("list",
jwt.RequireAuth(false),
middleware_cache.Response(10*time.Second),
torrent_service.List)
// 种子详情
torrent.GET("detail",
jwt.RequireAuth(false),
Expand Down
42 changes: 21 additions & 21 deletions internal/service/torrent/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,36 @@ func GetTorrentOSSUrl(hash string, title string) (string, error) {
}

// GetTorrentInfo 获取种子信息
func GetTorrentInfo(t *model.Torrent) (*Info, error) {
magnet := torrent.GetMagnet(t.Hash, torrent.TRACKER_LIST)
func GetTorrentInfo(bt *model.Torrent) (*Info, error) {
magnet := torrent.GetMagnet(bt.Hash, torrent.TRACKER_LIST)

size := util.ByteCountBinary(uint64(t.Size))
size := util.ByteCountBinary(uint64(bt.Size))

urlString, err := GetTorrentOSSUrl(t.Hash, t.Title)
urlString, err := GetTorrentOSSUrl(bt.Hash, bt.Title)
if err != nil {
return nil, err
}

return &Info{
AnidbID: t.AnidbID,
AudioCodec: t.AudioCodec,
CreatedAt: t.CreatedAt.Format("2006-01-02 15:04:05"),
UpdateAt: t.UpdatedAt.Format("2006-01-02 15:04:05"),
Description: &t.Description,
Essay: &t.Essay,
Genre: t.Genre,
Img: t.Img,
Language: t.Language,
AnidbID: bt.AnidbID,
AudioCodec: bt.AudioCodec,
CreatedAt: bt.CreatedAt.Format("2006-01-02 15:04:05"),
UpdateAt: bt.UpdatedAt.Format("2006-01-02 15:04:05"),
Description: &bt.Description,
Essay: &bt.Essay,
Genre: bt.Genre,
Img: bt.Img,
Language: bt.Language,
Magnet: magnet,
Official: t.Official,
Resolution: t.Resolution,
Official: bt.Official,
Resolution: bt.Resolution,
Size: size,
Status: t.Status,
Subtitle: t.Subtitle,
Title: t.Title,
TorrentID: t.TorrentID,
UploaderID: t.UploaderID,
Status: bt.Status,
Subtitle: bt.Subtitle,
Title: bt.Title,
TorrentID: bt.TorrentID,
UploaderID: bt.UploaderID,
URL: &urlString,
VideoCodec: t.VideoCodec,
VideoCodec: bt.VideoCodec,
}, nil
}
79 changes: 79 additions & 0 deletions internal/service/torrent/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package torrent

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/db"
"github.com/TensoRaws/NuxBT-Backend/module/code"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/resp"
"github.com/TensoRaws/NuxBT-Backend/module/torrent"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
)

type ListRequest struct {
Order string `form:"order" binding:"required,oneof=asc desc"`
OrderBy string `form:"order_by" binding:"required,oneof=date size"`
Page int `form:"page" binding:"required,min=1"`
PerPage int `form:"per_page" binding:"required,min=10"`
Search *string `form:"search" binding:"omitempty"`
Zone string `form:"zone" binding:"required,oneof=official general pending"`
}

type ListResponse struct {
Torrents []Info `json:"torrents"`
TotalPage int `json:"total_page"`
}

// List 获取种子文件列表 (GET /list)
func List(c *gin.Context) {
// 绑定参数
var req ListRequest
if err := c.ShouldBindQuery(&req); err != nil {
resp.AbortWithMsg(c, code.RequestErrorInvalidParams, err.Error())
return
}

search := ""
if req.Search != nil {
search = *req.Search
}

// 获取种子列表
bts, totalPage, err := db.GetTorrentList(
db.TorrentZone(req.Zone), db.OrderByType(req.OrderBy), db.OrderType(req.Order),
req.Page, req.PerPage, search)
if err != nil {
resp.AbortWithMsg(c, code.DatabaseErrorRecordNotFound, err.Error())
log.Logger.Error("failed to get torrent list" + err.Error())
return
}

torrentsInfo := make([]Info, 0, len(bts))
for _, bt := range bts {
torrentsInfo = append(torrentsInfo, Info{
AnidbID: bt.AnidbID,
AudioCodec: bt.AudioCodec,
CreatedAt: bt.CreatedAt.Format("2006-01-02 15:04:05"),
UpdateAt: bt.UpdatedAt.Format("2006-01-02 15:04:05"),
Genre: bt.Genre,
Img: bt.Img,
Language: bt.Language,
Magnet: torrent.GetMagnet(bt.Hash, torrent.TRACKER_LIST),
Official: bt.Official,
Resolution: bt.Resolution,
Size: util.ByteCountBinary(uint64(bt.Size)),
Status: bt.Status,
Subtitle: bt.Subtitle,
Title: bt.Title,
TorrentID: bt.TorrentID,
UploaderID: bt.UploaderID,
VideoCodec: bt.VideoCodec,
})
}

resp.OKWithData(c, &ListResponse{
Torrents: torrentsInfo,
TotalPage: totalPage,
})
log.Logger.Info("get torrent list successfully")
}
3 changes: 2 additions & 1 deletion internal/service/torrent/official.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type OfficialResponse struct {
TotalPage int `json:"total_page"`
}

// Official 获取种子文件列表 (GET /official)
// Official 获取官方种子文件列表 (GET /official)
func Official(c *gin.Context) {
// 绑定参数
var req OfficialRequest
Expand Down Expand Up @@ -64,4 +64,5 @@ func Official(c *gin.Context) {
Torrents: torrentsInfo,
TotalPage: totalPage,
})
log.Logger.Info("get official torrent list successfully")
}
Loading