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.

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