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.

243 lines
7.8 KiB

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