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.

803 lines
28 KiB

5 years ago
5 years ago
4 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
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "encoding/json"
  7. "encoding/xml"
  8. "errors"
  9. "fmt"
  10. "hash/crc64"
  11. "io"
  12. "net/http"
  13. "os"
  14. "strconv"
  15. )
  16. type CIService service
  17. type PicOperations struct {
  18. IsPicInfo int `json:"is_pic_info,omitempty"`
  19. Rules []PicOperationsRules `json:"rules,omitemtpy"`
  20. }
  21. type PicOperationsRules struct {
  22. Bucket string `json:"bucket,omitempty"`
  23. FileId string `json:"fileid"`
  24. Rule string `json:"rule"`
  25. }
  26. func EncodePicOperations(pic *PicOperations) string {
  27. if pic == nil {
  28. return ""
  29. }
  30. bs, err := json.Marshal(pic)
  31. if err != nil {
  32. return ""
  33. }
  34. return string(bs)
  35. }
  36. type ImageProcessResult struct {
  37. XMLName xml.Name `xml:"UploadResult"`
  38. OriginalInfo *PicOriginalInfo `xml:"OriginalInfo,omitempty"`
  39. ProcessResults *PicProcessObject `xml:"ProcessResults>Object,omitempty"`
  40. }
  41. type PicOriginalInfo struct {
  42. Key string `xml:"Key,omitempty"`
  43. Location string `xml:"Location,omitempty"`
  44. ImageInfo *PicImageInfo `xml:"ImageInfo,omitempty"`
  45. ETag string `xml:"ETag,omitempty"`
  46. }
  47. type PicImageInfo struct {
  48. Format string `xml:"Format,omitempty"`
  49. Width int `xml:"Width,omitempty"`
  50. Height int `xml:"Height,omitempty"`
  51. Quality int `xml:"Quality,omitempty"`
  52. Ave string `xml:"Ave,omitempty"`
  53. Orientation int `xml:"Orientation,omitempty"`
  54. }
  55. type PicProcessObject struct {
  56. Key string `xml:"Key,omitempty"`
  57. Location string `xml:"Location,omitempty"`
  58. Format string `xml:"Format,omitempty"`
  59. Width int `xml:"Width,omitempty"`
  60. Height int `xml:"Height,omitempty"`
  61. Size int `xml:"Size,omitempty"`
  62. Quality int `xml:"Quality,omitempty"`
  63. ETag string `xml:"ETag,omitempty"`
  64. WatermarkStatus int `xml:"WatermarkStatus,omitempty"`
  65. CodeStatus int `xml:"CodeStatus,omitempty"`
  66. QRcodeInfo []QRcodeInfo `xml:"QRcodeInfo,omitempty"`
  67. }
  68. type QRcodeInfo struct {
  69. CodeUrl string `xml:"CodeUrl,omitempty"`
  70. CodeLocation *CodeLocation `xml:"CodeLocation,omitempty"`
  71. }
  72. type CodeLocation struct {
  73. Point []string `xml:"Point,omitempty"`
  74. }
  75. type picOperationsHeader struct {
  76. PicOperations string `header:"Pic-Operations" xml:"-" url:"-"`
  77. }
  78. type ImageProcessOptions = PicOperations
  79. // 云上数据处理 https://cloud.tencent.com/document/product/460/18147
  80. func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error) {
  81. header := &picOperationsHeader{
  82. PicOperations: EncodePicOperations(opt),
  83. }
  84. var res ImageProcessResult
  85. sendOpt := sendOptions{
  86. baseURL: s.client.BaseURL.BucketURL,
  87. uri: "/" + encodeURIComponent(name) + "?image_process",
  88. method: http.MethodPost,
  89. optHeader: header,
  90. result: &res,
  91. }
  92. resp, err := s.client.send(ctx, &sendOpt)
  93. return &res, resp, err
  94. }
  95. // ImageRecognitionOptions is the option of ImageAuditing
  96. type ImageRecognitionOptions struct {
  97. CIProcess string `url:"ci-process,omitempty"`
  98. DetectType string `url:"detect-type,omitempty"`
  99. DetectUrl string `url:"detect-url,omitempty"`
  100. Interval int `url:"interval,omitempty"`
  101. MaxFrames int `url:"max-frames,omitempty"`
  102. BizType string `url:"biz-type,omitempty"`
  103. }
  104. // ImageRecognitionResult is the result of ImageRecognition/ImageAuditing
  105. type ImageRecognitionResult struct {
  106. XMLName xml.Name `xml:"RecognitionResult"`
  107. Text string `xml:"Text,omitempty"`
  108. Label string `xml:"Label,omitempty"`
  109. Result int `xml:"Result,omitempty"`
  110. Score int `xml:"Score,omitempty"`
  111. SubLabel string `xml:"SubLabel,omitempty"`
  112. PornInfo *RecognitionInfo `xml:"PornInfo,omitempty"`
  113. TerroristInfo *RecognitionInfo `xml:"TerroristInfo,omitempty"`
  114. PoliticsInfo *RecognitionInfo `xml:"PoliticsInfo,omitempty"`
  115. AdsInfo *RecognitionInfo `xml:"AdsInfo,omitempty"`
  116. }
  117. // RecognitionInfo is the result of auditing scene
  118. type RecognitionInfo struct {
  119. Code int `xml:"Code,omitempty"`
  120. Msg string `xml:"Msg,omitempty"`
  121. HitFlag int `xml:"HitFlag,omitempty"`
  122. Score int `xml:"Score,omitempty"`
  123. Label string `xml:"Label,omitempty"`
  124. Count int `xml:"Count,omitempty"`
  125. SubLabel string `xml:"SubLabel,omitempty"`
  126. Keywords string `xml:"Keywords,omitempty"`
  127. OcrResults []OcrResult `xml:"OcrResults,omitempty"`
  128. ObjectResults []ObjectResult `xml:"ObjectResults,omitempty"`
  129. LibResults []LibResult `xml:"LibResults,omitempty"`
  130. }
  131. // 图片审核 https://cloud.tencent.com/document/product/460/37318
  132. func (s *CIService) ImageRecognition(ctx context.Context, name string, DetectType string) (*ImageRecognitionResult, *Response, error) {
  133. opt := &ImageRecognitionOptions{
  134. CIProcess: "sensitive-content-recognition",
  135. DetectType: DetectType,
  136. }
  137. var res ImageRecognitionResult
  138. sendOpt := sendOptions{
  139. baseURL: s.client.BaseURL.BucketURL,
  140. uri: "/" + encodeURIComponent(name),
  141. method: http.MethodGet,
  142. optQuery: opt,
  143. result: &res,
  144. }
  145. resp, err := s.client.send(ctx, &sendOpt)
  146. return &res, resp, err
  147. }
  148. // 图片审核 支持detect-url等全部参数
  149. func (s *CIService) ImageAuditing(ctx context.Context, name string, opt *ImageRecognitionOptions) (*ImageRecognitionResult, *Response, error) {
  150. var res ImageRecognitionResult
  151. sendOpt := sendOptions{
  152. baseURL: s.client.BaseURL.BucketURL,
  153. uri: "/" + encodeURIComponent(name),
  154. method: http.MethodGet,
  155. optQuery: opt,
  156. result: &res,
  157. }
  158. resp, err := s.client.send(ctx, &sendOpt)
  159. return &res, resp, err
  160. }
  161. // PutVideoAuditingJobOptions is the option of PutVideoAuditingJob
  162. type PutVideoAuditingJobOptions struct {
  163. XMLName xml.Name `xml:"Request"`
  164. InputObject string `xml:"Input>Object,omitempty"`
  165. InputUrl string `xml:"Input>Url,omitempty"`
  166. Conf *VideoAuditingJobConf `xml:"Conf"`
  167. }
  168. // VideoAuditingJobConf is the config of PutVideoAuditingJobOptions
  169. type VideoAuditingJobConf struct {
  170. DetectType string `xml:",omitempty"`
  171. Snapshot *PutVideoAuditingJobSnapshot `xml:",omitempty"`
  172. Callback string `xml:",omitempty"`
  173. CallbackVersion string `xml:",omitempty"`
  174. BizType string `xml:",omitempty"`
  175. DetectContent int `xml:",omitempty"`
  176. }
  177. // PutVideoAuditingJobSnapshot is the snapshot config of VideoAuditingJobConf
  178. type PutVideoAuditingJobSnapshot struct {
  179. Mode string `xml:",omitempty"`
  180. Count int `xml:",omitempty"`
  181. TimeInterval float32 `xml:",omitempty"`
  182. Start float32 `xml:",omitempty"`
  183. }
  184. // PutVideoAuditingJobResult is the result of PutVideoAuditingJob
  185. type PutVideoAuditingJobResult struct {
  186. XMLName xml.Name `xml:"Response"`
  187. JobsDetail struct {
  188. JobId string `xml:"JobId,omitempty"`
  189. State string `xml:"State,omitempty"`
  190. CreationTime string `xml:"CreationTime,omitempty"`
  191. Object string `xml:"Object,omitempty"`
  192. } `xml:"JobsDetail,omitempty"`
  193. }
  194. // 视频审核-创建任务 https://cloud.tencent.com/document/product/460/46427
  195. func (s *CIService) PutVideoAuditingJob(ctx context.Context, opt *PutVideoAuditingJobOptions) (*PutVideoAuditingJobResult, *Response, error) {
  196. var res PutVideoAuditingJobResult
  197. sendOpt := sendOptions{
  198. baseURL: s.client.BaseURL.CIURL,
  199. uri: "/video/auditing",
  200. method: http.MethodPost,
  201. body: opt,
  202. result: &res,
  203. }
  204. resp, err := s.client.send(ctx, &sendOpt)
  205. return &res, resp, err
  206. }
  207. // GetVideoAuditingJobResult is the result of GetVideoAuditingJob
  208. type GetVideoAuditingJobResult struct {
  209. XMLName xml.Name `xml:"Response"`
  210. JobsDetail *AuditingJobDetail `xml:",omitempty"`
  211. NonExistJobIds string `xml:",omitempty"`
  212. }
  213. // AuditingJobDetail is the detail of GetVideoAuditingJobResult
  214. type AuditingJobDetail struct {
  215. Code string `xml:",omitempty"`
  216. Message string `xml:",omitempty"`
  217. JobId string `xml:",omitempty"`
  218. State string `xml:",omitempty"`
  219. CreationTime string `xml:",omitempty"`
  220. Object string `xml:",omitempty"`
  221. Url string `xml:",omitempty"`
  222. SnapshotCount string `xml:",omitempty"`
  223. Label string `xml:",omitempty"`
  224. Result int `xml:",omitempty"`
  225. PornInfo *RecognitionInfo `xml:",omitempty"`
  226. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  227. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  228. AdsInfo *RecognitionInfo `xml:",omitempty"`
  229. Snapshot []GetVideoAuditingJobSnapshot `xml:",omitempty"`
  230. AudioSection []AudioSectionResult `xml:",omitempty"`
  231. }
  232. // GetVideoAuditingJobSnapshot is the snapshot result of AuditingJobDetail
  233. type GetVideoAuditingJobSnapshot struct {
  234. Url string `xml:",omitempty"`
  235. Text string `xml:",omitempty"`
  236. SnapshotTime int `xml:",omitempty"`
  237. Label string `xml:",omitempty"`
  238. Result int `xml:",omitempty"`
  239. PornInfo *RecognitionInfo `xml:",omitempty"`
  240. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  241. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  242. AdsInfo *RecognitionInfo `xml:",omitempty"`
  243. }
  244. // AudioSectionResult is the audio section result of AuditingJobDetail/AudioAuditingJobDetail
  245. type AudioSectionResult struct {
  246. Url string `xml:",omitempty"`
  247. Text string `xml:",omitempty"`
  248. OffsetTime int `xml:",omitempty"`
  249. Duration int `xml:",omitempty"`
  250. Label string `xml:",omitempty"`
  251. Result int `xml:",omitempty"`
  252. PornInfo *RecognitionInfo `xml:",omitempty"`
  253. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  254. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  255. AdsInfo *RecognitionInfo `xml:",omitempty"`
  256. }
  257. // 视频审核-查询任务 https://cloud.tencent.com/document/product/460/46926
  258. func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*GetVideoAuditingJobResult, *Response, error) {
  259. var res GetVideoAuditingJobResult
  260. sendOpt := sendOptions{
  261. baseURL: s.client.BaseURL.CIURL,
  262. uri: "/video/auditing/" + jobid,
  263. method: http.MethodGet,
  264. result: &res,
  265. }
  266. resp, err := s.client.send(ctx, &sendOpt)
  267. return &res, resp, err
  268. }
  269. // PutAudioAuditingJobOptions is the option of PutAudioAuditingJob
  270. type PutAudioAuditingJobOptions struct {
  271. XMLName xml.Name `xml:"Request"`
  272. InputObject string `xml:"Input>Object,omitempty"`
  273. InputUrl string `xml:"Input>Url,omitempty"`
  274. Conf *AudioAuditingJobConf `xml:"Conf"`
  275. }
  276. // AudioAuditingJobConf is the config of PutAudioAuditingJobOptions
  277. type AudioAuditingJobConf struct {
  278. DetectType string `xml:",omitempty"`
  279. Callback string `xml:",omitempty"`
  280. CallbackVersion string `xml:",omitempty"`
  281. BizType string `xml:",omitempty"`
  282. }
  283. // PutAudioAuditingJobResult is the result of PutAudioAuditingJob
  284. type PutAudioAuditingJobResult PutVideoAuditingJobResult
  285. // 音频审核-创建任务 https://cloud.tencent.com/document/product/460/53395
  286. func (s *CIService) PutAudioAuditingJob(ctx context.Context, opt *PutAudioAuditingJobOptions) (*PutAudioAuditingJobResult, *Response, error) {
  287. var res PutAudioAuditingJobResult
  288. sendOpt := sendOptions{
  289. baseURL: s.client.BaseURL.CIURL,
  290. uri: "/audio/auditing",
  291. method: http.MethodPost,
  292. body: opt,
  293. result: &res,
  294. }
  295. resp, err := s.client.send(ctx, &sendOpt)
  296. return &res, resp, err
  297. }
  298. // GetAudioAuditingJobResult is the result of GetAudioAuditingJob
  299. type GetAudioAuditingJobResult struct {
  300. XMLName xml.Name `xml:"Response"`
  301. JobsDetail *AudioAuditingJobDetail `xml:",omitempty"`
  302. NonExistJobIds string `xml:",omitempty"`
  303. }
  304. // AudioAuditingJobDetail is the detail of GetAudioAuditingJobResult
  305. type AudioAuditingJobDetail struct {
  306. Code string `xml:",omitempty"`
  307. Message string `xml:",omitempty"`
  308. JobId string `xml:",omitempty"`
  309. State string `xml:",omitempty"`
  310. CreationTime string `xml:",omitempty"`
  311. Object string `xml:",omitempty"`
  312. Url string `xml:",omitempty"`
  313. AudioText string `xml:",omitempty"`
  314. Label string `xml:",omitempty"`
  315. Result int `xml:",omitempty"`
  316. PornInfo *RecognitionInfo `xml:",omitempty"`
  317. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  318. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  319. AdsInfo *RecognitionInfo `xml:",omitempty"`
  320. Section *AudioSectionResult `xml:",omitempty"`
  321. }
  322. // 音频审核-查询任务 https://cloud.tencent.com/document/product/460/53396
  323. func (s *CIService) GetAudioAuditingJob(ctx context.Context, jobid string) (*GetAudioAuditingJobResult, *Response, error) {
  324. var res GetAudioAuditingJobResult
  325. sendOpt := sendOptions{
  326. baseURL: s.client.BaseURL.CIURL,
  327. uri: "/audio/auditing/" + jobid,
  328. method: http.MethodGet,
  329. result: &res,
  330. }
  331. resp, err := s.client.send(ctx, &sendOpt)
  332. return &res, resp, err
  333. }
  334. // PutTextAuditingJobOptions is the option of PutTextAuditingJob
  335. type PutTextAuditingJobOptions struct {
  336. XMLName xml.Name `xml:"Request"`
  337. InputObject string `xml:"Input>Object,omitempty"`
  338. InputContent string `xml:"Input>Content,omitempty"`
  339. Conf *TextAuditingJobConf `xml:"Conf"`
  340. }
  341. // TextAuditingJobConf is the config of PutAudioAuditingJobOptions
  342. type TextAuditingJobConf struct {
  343. DetectType string `xml:",omitempty"`
  344. Callback string `xml:",omitempty"`
  345. CallbackVersion string `xml:",omitempty"`
  346. BizType string `xml:",omitempty"`
  347. }
  348. // PutTextAuditingJobResult is the result of PutTextAuditingJob
  349. type PutTextAuditingJobResult GetTextAuditingJobResult
  350. // 文本审核-创建任务 https://cloud.tencent.com/document/product/436/56289
  351. func (s *CIService) PutTextAuditingJob(ctx context.Context, opt *PutTextAuditingJobOptions) (*PutTextAuditingJobResult, *Response, error) {
  352. var res PutTextAuditingJobResult
  353. sendOpt := sendOptions{
  354. baseURL: s.client.BaseURL.CIURL,
  355. uri: "/text/auditing",
  356. method: http.MethodPost,
  357. body: opt,
  358. result: &res,
  359. }
  360. resp, err := s.client.send(ctx, &sendOpt)
  361. return &res, resp, err
  362. }
  363. // GetTextAuditingJobResult is the result of GetTextAuditingJob
  364. type GetTextAuditingJobResult struct {
  365. XMLName xml.Name `xml:"Response"`
  366. JobsDetail *TextAuditingJobDetail `xml:",omitempty"`
  367. NonExistJobIds string `xml:",omitempty"`
  368. }
  369. // TextAuditingJobDetail is the detail of GetTextAuditingJobResult
  370. type TextAuditingJobDetail struct {
  371. Code string `xml:",omitempty"`
  372. Message string `xml:",omitempty"`
  373. JobId string `xml:",omitempty"`
  374. State string `xml:",omitempty"`
  375. CreationTime string `xml:",omitempty"`
  376. Object string `xml:",omitempty"`
  377. Url string `xml:",omitempty"`
  378. Content string `xml:",omitempty"`
  379. SectionCount int `xml:",omitempty"`
  380. Label string `xml:",omitempty"`
  381. Result int `xml:",omitempty"`
  382. PornInfo *RecognitionInfo `xml:",omitempty"`
  383. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  384. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  385. AdsInfo *RecognitionInfo `xml:",omitempty"`
  386. IllegalInfo *RecognitionInfo `xml:",omitempty"`
  387. AbuseInfo *RecognitionInfo `xml:",omitempty"`
  388. Section []TextSectionResult `xml:",omitempty"`
  389. }
  390. // TextSectionResult is the section result of TextAuditingJobDetail
  391. type TextSectionResult struct {
  392. StartByte int `xml:",omitempty"`
  393. Label string `xml:",omitempty"`
  394. Result int `xml:",omitempty"`
  395. PornInfo *RecognitionInfo `xml:",omitempty"`
  396. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  397. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  398. AdsInfo *RecognitionInfo `xml:",omitempty"`
  399. IllegalInfo *RecognitionInfo `xml:",omitempty"`
  400. AbuseInfo *RecognitionInfo `xml:",omitempty"`
  401. }
  402. // 文本审核-查询任务 https://cloud.tencent.com/document/product/436/56288
  403. func (s *CIService) GetTextAuditingJob(ctx context.Context, jobid string) (*GetTextAuditingJobResult, *Response, error) {
  404. var res GetTextAuditingJobResult
  405. sendOpt := sendOptions{
  406. baseURL: s.client.BaseURL.CIURL,
  407. uri: "/text/auditing/" + jobid,
  408. method: http.MethodGet,
  409. result: &res,
  410. }
  411. resp, err := s.client.send(ctx, &sendOpt)
  412. return &res, resp, err
  413. }
  414. // PutDocumentAuditingJobOptions is the option of PutDocumentAuditingJob
  415. type PutDocumentAuditingJobOptions struct {
  416. XMLName xml.Name `xml:"Request"`
  417. InputUrl string `xml:"Input>Url,omitempty"`
  418. InputType string `xml:"Input>Type,omitempty"`
  419. Conf *DocumentAuditingJobConf `xml:"Conf"`
  420. }
  421. // DocumentAuditingJobConf is the config of PutDocumentAuditingJobOptions
  422. type DocumentAuditingJobConf struct {
  423. DetectType string `xml:",omitempty"`
  424. Callback string `xml:",omitempty"`
  425. BizType string `xml:",omitempty"`
  426. }
  427. // PutDocumentAuditingJobResult is the result of PutDocumentAuditingJob
  428. type PutDocumentAuditingJobResult PutVideoAuditingJobResult
  429. // 文档审核-创建任务 https://cloud.tencent.com/document/product/436/59381
  430. func (s *CIService) PutDocumentAuditingJob(ctx context.Context, opt *PutDocumentAuditingJobOptions) (*PutDocumentAuditingJobResult, *Response, error) {
  431. var res PutDocumentAuditingJobResult
  432. sendOpt := sendOptions{
  433. baseURL: s.client.BaseURL.CIURL,
  434. uri: "/document/auditing",
  435. method: http.MethodPost,
  436. body: opt,
  437. result: &res,
  438. }
  439. resp, err := s.client.send(ctx, &sendOpt)
  440. return &res, resp, err
  441. }
  442. // GetDocumentAuditingJobResult is the result of GetDocumentAuditingJob
  443. type GetDocumentAuditingJobResult struct {
  444. XMLName xml.Name `xml:"Response"`
  445. JobsDetail *DocumentAuditingJobDetail `xml:",omitempty"`
  446. NonExistJobIds string `xml:",omitempty"`
  447. }
  448. // DocumentAuditingJobDetail is the detail of GetDocumentAuditingJobResult
  449. type DocumentAuditingJobDetail struct {
  450. Code string `xml:",omitempty"`
  451. Message string `xml:",omitempty"`
  452. JobId string `xml:",omitempty"`
  453. State string `xml:",omitempty"`
  454. CreationTime string `xml:",omitempty"`
  455. Object string `xml:",omitempty"`
  456. Url string `xml:",omitempty"`
  457. PageCount int `xml:",omitempty"`
  458. Label string `xml:",omitempty"`
  459. Suggestion int `xml:",omitempty"`
  460. Labels *DocumentResultInfo `xml:",omitempty"`
  461. PageSegment *DocumentPageSegmentInfo `xml:",omitempty"`
  462. }
  463. // DocumentResultInfo
  464. type DocumentResultInfo struct {
  465. PornInfo *RecognitionInfo `xml:",omitempty"`
  466. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  467. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  468. AdsInfo *RecognitionInfo `xml:",omitempty"`
  469. }
  470. // DocumentPageSegmentInfo
  471. type DocumentPageSegmentInfo struct {
  472. Results []DocumentPageSegmentResultResult `xml:",omitempty"`
  473. }
  474. // DocumentPageSegmentResultResult
  475. type DocumentPageSegmentResultResult struct {
  476. Url string `xml:",omitempty"`
  477. Text string `xml:",omitempty"`
  478. PageNumber int `xml:",omitempty"`
  479. SheetNumber int `xml:",omitempty"`
  480. Label string `xml:",omitempty"`
  481. Suggestion int `xml:",omitempty"`
  482. PornInfo *RecognitionInfo `xml:",omitempty"`
  483. TerrorismInfo *RecognitionInfo `xml:",omitempty"`
  484. PoliticsInfo *RecognitionInfo `xml:",omitempty"`
  485. AdsInfo *RecognitionInfo `xml:",omitempty"`
  486. }
  487. // OcrResult
  488. type OcrResult struct {
  489. Text string `xml:"Text"`
  490. Keywords []string `xml:"Keywords"`
  491. Location *Location `xml:"Location,omitempty"`
  492. }
  493. // ObjectResult
  494. type ObjectResult struct {
  495. Name string `xml:"Name"`
  496. Location *Location `xml:"Location,omitempty"`
  497. }
  498. // LibResult
  499. type LibResult struct {
  500. ImageId string `xml:"ImageId"`
  501. Score uint32 `xml:"Score"`
  502. }
  503. // Location
  504. type Location struct {
  505. X float64 `xml:"X,omitempty"` // 左上角横坐标
  506. Y float64 `xml:"Y,omitempty"` // 左上角纵坐标
  507. Width float64 `xml:"Width,omitempty"` // 宽度
  508. Height float64 `xml:"Height,omitempty"` // 高度
  509. Rotate float64 `xml:"Rotate,omitempty"` // 检测框的旋转角度
  510. }
  511. // 文档审核-查询任务 https://cloud.tencent.com/document/product/436/59382
  512. func (s *CIService) GetDocumentAuditingJob(ctx context.Context, jobid string) (*GetDocumentAuditingJobResult, *Response, error) {
  513. var res GetDocumentAuditingJobResult
  514. sendOpt := sendOptions{
  515. baseURL: s.client.BaseURL.CIURL,
  516. uri: "/document/auditing/" + jobid,
  517. method: http.MethodGet,
  518. result: &res,
  519. }
  520. resp, err := s.client.send(ctx, &sendOpt)
  521. return &res, resp, err
  522. }
  523. // 图片持久化处理-上传时处理 https://cloud.tencent.com/document/product/460/18147
  524. // 盲水印-上传时添加 https://cloud.tencent.com/document/product/460/19017
  525. // 二维码识别-上传时识别 https://cloud.tencent.com/document/product/460/37513
  526. func (s *CIService) Put(ctx context.Context, name string, r io.Reader, uopt *ObjectPutOptions) (*ImageProcessResult, *Response, error) {
  527. if r == nil {
  528. return nil, nil, fmt.Errorf("reader is nil")
  529. }
  530. if err := CheckReaderLen(r); err != nil {
  531. return nil, nil, err
  532. }
  533. opt := CloneObjectPutOptions(uopt)
  534. totalBytes, err := GetReaderLen(r)
  535. if err != nil && opt != nil && opt.Listener != nil {
  536. if opt.ContentLength == 0 {
  537. return nil, nil, err
  538. }
  539. totalBytes = opt.ContentLength
  540. }
  541. if err == nil {
  542. // 与 go http 保持一致, 非bytes.Buffer/bytes.Reader/strings.Reader由用户指定ContentLength, 或使用 Chunk 上传
  543. if opt != nil && opt.ContentLength == 0 && IsLenReader(r) {
  544. opt.ContentLength = totalBytes
  545. }
  546. }
  547. reader := TeeReader(r, nil, totalBytes, nil)
  548. if s.client.Conf.EnableCRC {
  549. reader.writer = crc64.New(crc64.MakeTable(crc64.ECMA))
  550. }
  551. if opt != nil && opt.Listener != nil {
  552. reader.listener = opt.Listener
  553. }
  554. var res ImageProcessResult
  555. sendOpt := sendOptions{
  556. baseURL: s.client.BaseURL.BucketURL,
  557. uri: "/" + encodeURIComponent(name),
  558. method: http.MethodPut,
  559. body: reader,
  560. optHeader: opt,
  561. result: &res,
  562. }
  563. resp, err := s.client.send(ctx, &sendOpt)
  564. return &res, resp, err
  565. }
  566. // ci put object from local file
  567. func (s *CIService) PutFromFile(ctx context.Context, name string, filePath string, opt *ObjectPutOptions) (*ImageProcessResult, *Response, error) {
  568. fd, err := os.Open(filePath)
  569. if err != nil {
  570. return nil, nil, err
  571. }
  572. defer fd.Close()
  573. return s.Put(ctx, name, fd, opt)
  574. }
  575. // 基本图片处理 https://cloud.tencent.com/document/product/460/36540
  576. // 盲水印-下载时添加 https://cloud.tencent.com/document/product/460/19017
  577. func (s *CIService) Get(ctx context.Context, name string, operation string, opt *ObjectGetOptions, id ...string) (*Response, error) {
  578. var u string
  579. if len(id) == 1 {
  580. u = fmt.Sprintf("/%s?versionId=%s&%s", encodeURIComponent(name), id[0], operation)
  581. } else if len(id) == 0 {
  582. u = fmt.Sprintf("/%s?%s", encodeURIComponent(name), operation)
  583. } else {
  584. return nil, errors.New("wrong params")
  585. }
  586. sendOpt := sendOptions{
  587. baseURL: s.client.BaseURL.BucketURL,
  588. uri: u,
  589. method: http.MethodGet,
  590. optQuery: opt,
  591. optHeader: opt,
  592. disableCloseBody: true,
  593. }
  594. resp, err := s.client.send(ctx, &sendOpt)
  595. if opt != nil && opt.Listener != nil {
  596. if err == nil && resp != nil {
  597. if totalBytes, e := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64); e == nil {
  598. resp.Body = TeeReader(resp.Body, nil, totalBytes, opt.Listener)
  599. }
  600. }
  601. }
  602. return resp, err
  603. }
  604. func (s *CIService) GetToFile(ctx context.Context, name, localpath, operation string, opt *ObjectGetOptions, id ...string) (*Response, error) {
  605. resp, err := s.Get(ctx, name, operation, opt, id...)
  606. if err != nil {
  607. return resp, err
  608. }
  609. defer resp.Body.Close()
  610. // If file exist, overwrite it
  611. fd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
  612. if err != nil {
  613. return resp, err
  614. }
  615. _, err = io.Copy(fd, resp.Body)
  616. fd.Close()
  617. if err != nil {
  618. return resp, err
  619. }
  620. return resp, nil
  621. }
  622. type GetQRcodeResult struct {
  623. XMLName xml.Name `xml:"Response"`
  624. CodeStatus int `xml:"CodeStatus,omitempty"`
  625. QRcodeInfo *QRcodeInfo `xml:"QRcodeInfo,omitempty"`
  626. ResultImage string `xml:"ResultImage,omitempty"`
  627. }
  628. // 二维码识别-下载时识别 https://cloud.tencent.com/document/product/436/54070
  629. func (s *CIService) GetQRcode(ctx context.Context, name string, cover int, opt *ObjectGetOptions, id ...string) (*GetQRcodeResult, *Response, error) {
  630. var u string
  631. if len(id) == 1 {
  632. u = fmt.Sprintf("/%s?versionId=%s&ci-process=QRcode&cover=%v", encodeURIComponent(name), id[0], cover)
  633. } else if len(id) == 0 {
  634. u = fmt.Sprintf("/%s?ci-process=QRcode&cover=%v", encodeURIComponent(name), cover)
  635. } else {
  636. return nil, nil, errors.New("wrong params")
  637. }
  638. var res GetQRcodeResult
  639. sendOpt := sendOptions{
  640. baseURL: s.client.BaseURL.BucketURL,
  641. uri: u,
  642. method: http.MethodGet,
  643. optQuery: opt,
  644. optHeader: opt,
  645. result: &res,
  646. }
  647. resp, err := s.client.send(ctx, &sendOpt)
  648. return &res, resp, err
  649. }
  650. type GenerateQRcodeOptions struct {
  651. QRcodeContent string `url:"qrcode-content,omitempty"`
  652. Mode int `url:"mode,omitempty"`
  653. Width int `url:"width,omitempty"`
  654. }
  655. type GenerateQRcodeResult struct {
  656. XMLName xml.Name `xml:"Response"`
  657. ResultImage string `xml:"ResultImage,omitempty"`
  658. }
  659. // 二维码生成 https://cloud.tencent.com/document/product/436/54071
  660. func (s *CIService) GenerateQRcode(ctx context.Context, opt *GenerateQRcodeOptions) (*GenerateQRcodeResult, *Response, error) {
  661. var res GenerateQRcodeResult
  662. sendOpt := &sendOptions{
  663. baseURL: s.client.BaseURL.BucketURL,
  664. uri: "/?ci-process=qrcode-generate",
  665. method: http.MethodGet,
  666. optQuery: opt,
  667. result: &res,
  668. }
  669. resp, err := s.client.send(ctx, sendOpt)
  670. return &res, resp, err
  671. }
  672. func (s *CIService) GenerateQRcodeToFile(ctx context.Context, filePath string, opt *GenerateQRcodeOptions) (*GenerateQRcodeResult, *Response, error) {
  673. res, resp, err := s.GenerateQRcode(ctx, opt)
  674. if err != nil {
  675. return res, resp, err
  676. }
  677. // If file exist, overwrite it
  678. fd, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
  679. if err != nil {
  680. return res, resp, err
  681. }
  682. defer fd.Close()
  683. bs, err := base64.StdEncoding.DecodeString(res.ResultImage)
  684. if err != nil {
  685. return res, resp, err
  686. }
  687. fb := bytes.NewReader(bs)
  688. _, err = io.Copy(fd, fb)
  689. return res, resp, err
  690. }
  691. // 开通 Guetzli 压缩 https://cloud.tencent.com/document/product/460/30112
  692. func (s *CIService) PutGuetzli(ctx context.Context) (*Response, error) {
  693. sendOpt := &sendOptions{
  694. baseURL: s.client.BaseURL.CIURL,
  695. uri: "/?guetzli",
  696. method: http.MethodPut,
  697. }
  698. resp, err := s.client.send(ctx, sendOpt)
  699. return resp, err
  700. }
  701. type GetGuetzliResult struct {
  702. XMLName xml.Name `xml:"GuetzliStatus"`
  703. GuetzliStatus string `xml:",chardata"`
  704. }
  705. // 查询 Guetzli 状态 https://cloud.tencent.com/document/product/460/30111
  706. func (s *CIService) GetGuetzli(ctx context.Context) (*GetGuetzliResult, *Response, error) {
  707. var res GetGuetzliResult
  708. sendOpt := &sendOptions{
  709. baseURL: s.client.BaseURL.CIURL,
  710. uri: "/?guetzli",
  711. method: http.MethodGet,
  712. result: &res,
  713. }
  714. resp, err := s.client.send(ctx, sendOpt)
  715. return &res, resp, err
  716. }
  717. // 关闭 Guetzli 压缩 https://cloud.tencent.com/document/product/460/30113
  718. func (s *CIService) DeleteGuetzli(ctx context.Context) (*Response, error) {
  719. sendOpt := &sendOptions{
  720. baseURL: s.client.BaseURL.CIURL,
  721. uri: "/?guetzli",
  722. method: http.MethodDelete,
  723. }
  724. resp, err := s.client.send(ctx, sendOpt)
  725. return resp, err
  726. }