feat: support player
This commit is contained in:
@@ -13,9 +13,9 @@ import (
|
||||
|
||||
"github.com/go-pay/gopay/wechat/v3"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ListQuery struct {
|
||||
@@ -95,8 +95,10 @@ type PostItem struct {
|
||||
// @Router /api/posts/:id [get]
|
||||
// @Bind id path
|
||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
log.Infof("Fetching post with ID: %d", id)
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -126,6 +128,43 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
type PlayUrl struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// Play
|
||||
// @Router /api/posts/:id/play [get]
|
||||
// @Bind id path
|
||||
// @Bind user local
|
||||
func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, error) {
|
||||
log.Infof("Fetching play URL for post ID: %d", id)
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
preview := false
|
||||
bought, err := models.Users.HasBought(ctx.Context(), user.ID, post.ID)
|
||||
if !bought || err != nil {
|
||||
preview = true
|
||||
}
|
||||
|
||||
for _, asset := range post.Assets.Data {
|
||||
if asset.Type == "video/mp4" && asset.Metas.Short == preview {
|
||||
media, err := models.Medias.GetByID(ctx.Context(), asset.Media)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url, err := ctl.oss.GetSignedUrl(ctx.Context(), media.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PlayUrl{Url: url}, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("视频不存在")
|
||||
}
|
||||
|
||||
// Mine posts
|
||||
// @Router /api/posts/mine [get]
|
||||
// @Bind pagination query
|
||||
|
||||
@@ -63,6 +63,12 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
PathParam[int64]("id"),
|
||||
))
|
||||
|
||||
router.Get("/api/posts/:id/play", DataFunc2(
|
||||
r.posts.Play,
|
||||
PathParam[int64]("id"),
|
||||
Local[*model.Users]("user"),
|
||||
))
|
||||
|
||||
router.Get("/api/posts/mine", DataFunc2(
|
||||
r.posts.Mine,
|
||||
Query[requests.Pagination]("pagination"),
|
||||
|
||||
@@ -283,3 +283,25 @@ func (m *usersModel) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int
|
||||
return item.ID, item
|
||||
}), nil
|
||||
}
|
||||
|
||||
// HasBought
|
||||
func (m *usersModel) HasBought(ctx context.Context, userID, postID int64) (bool, error) {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.ID).
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userID)).AND(
|
||||
tbl.PostID.EQ(Int64(postID)),
|
||||
),
|
||||
)
|
||||
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var userPost model.UserPosts
|
||||
if err := stmt.QueryContext(ctx, db, &userPost); err != nil {
|
||||
m.log.Errorf("error querying user post: %v", err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return userPost.ID > 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user