|
1 | 1 | package cos |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
| 6 | + "encoding/xml" |
| 7 | + "net/http" |
5 | 8 | ) |
6 | 9 |
|
| 10 | +type CIService service |
| 11 | + |
7 | 12 | type PicOperations struct { |
8 | 13 | IsPicInfo int `json:"is_pic_info,omitempty"` |
9 | 14 | Rules []PicOperationsRules `json:"rules,omitemtpy"` |
10 | 15 | } |
11 | | - |
12 | 16 | type PicOperationsRules struct { |
13 | 17 | Bucket string `json:"bucket,omitempty"` |
14 | 18 | FileId string `json:"fileid"` |
15 | 19 | Rule string `json:"rule"` |
16 | 20 | } |
17 | 21 |
|
18 | 22 | func EncodePicOperations(pic *PicOperations) string { |
| 23 | + if pic == nil { |
| 24 | + return "" |
| 25 | + } |
19 | 26 | bs, err := json.Marshal(pic) |
20 | 27 | if err != nil { |
21 | 28 | return "" |
22 | 29 | } |
23 | 30 | return string(bs) |
24 | 31 | } |
| 32 | + |
| 33 | +type ImageProcessResult struct { |
| 34 | + XMLName xml.Name `xml:"UploadResult"` |
| 35 | + OriginalInfo *PicOriginalInfo `xml:"OriginalInfo,omitempty"` |
| 36 | + ProcessObject *PicProcessObject `xml:"ProcessResults>Object,omitempty"` |
| 37 | +} |
| 38 | +type PicOriginalInfo struct { |
| 39 | + Key string `xml:"Key,omitempty"` |
| 40 | + Location string `xml:"Location,omitempty"` |
| 41 | + ImageInfo *PicImageInfo `xml:"ImageInfo,omitempty"` |
| 42 | +} |
| 43 | +type PicImageInfo struct { |
| 44 | + Format string `xml:"Format,omitempty"` |
| 45 | + Width int `xml:"Width,omitempty"` |
| 46 | + Height int `xml:"Height,omitempty"` |
| 47 | + Size int `xml:"Size,omitempty"` |
| 48 | + Quality int `xml:"Quality,omitempty"` |
| 49 | +} |
| 50 | +type PicProcessObject struct { |
| 51 | + Key string `xml:"Key,omitempty"` |
| 52 | + Location string `xml:"Location,omitempty"` |
| 53 | + Format string `xml:"Format,omitempty"` |
| 54 | + Width int `xml:"Width,omitempty"` |
| 55 | + Height int `xml:"Height,omitempty"` |
| 56 | + Size int `xml:"Size,omitempty"` |
| 57 | + Quality int `xml:"Quality,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +type picOperationsHeader struct { |
| 61 | + PicOperations string `header:"Pic-Operations" xml:"-" url:"-"` |
| 62 | +} |
| 63 | + |
| 64 | +type ImageProcessOptions = PicOperations |
| 65 | + |
| 66 | +// 云上数据处理 https://cloud.tencent.com/document/product/460/18147 |
| 67 | +func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error) { |
| 68 | + header := &picOperationsHeader{ |
| 69 | + PicOperations: EncodePicOperations(opt), |
| 70 | + } |
| 71 | + var res ImageProcessResult |
| 72 | + sendOpt := sendOptions{ |
| 73 | + baseURL: s.client.BaseURL.BucketURL, |
| 74 | + uri: "/" + encodeURIComponent(name) + "?image_process", |
| 75 | + method: http.MethodPost, |
| 76 | + optHeader: header, |
| 77 | + result: &res, |
| 78 | + } |
| 79 | + resp, err := s.client.send(ctx, &sendOpt) |
| 80 | + return &res, resp, err |
| 81 | +} |
| 82 | + |
| 83 | +type ImageRecognitionOptions struct { |
| 84 | + CIProcess string `url:"ci-process,omitempty"` |
| 85 | + DetectType string `url:"detect-type,omitempty"` |
| 86 | +} |
| 87 | + |
| 88 | +type ImageRecognitionResult struct { |
| 89 | + XMLName xml.Name `xml:"RecognitionResult"` |
| 90 | + PornInfo *RecognitionInfo `xml:"PornInfo,omitempty"` |
| 91 | + TerroristInfo *RecognitionInfo `xml:"TerroristInfo,omitempty"` |
| 92 | + PoliticsInfo *RecognitionInfo `xml:"PoliticsInfo,omitempty"` |
| 93 | + AdsInfo *RecognitionInfo `xml:"AdsInfo,omitempty"` |
| 94 | +} |
| 95 | +type RecognitionInfo struct { |
| 96 | + Code int `xml:"Code,omitempty"` |
| 97 | + Msg string `xml:"Msg,omitempty"` |
| 98 | + HitFlag int `xml:"HitFlag,omitempty"` |
| 99 | + Score int `xml:"Score,omitempty"` |
| 100 | + Label string `xml:"Label,omitempty"` |
| 101 | + Count int `xml:"Count,omitempty"` |
| 102 | +} |
| 103 | + |
| 104 | +// 图片审核 https://cloud.tencent.com/document/product/460/37318 |
| 105 | +func (s *CIService) ImageRecognition(ctx context.Context, name string, opt *ImageRecognitionOptions) (*ImageRecognitionResult, *Response, error) { |
| 106 | + if opt != nil && opt.CIProcess == "" { |
| 107 | + opt.CIProcess = "sensitive-content-recognition" |
| 108 | + } |
| 109 | + var res ImageRecognitionResult |
| 110 | + sendOpt := sendOptions{ |
| 111 | + baseURL: s.client.BaseURL.BucketURL, |
| 112 | + uri: "/" + encodeURIComponent(name), |
| 113 | + method: http.MethodGet, |
| 114 | + optQuery: opt, |
| 115 | + result: &res, |
| 116 | + } |
| 117 | + resp, err := s.client.send(ctx, &sendOpt) |
| 118 | + return &res, resp, err |
| 119 | +} |
| 120 | + |
| 121 | +type PutVideoAuditingJobOptions struct { |
| 122 | + XMLName xml.Name `xml:"Request"` |
| 123 | + InputObject string `xml:"Input>Object"` |
| 124 | + Conf *VideoAuditingJobConf `xml:"Conf"` |
| 125 | +} |
| 126 | +type VideoAuditingJobConf struct { |
| 127 | + DetectType string `xml:",omitempty"` |
| 128 | + Snapshot *PutVideoAuditingJobSnapshot `xml:",omitempty"` |
| 129 | + Callback string `xml:",omitempty"` |
| 130 | +} |
| 131 | +type PutVideoAuditingJobSnapshot struct { |
| 132 | + Mode string `xml:",omitempty"` |
| 133 | + Count int `xml:",omitempty"` |
| 134 | + TimeInterval float32 `xml:",omitempty"` |
| 135 | + Start float32 `xml:",omitempty"` |
| 136 | +} |
| 137 | + |
| 138 | +type PutVideoAuditingJobResult struct { |
| 139 | + XMLName xml.Name `xml:"Response"` |
| 140 | + JobsDetail struct { |
| 141 | + JobId string `xml:"JobId,omitempty"` |
| 142 | + State string `xml:"State,omitempty"` |
| 143 | + CreationTime string `xml:"CreationTime,omitempty"` |
| 144 | + Object string `xml:"Object,omitempty"` |
| 145 | + } `xml:"JobsDetail,omitempty"` |
| 146 | +} |
| 147 | + |
| 148 | +func (s *CIService) PutVideoAuditingJob(ctx context.Context, opt *PutVideoAuditingJobOptions) (*PutVideoAuditingJobResult, *Response, error) { |
| 149 | + var res PutVideoAuditingJobResult |
| 150 | + sendOpt := sendOptions{ |
| 151 | + baseURL: s.client.BaseURL.CIURL, |
| 152 | + uri: "/video/auditing", |
| 153 | + method: http.MethodPost, |
| 154 | + body: opt, |
| 155 | + result: &res, |
| 156 | + } |
| 157 | + resp, err := s.client.send(ctx, &sendOpt) |
| 158 | + return &res, resp, err |
| 159 | +} |
| 160 | + |
| 161 | +type GetVideoAuditingJobResult struct { |
| 162 | + XMLName xml.Name `xml:"Response"` |
| 163 | + JobsDetail *VideoAuditingJobDetail `xml:",omitempty"` |
| 164 | + NonExistJobIds string `xml:",omitempty"` |
| 165 | +} |
| 166 | +type VideoAuditingJobDetail struct { |
| 167 | + Code string `xml:",omitempty"` |
| 168 | + Message string `xml:",omitempty"` |
| 169 | + JobId string `xml:",omitempty"` |
| 170 | + State string `xml:",omitempty"` |
| 171 | + CreationTime string `xml:",omitempty"` |
| 172 | + Object string `xml:",omitempty"` |
| 173 | + SnapshotCount string `xml:",omitempty"` |
| 174 | + result int `xml:",omitempty"` |
| 175 | + PornInfo *RecognitionInfo `xml:",omitempty"` |
| 176 | + TerrorismInfo *RecognitionInfo `xml:",omitempty"` |
| 177 | + PoliticsInfo *RecognitionInfo `xml:",omitempty"` |
| 178 | + AdsInfo *RecognitionInfo `xml:",omitempty"` |
| 179 | + Snapshot *GetVideoAuditingJobSnapshot `xml:",omitempty"` |
| 180 | +} |
| 181 | +type GetVideoAuditingJobSnapshot struct { |
| 182 | + Url string `xml:",omitempty"` |
| 183 | + PornInfo *RecognitionInfo `xml:",omitempty"` |
| 184 | + TerrorismInfo *RecognitionInfo `xml:",omitempty"` |
| 185 | + PoliticsInfo *RecognitionInfo `xml:",omitempty"` |
| 186 | + AdsInfo *RecognitionInfo `xml:",omitempty"` |
| 187 | +} |
| 188 | + |
| 189 | +func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*GetVideoAuditingJobResult, *Response, error) { |
| 190 | + var res GetVideoAuditingJobResult |
| 191 | + sendOpt := sendOptions{ |
| 192 | + baseURL: s.client.BaseURL.CIURL, |
| 193 | + uri: "/video/auditing/" + jobid, |
| 194 | + method: http.MethodGet, |
| 195 | + result: &res, |
| 196 | + } |
| 197 | + resp, err := s.client.send(ctx, &sendOpt) |
| 198 | + return &res, resp, err |
| 199 | +} |
0 commit comments