Skip to content

Commit

Permalink
faet: new api
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky committed Aug 15, 2024
1 parent 841a314 commit a8160a6
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 95 deletions.
54 changes: 0 additions & 54 deletions common/db/completed.go

This file was deleted.

55 changes: 55 additions & 0 deletions common/db/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package db

import (
"context"

"github.com/TensoRaws/FinalRip/module/db"
"go.mongodb.org/mongo-driver/bson"
)

// CheckTaskExist checks if a task exists in the database
func CheckTaskExist(videoKey string) bool {
coll := db.DB.Collection(TASK_COLLECTION)
count, _ := coll.CountDocuments(context.TODO(), Task{Key: videoKey})
return count > 0
}

// CheckTaskStart checks if a task has started
func CheckTaskStart(videoKey string) bool {
task, err := GetTask(videoKey)
if err != nil {
return false
}
return task.EncodeParam != ""
}

// InsertTask inserts a new uncompleted task into the database
func InsertTask(videoKey string) error {
coll := db.DB.Collection(TASK_COLLECTION)
_, err := coll.InsertOne(context.TODO(), Task{
Key: videoKey,
})
return err
}

// UpdateTask updates an uncompleted task in the database
func UpdateTask(filter Task, update Task) error {
coll := db.DB.Collection(TASK_COLLECTION)

up := bson.D{{"$set", update}} //nolint:govet

_, err := coll.UpdateOne(context.TODO(), filter, up)
if err != nil {
return err
}
return nil
}

// GetTask gets a completed Task from the database
func GetTask(videoKey string) (Task, error) {
coll := db.DB.Collection(TASK_COLLECTION)
var task Task
err := coll.FindOne(context.TODO(), Task{Key: videoKey}).Decode(&task)

return task, err
}
6 changes: 3 additions & 3 deletions common/db/type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package db

const (
VIDEO_COLLECTION = "video"
COMPLETED_COLLECTION = "completed"
VIDEO_COLLECTION = "video"
TASK_COLLECTION = "task"
)

type VideoClipInfo struct {
Expand All @@ -13,7 +13,7 @@ type VideoClipInfo struct {
EncodeKey string `bson:"encode_key,omitempty"`
}

type CompletedTask struct {
type Task struct {
Key string `bson:"key,omitempty"`
EncodeKey string `bson:"encode_key,omitempty"`
EncodeParam string `bson:"encode_param,omitempty"`
Expand Down
30 changes: 0 additions & 30 deletions common/db/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ func InsertVideo(info VideoClipInfo) error {
return err
}

// InsertManyVideo 批量插入视频信息
func InsertManyVideo(infos []VideoClipInfo) error {
coll := db.DB.Collection(VIDEO_COLLECTION)
_, err := coll.InsertMany(context.TODO(), []interface{}{
infos,
})
return err
}

// GetVideoClips 获取所有视频切片信息,按照索引排序
func GetVideoClips(videoKey string) ([]VideoClipInfo, error) {
coll := db.DB.Collection(VIDEO_COLLECTION)
Expand Down Expand Up @@ -70,27 +61,6 @@ func UpdateVideo(filter VideoClipInfo, update VideoClipInfo) error {
return nil
}

// UpdateVideoEncodeClip 更新 Encode 后视频切片信息
func UpdateVideoEncodeClip(videoKey string, clipKey string, encodeKey string) error {
coll := db.DB.Collection(VIDEO_COLLECTION)

filter := VideoClipInfo{
Key: videoKey,
ClipKey: clipKey,
}

update := bson.D{{"$set", VideoClipInfo{ //nolint: govet
EncodeKey: encodeKey,
}}}

_, err := coll.UpdateOne(context.TODO(), filter, update)
if err != nil {
return err
}

return nil
}

// GetVideoProgress 获取视频处理进度和每个切片的状态
func GetVideoProgress(videoKey string) ([]bool, error) {
infos, err := GetVideoClips(videoKey)
Expand Down
1 change: 1 addition & 0 deletions server/internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewAPI() *gin.Engine {
{
processGroup := api.Group("task/")
{
processGroup.POST("new", task.New)
processGroup.POST("start", task.Start)
processGroup.GET("progress", task.Progress)
processGroup.GET("oss/presigned", task.OSSPresigned)
Expand Down
37 changes: 37 additions & 0 deletions server/internal/service/task/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package task

import (
"github.com/TensoRaws/FinalRip/common/db"
"github.com/TensoRaws/FinalRip/module/log"
"github.com/TensoRaws/FinalRip/module/resp"
"github.com/gin-gonic/gin"
)

type NewRequest struct {
VideoKey string `form:"video_key" binding:"required"`
}

// New 创建任务 (POST /new)
func New(c *gin.Context) {
// 绑定参数
var req NewRequest
if err := c.ShouldBind(&req); err != nil {
resp.AbortWithMsg(c, err.Error())
return
}

// 检查视频是否存在
if db.CheckTaskExist(req.VideoKey) {
resp.AbortWithMsg(c, "Task already exists, please wait for it to complete or delete it.")
return
}

err := db.InsertTask(req.VideoKey)
if err != nil {
log.Logger.Error("Failed to insert task: " + err.Error())
resp.AbortWithMsg(c, err.Error())
return
}

resp.OK(c)
}
2 changes: 1 addition & 1 deletion server/internal/service/task/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Progress(c *gin.Context) {
return
}

task, err := db.GetCompletedTask(req.VideoKey)
task, err := db.GetTask(req.VideoKey)
if err != nil {
log.Logger.Errorf("db.GetCompletedEncodeKey failed, err: %v", err)
resp.AbortWithMsg(c, err.Error())
Expand Down
20 changes: 15 additions & 5 deletions server/internal/service/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,25 @@ func Start(c *gin.Context) {
return
}

// 检查视频是否存在
if db.CheckTaskExist(req.VideoKey) {
resp.AbortWithMsg(c, "Task already exists, please wait for it to complete or delete it.")
// 检查任务是否 new 上传
if !db.CheckTaskExist(req.VideoKey) {
resp.AbortWithMsg(c, "Task not found, please upload video first.")
return
}

err := db.InsertUncompletedTask(req.VideoKey, req.EncodeParam, req.Script)
// 检查任务是否已经开始
if db.CheckTaskStart(req.VideoKey) {
resp.AbortWithMsg(c, "Task already started.")
return
}

// 更新任务
err := db.UpdateTask(db.Task{Key: req.VideoKey}, db.Task{
EncodeParam: req.EncodeParam,
Script: req.Script,
})
if err != nil {
log.Logger.Error("Failed to insert uncompleted task: " + err.Error())
log.Logger.Error("Failed to update task: " + err.Error())
resp.AbortWithMsg(c, err.Error())
return
}
Expand Down
5 changes: 4 additions & 1 deletion worker/internal/encode/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ func Handler(ctx context.Context, t *asynq.Task) error {
return err
}

err = db.UpdateVideoEncodeClip(p.Clip.Key, p.Clip.ClipKey, key)
err = db.UpdateVideo(db.VideoClipInfo{Key: p.Clip.Key}, db.VideoClipInfo{
ClipKey: p.Clip.ClipKey,
EncodeKey: key,
})
if err != nil {
log.Logger.Errorf("Failed to upload encode video %s: %s", key, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/merge/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Handler(ctx context.Context, t *asynq.Task) error {
}

// 保存合并后的视频信息
err = db.UpdateUncompletedTask(p.Clips[0].Key, mergedKey)
err = db.UpdateTask(db.Task{Key: p.Clips[0].Key}, db.Task{EncodeKey: mergedKey})
if err != nil {
log.Logger.Errorf("Failed to update completed task: %v", err)
return err
Expand Down

0 comments on commit a8160a6

Please sign in to comment.