-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: some pkg * feat: oss * fix: cros * faet: new api
- Loading branch information
Showing
14 changed files
with
188 additions
and
101 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.