Skip to content

Commit

Permalink
workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
lilang authored and jojoliang committed May 19, 2022
1 parent 2b70cbc commit 3d28a02
Show file tree
Hide file tree
Showing 2 changed files with 363 additions and 4 deletions.
150 changes: 146 additions & 4 deletions ci_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ type Template struct {
Tag string `xml:"Code,omitempty"`
Name string `xml:"Name,omitempty"`
TransTpl *Transcode `xml:"TransTpl,omitempty"`
CreationTime string `xml:"CreationTime,omitempty"`
CreateTime string `xml:"CreateTime,omitempty"`
UpdateTime string `xml:"UpdateTime,omitempty"`
BucketId string `xml:"BucketId,omitempty"`
Category string `xml:"Category,omitempty"`
Expand Down Expand Up @@ -1820,7 +1820,149 @@ func (s *CIService) UpdateMediaWatermarkTemplate(ctx context.Context, opt *Creat

// MediaWorkflow TODO
type MediaWorkflow struct {
Name string `xml:"Name,omitempty"`
State string `xml:"State,omitempty"`
Topology *Topology `xml:"Topology,omitempty"`
Name string `xml:"Name,omitempty"`
WorkflowId string `xml:"WorkflowId,omitempty"`
State string `xml:"State,omitempty"`
Topology *Topology `xml:"Topology,omitempty"`
CreateTime string `xml:"CreateTime,omitempty"`
UpdateTime string `xml:"UpdateTime,omitempty"`
BucketId string `xml:"BucketId,omitempty"`
}

// MarshalXML TODO
func (m *CreateMediaWorkflowOptions) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
fmt.Println(123)
return nil
}

// UnmarshalXML TODO
func (m *CreateMediaWorkflowOptions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v struct {
XMLName xml.Name //`xml:"Topology"`
Dependencies struct {
Inner []byte `xml:",innerxml"`
} `xml:"Dependencies"`
Nodes struct {
Inner []byte `xml:",innerxml"`
} `xml:"Nodes"`
}
err := d.DecodeElement(&v, &start)
if err != nil {
return err
}

myMap := make(map[string]interface{})

// ... do the mxj magic here ... -

temp := v.Nodes.Inner

prefix := "<Nodes>"
postfix := "</Nodes>"
str := prefix + string(temp) + postfix
fmt.Println(str)
fmt.Println(123)
myMxjMap, _ := mxj.NewMapXml([]byte(str))
myMap, _ = myMxjMap["Nodes"].(map[string]interface{})
nodesMap := make(map[string]Node)

for k, v := range myMap {
var node Node
mapstructure.Decode(v, &node)
nodesMap[k] = node
}

// fill myMap
m.MediaWorkflow.Topology.Nodes = nodesMap

deps := make(map[string]interface{})

tep := "<Dependencies>" + string(v.Dependencies.Inner) + "</Dependencies>"
tepMxjMap, _ := mxj.NewMapXml([]byte(tep))
deps, _ = tepMxjMap["Dependencies"].(map[string]interface{})
depsString := make(map[string]string)
for k, v := range deps {
depsString[k] = v.(string)
}
m.MediaWorkflow.Topology.Dependencies = depsString
return nil
}

// CreateMediaWorkflowOptions TODO
type CreateMediaWorkflowOptions struct {
XMLName xml.Name `xml:"Request"`
MediaWorkflow *MediaWorkflow `xml:"MediaWorkflow,omitempty"`
}

// CreateMediaWorkflowResult TODO
type CreateMediaWorkflowResult struct {
XMLName xml.Name `xml:"Response"`
RequestId string `xml:"RequestId,omitempty"`
MediaWorkflow *MediaWorkflow `xml:"MediaWorkflow,omitempty"`
}

// CreateMediaWorkflow 创建工作流
func (s *CIService) CreateMediaWorkflow(ctx context.Context, opt *CreateMediaWorkflowOptions) (*CreateMediaWorkflowResult, *Response, error) {
var res CreateMediaWorkflowResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/workflow",
method: http.MethodPost,
body: opt,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

// DescribeMediaWorkflowOptions TODO
type DescribeMediaWorkflowOptions struct {
Ids string `url:"ids,omitempty"`
Name string `url:"name,omitempty"`
PageNumber int `url:"pageNumber,omitempty"`
PageSize int `url:"pageSize,omitempty"`
}

// DescribeMediaWorkflowResult TODO
type DescribeMediaWorkflowResult struct {
XMLName xml.Name `xml:"Response"`
MediaWorkflowList []MediaWorkflow `xml:"MediaWorkflowList,omitempty"`
RequestId string `xml:"RequestId,omitempty"`
TotalCount int `xml:"TotalCount,omitempty"`
PageNumber int `xml:"PageNumber,omitempty"`
PageSize int `xml:"PageSize,omitempty"`
NonExistIDs []string `xml:"NonExistIDs,omitempty"`
}

// DescribeMediaWorkflow 搜索工作流
func (s *CIService) DescribeMediaWorkflow(ctx context.Context, opt *DescribeMediaWorkflowOptions) (*DescribeMediaWorkflowResult, *Response, error) {
var res DescribeMediaWorkflowResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/Workflow",
optQuery: opt,
method: http.MethodGet,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

// DeleteMediaWorkflowResult TODO
type DeleteMediaWorkflowResult struct {
RequestId string `xml:"RequestId,omitempty"`
WorkflowId string `xml:"WorkflowId,omitempty"`
}

// DeleteMediaWorkflow TODO
func (s *CIService) DeleteMediaWorkflow(ctx context.Context, workflowId string) (*DeleteMediaWorkflowResult, *Response, error) {
var res DeleteMediaWorkflowResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/Workflow/" + workflowId,
method: http.MethodDelete,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}
217 changes: 217 additions & 0 deletions example/CI/media_process/media_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package main

import (
"context"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

// DescribeWorkflow 查询工作流
func DescribeWorkflow() {
u, _ := url.Parse("https://lilang-1253960454.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://lilang-1253960454.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
opt := &cos.DescribeMediaWorkflowOptions{
Ids: "w93aa43ba105347169fa093ed857b2a90,abc,123",
PageNumber: 1,
PageSize: 5,
}
DescribeWorkflowRes, _, err := c.CI.DescribeMediaWorkflow(context.Background(), opt)
log_status(err)
fmt.Printf("%+v\n", DescribeWorkflowRes)
}

// DeleteWorkflow 删除工作流
func DeleteWorkflow() {
u, _ := url.Parse("https://lilang-1253960454.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://lilang-1253960454.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
DescribeWorkflowRes, _, err := c.CI.DeleteMediaWorkflow(context.Background(), "w843779f0b22f49bbb7a189778d865059")
log_status(err)
fmt.Printf("%+v\n", DescribeWorkflowRes)
}

// CreateWorkflow 创建工作流
func CreateWorkflow() {
u, _ := url.Parse("https://lilang-1253960454.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://lilang-1253960454.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
// CreateMediaWorkflow
rand.Seed(time.Now().UnixNano())
createWorkflowOpt := &cos.CreateMediaWorkflowOptions{
MediaWorkflow: &cos.MediaWorkflow{
Name: "workflow-" + strconv.Itoa(rand.Intn(100)),
State: "Active",
Topology: &cos.Topology{
Dependencies: map[string]string{"Start": "Transcode_1581665960537", "Transcode_1581665960537": "Snapshot_1581665960536",
"Snapshot_1581665960536": "End"},
Nodes: map[string]cos.Node{"Start": cos.Node{Type: "Start", Input: &cos.NodeInput{QueueId: "p09d709939fef48a0a5c247ef39d90cec",
ObjectPrefix: "wk-test", ExtFilter: &cos.ExtFilter{State: "On", CustomExts: "mp4"}}},
"Transcode_1581665960537": cos.Node{Type: "Transcode", Operation: &cos.NodeOperation{TemplateId: "t01e57db1c2d154d2fb57aa5de9313a897",
Output: &cos.NodeOutput{Region: "ap-chongqing", Bucket: "lilang-1253960454", Object: "trans1.mp4"}}},
"Snapshot_1581665960536": cos.Node{Type: "Snapshot", Operation: &cos.NodeOperation{TemplateId: "t07740e32081b44ad7a0aea03adcffd54a",
Output: &cos.NodeOutput{Region: "ap-chongqing", Bucket: "lilang-1253960454", Object: "snapshot-${number}.jpg"}}},
},
},
},
}
createWorkflowRes, _, err := c.CI.CreateMediaWorkflow(context.Background(), createWorkflowOpt)
log_status(err)
fmt.Printf("%+v\n", createWorkflowRes.MediaWorkflow)

// DescribeMediaWorkflow
if createWorkflowRes.MediaWorkflow != nil {
opt := &cos.DescribeMediaWorkflowOptions{
Ids: createWorkflowRes.MediaWorkflow.WorkflowId,
}
DescribeWorkflowRes, _, err := c.CI.DescribeMediaWorkflow(context.Background(), opt)
log_status(err)
fmt.Printf("%+v\n", DescribeWorkflowRes)
}
}

// // UpdateTranscodeWorkflow TODO
// func UpdateTranscodeWorkflow() {
// u, _ := url.Parse("https://lilang-1253960454.cos.ap-chongqing.myqcloud.com")
// cu, _ := url.Parse("https://lilang-1253960454.ci.ap-chongqing.myqcloud.com")
// b := &cos.BaseURL{BucketURL: u, CIURL: cu}
// c := cos.NewClient(b, &http.Client{
// Transport: &cos.AuthorizationTransport{
// SecretID: os.Getenv("COS_SECRETID"),
// SecretKey: os.Getenv("COS_SECRETKEY"),
// Transport: &debug.DebugRequestTransport{
// RequestHeader: true,
// // Notice when put a large file and set need the request body, might happend out of memory error.
// RequestBody: true,
// ResponseHeader: true,
// ResponseBody: true,
// },
// },
// })
// // UpdateMediaWorkflow
// rand.Seed(time.Now().UnixNano())
// updateTplOpt := &cos.CreateMediaTranscodeWorkflowOptions{
// Tag: "Transcode",
// Name: "transtpl-" + strconv.Itoa(rand.Intn(100)),
// Container: &cos.Container{
// Format: "mp4",
// },
// Video: &cos.Video{
// Codec: "h.264",
// Width: "1280",
// Fps: "30",
// Crf: "26",
// },
// Audio: &cos.Audio{
// Codec: "aac",
// },
// TimeInterval: &cos.TimeInterval{
// Start: "0",
// Duration: "",
// },
// }
// WorkflowId := "t1d31d58d8a4204d7396087f56a448abd5"
// updateTplRes, _, err := c.CI.UpdateMediaTranscodeWorkflow(context.Background(), updateTplOpt, WorkflowId)
// log_status(err)
// fmt.Printf("%+v\n", updateTplRes.Workflow)

// opt := &cos.DescribeMediaWorkflowOptions{
// Ids: WorkflowId,
// }
// DescribeWorkflowRes, _, err := c.CI.DescribeMediaWorkflow(context.Background(), opt)
// log_status(err)
// fmt.Printf("%+v\n", DescribeWorkflowRes)
// }

func main() {
DescribeWorkflow()
// DeleteWorkflow()
// CreateWorkflow()
// UpdateTranscodeWorkflow()
// CreateSnapshotWorkflow()
// UpdateSnapshotWorkflow()
// CreateHighSpeedHdWorkflow()
// UpdateHighSpeedHdWorkflow()
// CreateAnimationWorkflow()
// UpdateAnimationWorkflow()
// CreateConcatWorkflow()
// UpdateConcatWorkflow()
// CreateVideoProcessWorkflow()
// UpdateVideoProcessWorkflow()
// CreateVideoMontageWorkflow()
// UpdateVideoMontageWorkflow()
// CreateVoiceSeparateWorkflow()
// UpdateVoiceSeparateWorkflow()
// CreateSuperResolutionWorkflow()
// UpdateSuperResolutionWorkflow()
// CreatePicProcessWorkflow()
// UpdatePicProcessWorkflow()
// CreateWatermarkWorkflow()
// UpdateWatermarkWorkflow()
}

0 comments on commit 3d28a02

Please sign in to comment.