Skip to content

Commit caaca46

Browse files
author
lilang
committed
文档对比
1 parent affc131 commit caaca46

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

ci_doc.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package cos
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"encoding/xml"
8+
"fmt"
9+
"io"
710
"net/http"
811
"net/url"
912
)
@@ -293,6 +296,53 @@ func (s *CIService) DocPreview(ctx context.Context, name string, opt *DocPreview
293296
return resp, err
294297
}
295298

299+
type CIDocCompareOptions struct {
300+
Object string `url:"object,omitempty"`
301+
ComparePath string `url:"comparePath,omitempty"`
302+
CompareUrl string `url:"compareUrl,omitempty"`
303+
SrcType string `url:"srcType,omitempty"`
304+
TgtUri string `url:"tgtUri,omitempty"`
305+
}
306+
307+
type CIDocCompareResult struct {
308+
Code string `xml:"Code,omitempty" json:"code,omitempty"`
309+
ETag string `xml:"ETag,omitempty" json:"eTag,omitempty"`
310+
Msg string `xml:"Msg,omitempty" json:"msg,omitempty"`
311+
ResultPath string `xml:"ResultPath,omitempty" json:"resultPath,omitempty"`
312+
}
313+
314+
// 优先 json
315+
func (w *CIDocCompareResult) Write(p []byte) (n int, err error) {
316+
err = json.Unmarshal(p, w)
317+
if err != nil {
318+
fmt.Println(err.Error())
319+
err = xml.NewDecoder(bytes.NewReader(p)).Decode(w)
320+
if err == nil {
321+
return len(p), nil
322+
}
323+
if err == io.EOF {
324+
err = nil // ignore EOF errors caused by empty response body
325+
}
326+
return 0, err
327+
}
328+
return len(p), nil
329+
}
330+
331+
// DocCompare TODO
332+
func (s *CIService) CIDocCompare(ctx context.Context, opt *CIDocCompareOptions) (*Response, *CIDocCompareResult, error) {
333+
var res CIDocCompareResult
334+
sendOpt := sendOptions{
335+
baseURL: s.client.BaseURL.BucketURL,
336+
uri: "/doccompare",
337+
optQuery: opt,
338+
method: http.MethodGet,
339+
disableCloseBody: true,
340+
result: &res,
341+
}
342+
resp, err := s.client.send(ctx, &sendOpt)
343+
return resp, &res, err
344+
}
345+
296346
type DocPreviewHTMLOptions struct {
297347
DstType string `url:"dstType,omitempty"`
298348
SrcType string `url:"srcType,omitempty"`

ci_doc_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ func TestCIService_DocPreview(t *testing.T) {
202202
}
203203
}
204204

205+
func TestCIService_CIDocCompare(t *testing.T) {
206+
setup()
207+
defer teardown()
208+
209+
mux.HandleFunc("/doccompare", func(w http.ResponseWriter, r *http.Request) {
210+
testMethod(t, r, http.MethodGet)
211+
v := values{
212+
"object": "doc/1.docx",
213+
"comparePath": "doc/2.docx",
214+
}
215+
testFormValues(t, r, v)
216+
})
217+
218+
opt := &CIDocCompareOptions{
219+
Object: "doc/1.docx",
220+
ComparePath: "doc/2.docx",
221+
}
222+
223+
_, _, err := client.CI.CIDocCompare(context.Background(), opt)
224+
if err != nil {
225+
t.Fatalf("CI.DocPreview returned error: %v", err)
226+
}
227+
}
228+
205229
func TestCIService_DocPreviewHTML(t *testing.T) {
206230
setup()
207231
defer teardown()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35+
cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
36+
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
37+
c := cos.NewClient(b, &http.Client{
38+
Transport: &cos.AuthorizationTransport{
39+
SecretID: os.Getenv("COS_SECRETID"),
40+
SecretKey: os.Getenv("COS_SECRETKEY"),
41+
Transport: &debug.DebugRequestTransport{
42+
RequestHeader: true,
43+
// Notice when put a large file and set need the request body, might happend out of memory error.
44+
RequestBody: true,
45+
ResponseHeader: true,
46+
ResponseBody: false,
47+
},
48+
},
49+
})
50+
51+
opt := &cos.CIDocCompareOptions{
52+
Object: "doc/1.docx",
53+
ComparePath: "doc/2.docx",
54+
}
55+
_, res, err := c.CI.CIDocCompare(context.Background(), opt)
56+
fmt.Printf("%+v\n", res)
57+
log_status(err)
58+
}

0 commit comments

Comments
 (0)