You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.5 KiB

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