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.

203 lines
6.8 KiB

5 years ago
5 years ago
4 years ago
4 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
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
4 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. ProcessResults *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. ETag string `xml:"ETag,omitempty"`
  38. }
  39. type PicImageInfo struct {
  40. Format string `xml:"Format,omitempty"`
  41. Width int `xml:"Width,omitempty"`
  42. Height int `xml:"Height,omitempty"`
  43. Quality int `xml:"Quality,omitempty"`
  44. Ave string `xml:"Ave,omitempty"`
  45. Orientation int `xml:"Orientation,omitempty"`
  46. }
  47. type PicProcessObject struct {
  48. Key string `xml:"Key,omitempty"`
  49. Location string `xml:"Location,omitempty"`
  50. Format string `xml:"Format,omitempty"`
  51. Width int `xml:"Width,omitempty"`
  52. Height int `xml:"Height,omitempty"`
  53. Size int `xml:"Size,omitempty"`
  54. Quality int `xml:"Quality,omitempty"`
  55. ETag string `xml:"ETag,omitempty"`
  56. WatermarkStatus int `xml:"WatermarkStatus,omitempty"`
  57. }
  58. type picOperationsHeader struct {
  59. PicOperations string `header:"Pic-Operations" xml:"-" url:"-"`
  60. }
  61. type ImageProcessOptions = PicOperations
  62. // 云上数据处理 https://cloud.tencent.com/document/product/460/18147
  63. func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error) {
  64. header := &picOperationsHeader{
  65. PicOperations: EncodePicOperations(opt),
  66. }
  67. var res ImageProcessResult
  68. sendOpt := sendOptions{
  69. baseURL: s.client.BaseURL.BucketURL,
  70. uri: "/" + encodeURIComponent(name) + "?image_process",
  71. method: http.MethodPost,
  72. optHeader: header,
  73. result: &res,
  74. }
  75. resp, err := s.client.send(ctx, &sendOpt)
  76. return &res, resp, err
  77. }
  78. type ImageRecognitionOptions struct {
  79. CIProcess string `url:"ci-process,omitempty"`
  80. DetectType string `url:"detect-type,omitempty"`
  81. }
  82. type ImageRecognitionResult struct {
  83. XMLName xml.Name `xml:"RecognitionResult"`
  84. PornInfo *RecognitionInfo `xml:"PornInfo,omitempty"`
  85. TerroristInfo *RecognitionInfo `xml:"TerroristInfo,omitempty"`
  86. PoliticsInfo *RecognitionInfo `xml:"PoliticsInfo,omitempty"`
  87. AdsInfo *RecognitionInfo `xml:"AdsInfo,omitempty"`
  88. }
  89. type RecognitionInfo struct {
  90. Code int `xml:"Code,omitempty"`
  91. Msg string `xml:"Msg,omitempty"`
  92. HitFlag int `xml:"HitFlag,omitempty"`
  93. Score int `xml:"Score,omitempty"`
  94. Label string `xml:"Label,omitempty"`
  95. Count int `xml:"Count,omitempty"`
  96. }
  97. // 图片审核 https://cloud.tencent.com/document/product/460/37318
  98. func (s *CIService) ImageRecognition(ctx context.Context, name string, opt *ImageRecognitionOptions) (*ImageRecognitionResult, *Response, error) {
  99. if opt != nil && opt.CIProcess == "" {
  100. opt.CIProcess = "sensitive-content-recognition"
  101. }
  102. var res ImageRecognitionResult
  103. sendOpt := sendOptions{
  104. baseURL: s.client.BaseURL.BucketURL,
  105. uri: "/" + encodeURIComponent(name),
  106. method: http.MethodGet,
  107. optQuery: opt,
  108. result: &res,
  109. }
  110. resp, err := s.client.send(ctx, &sendOpt)
  111. return &res, resp, err
  112. }
  113. type PutVideoAuditingJobOptions struct {
  114. XMLName xml.Name `xml:"Request"`
  115. InputObject string `xml:"Input>Object"`
  116. Conf *VideoAuditingJobConf `xml:"Conf"`
  117. }
  118. type VideoAuditingJobConf struct {
  119. DetectType string `xml:",omitempty"`
  120. Snapshot *PutVideoAuditingJobSnapshot `xml:",omitempty"`
  121. Callback string `xml:",omitempty"`
  122. }
  123. type PutVideoAuditingJobSnapshot struct {
  124. Mode string `xml:",omitempty"`
  125. Count int `xml:",omitempty"`
  126. TimeInterval float32 `xml:",omitempty"`
  127. Start float32 `xml:",omitempty"`
  128. }
  129. type PutVideoAuditingJobResult struct {
  130. XMLName xml.Name `xml:"Response"`
  131. JobsDetail struct {
  132. JobId string `xml:"JobId,omitempty"`
  133. State string `xml:"State,omitempty"`
  134. CreationTime string `xml:"CreationTime,omitempty"`
  135. Object string `xml:"Object,omitempty"`
  136. } `xml:"JobsDetail,omitempty"`
  137. }
  138. func (s *CIService) PutVideoAuditingJob(ctx context.Context, opt *PutVideoAuditingJobOptions) (*PutVideoAuditingJobResult, *Response, error) {
  139. var res PutVideoAuditingJobResult
  140. sendOpt := sendOptions{
  141. baseURL: s.client.BaseURL.CIURL,
  142. uri: "/video/auditing",
  143. method: http.MethodPost,
  144. body: opt,
  145. result: &res,
  146. }
  147. resp, err := s.client.send(ctx, &sendOpt)
  148. return &res, resp, err
  149. }
  150. type GetVideoAuditingJobResult struct {
  151. XMLName xml.Name `xml:"Response"`
  152. JobsDetail *VideoAuditingJobDetail `xml:",omitempty"`
  153. NonExistJobIds string `xml:",omitempty"`
  154. }
  155. type VideoAuditingJobDetail struct {
  156. Code string `xml:",omitempty"`
  157. Message string `xml:",omitempty"`
  158. JobId string `xml:",omitempty"`
  159. State string `xml:",omitempty"`
  160. CreationTime string `xml:",omitempty"`
  161. Object string `xml:",omitempty"`
  162. SnapshotCount string `xml:",omitempty"`
  163. Result int `xml:",omitempty"`
  164. PornInfo *RecognitionInfo `xml:",omitempty"`
  165. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  166. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  167. AdsInfo *RecognitionInfo `xml:",omitempty"`
  168. Snapshot *GetVideoAuditingJobSnapshot `xml:",omitempty"`
  169. }
  170. type GetVideoAuditingJobSnapshot struct {
  171. Url string `xml:",omitempty"`
  172. PornInfo *RecognitionInfo `xml:",omitempty"`
  173. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  174. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  175. AdsInfo *RecognitionInfo `xml:",omitempty"`
  176. }
  177. func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*GetVideoAuditingJobResult, *Response, error) {
  178. var res GetVideoAuditingJobResult
  179. sendOpt := sendOptions{
  180. baseURL: s.client.BaseURL.CIURL,
  181. uri: "/video/auditing/" + jobid,
  182. method: http.MethodGet,
  183. result: &res,
  184. }
  185. resp, err := s.client.send(ctx, &sendOpt)
  186. return &res, resp, err
  187. }