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.

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