Skip to content

Commit

Permalink
feat: new api (#16)
Browse files Browse the repository at this point in the history
* refactor: some pkg

* feat: oss

* fix: cros

* faet: new api
  • Loading branch information
Tohrusky authored Aug 15, 2024
1 parent 3826430 commit 683328a
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 101 deletions.
47 changes: 0 additions & 47 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
4 changes: 2 additions & 2 deletions conf/finalrip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ redis:
oss:
type: minio # minio, cos
endpoint: 127.0.0.1:9000
accessKey: ChYm7ufIwNAOzq6PQPCA
secretKey: udicP52IwRbmo2hf6lFvjUS7NP5BhlAdsGNIuDE5
accessKey: homo
secretKey: homo114514
region: local
bucket: finalrip
ssl: false
Expand Down
11 changes: 11 additions & 0 deletions module/oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ func GetPresignedURL(key string, fileName string, expiration time.Duration) (str
}
return presignedURL.String(), nil
}

// GetUploadPresignedURL gets the presigned URL for the file upload, use PUT method in frontend.
func GetUploadPresignedURL(key string, expiration time.Duration) (string, error) {
// Generate presigned put object url
presignedURL, err := oss.PresignedPutObject(context.Background(), config.OSSConfig.Bucket, key, expiration)
if err != nil {
log.Logger.Error("Failed to generate presigned URL: " + key + err.Error())
return "", err
}
return presignedURL.String(), nil
}
11 changes: 8 additions & 3 deletions server/internal/middleware/cros/cros.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cros

import (
"net/http"

"github.com/gin-gonic/gin"
)

// Cors 设置跨域
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.GetHeader("origin")

c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers",
"Content-Type, AccessToken, X-CSRF-Token, Authorization, Token, X-Token, X-User-Id")
c.Header("Access-Control-Allow-Methods", "POST, GET")
Expand All @@ -18,6 +18,11 @@ func Cors() gin.HandlerFunc {
"Access-Control-Allow-Headers, Content-Type, New-Token, New-Expires-At")
c.Header("Access-Control-Allow-Credentials", "true")

if c.Request.Method == "OPTIONS" {
c.JSON(http.StatusOK, "Options Request!")
return
}

c.Next()
}
}
16 changes: 7 additions & 9 deletions server/internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"github.com/TensoRaws/FinalRip/server/internal/middleware/auth"
"github.com/TensoRaws/FinalRip/server/internal/middleware/cros"
"github.com/TensoRaws/FinalRip/server/internal/middleware/logger"
"github.com/TensoRaws/FinalRip/server/internal/service/process"
"github.com/TensoRaws/FinalRip/server/internal/service/task"
"github.com/gin-gonic/gin"
)

func NewAPI() *gin.Engine {
r := gin.New()
r.Use(cros.Cors()) // 跨域中间件
r.Use(logger.DefaultLogger(), gin.Recovery()) // 日志中间件
r.Use(auth.RequireAuth()) // 鉴权中间件
r.Use(cros.Cors(), logger.DefaultLogger(), gin.Recovery(), auth.RequireAuth())

r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
Expand All @@ -24,12 +22,12 @@ func NewAPI() *gin.Engine {

api := r.Group("/api/v1/")
{
processGroup := api.Group("process/")
processGroup := api.Group("task/")
{
// 开始压制
processGroup.POST("start", process.Start)
// 查看进度
processGroup.GET("progress", process.Progress)
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)
}
39 changes: 39 additions & 0 deletions server/internal/service/task/oss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package task

import (
"time"

"github.com/TensoRaws/FinalRip/module/log"
"github.com/TensoRaws/FinalRip/module/oss"
"github.com/TensoRaws/FinalRip/module/resp"
"github.com/gin-gonic/gin"
)

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

type OSSPresignedResponse struct {
URL string `json:"url"`
}

// OSSPresigned 获取 OSS 上传 URL (GET /oss/presigned)
func OSSPresigned(c *gin.Context) {
// 绑定参数
var req OSSPresignedRequest
if err := c.ShouldBindQuery(&req); err != nil {
resp.AbortWithMsg(c, err.Error())
return
}

url, err := oss.GetUploadPresignedURL(req.VideoKey, 48*time.Hour)
if err != nil {
log.Logger.Error("get upload presigned url failed: " + err.Error())
resp.AbortWithMsg(c, "get upload presigned url failed: "+err.Error())
return
}

resp.OKWithData(c, &OSSPresignedResponse{
URL: url,
})
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package process
package task

import (
"time"
Expand Down 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package process
package task

import (
"errors"
Expand Down Expand Up @@ -31,9 +31,25 @@ func Start(c *gin.Context) {
return
}

err := db.InsertUncompletedTask(req.VideoKey, req.EncodeParam, req.Script)
// 检查任务是否 new 上传
if !db.CheckTaskExist(req.VideoKey) {
resp.AbortWithMsg(c, "Task not found, please upload video first.")
return
}

// 检查任务是否已经开始
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
Loading

0 comments on commit 683328a

Please sign in to comment.