Skip to content

Commit

Permalink
文档预览
Browse files Browse the repository at this point in the history
  • Loading branch information
lilang authored and jojoliang committed Aug 8, 2022
1 parent 2f8b126 commit 01fad12
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 55 deletions.
68 changes: 68 additions & 0 deletions ci_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cos

import (
"context"
"encoding/json"
"encoding/xml"
"net/http"
"net/url"
)

type DocProcessJobInput struct {
Expand Down Expand Up @@ -281,3 +283,69 @@ func (s *CIService) DocPreview(ctx context.Context, name string, opt *DocPreview
resp, err := s.client.send(ctx, &sendOpt)
return resp, err
}

type DocPreviewHTMLOptions struct {
DstType string `url:"dstType,omitempty"`
SrcType string `url:"srcType,omitempty"`
Sign string `url:"sign,omitempty"`
Copyable string `url:"copyable,omitempty"`
HtmlParams *HtmlParams `url:"htmlParams,omitempty"`
Htmlwaterword string `url:"htmlwaterword,omitempty"`
Htmlfillstyle string `url:"htmlfillstyle,omitempty"`
Htmlfront string `url:"htmlfront,omitempty"`
Htmlrotate string `url:"htmlrotate,omitempty"`
Htmlhorizontal string `url:"htmlhorizontal,omitempty"`
Htmlvertical string `url:"htmlvertical,omitempty"`
}

func (c *HtmlParams) EncodeValues(key string, v *url.Values) error {
config, err := json.Marshal(c)
if err != nil {
return err
}
v.Add("htmlParams", string(config))
return nil
}

type HtmlParams struct {
CommonOptions *HtmlCommonParams `json:"commonOptions,omitempty"`
WordOptions *HtmlWordParams `json:"wordOptions,omitempty"`
PdfOptions *HtmlPdfParams `json:"pdfOptions,omitempty"`
PptOptions *HtmlPptParams `json:"pptOptions,omitempty"`
}

type HtmlCommonParams struct {
IsShowTopArea bool `json:"isShowTopArea"`
IsShowHeader bool `json:"isShowHeader"`
IsBrowserViewFullscreen bool `json:"isBrowserViewFullscreen"`
IsIframeViewFullscreen bool `json:"isIframeViewFullscreen"`
}

type HtmlWordParams struct {
IsShowDocMap bool `json:"isShowDocMap"`
IsBestScale bool `json:"isBestScale"`
IsShowBottomStatusBar bool `json:"isShowBottomStatusBar"`
}

type HtmlPdfParams struct {
IsShowComment bool `json:"isShowComment"`
IsInSafeMode bool `json:"isInSafeMode"`
IsShowBottomStatusBar bool `json:"isShowBottomStatusBar"`
}

type HtmlPptParams struct {
IsShowBottomStatusBar bool `json:"isShowBottomStatusBar"`
}

// 文档转html https://cloud.tencent.com/document/product/460/52518
func (s *CIService) DocPreviewHTML(ctx context.Context, name string, opt *DocPreviewHTMLOptions) (*Response, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/" + encodeURIComponent(name) + "?ci-process=doc-preview",
optQuery: opt,
method: http.MethodGet,
disableCloseBody: true,
}
resp, err := s.client.send(ctx, &sendOpt)
return resp, err
}
58 changes: 58 additions & 0 deletions example/CI/doc_preview/bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("https://test-1234567890.cos.ap-beijing.myqcloud.com")
cu, _ := url.Parse("https://test-1234567890.ci.ap-beijing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})

BucketsOpt := &cos.DescribeDocProcessBucketsOptions{
Regions: "ap-chongqing",
BucketName: "test",
}
BucketsRes, _, err := c.CI.DescribeDocProcessBuckets(context.Background(), BucketsOpt)
log_status(err)
fmt.Printf("%+v\n", BucketsRes)
}
77 changes: 77 additions & 0 deletions example/CI/doc_preview/doc_preview_html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: false,
},
},
})

key := "input/doc_preview.ppt"
opt := &cos.DocPreviewHTMLOptions{
DstType: "html",
SrcType: "ppt",
Copyable: "1",
HtmlParams: &cos.HtmlParams{
CommonOptions: &cos.HtmlCommonParams{
IsShowTopArea: false,
},
PptOptions: &cos.HtmlPptParams{
IsShowBottomStatusBar: true,
},
},
Htmlwaterword: "5pWw5o2u5LiH6LGhLeaWh+aho+mihOiniA==",
Htmlfillstyle: "cmdiYSgxMDIsMjA0LDI1NSwwLjMp", // rgba(102,204,255,0.3)
Htmlfront: "Ym9sZCAyNXB4IFNlcmlm", // bold 25px Serif
Htmlrotate: "315",
Htmlhorizontal: "50",
Htmlvertical: "100",
}
resp, err := c.CI.DocPreviewHTML(context.Background(), key, opt)
log_status(err)
fd, _ := os.OpenFile("doc_preview.html", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
io.Copy(fd, resp.Body)
fd.Close()
}
61 changes: 61 additions & 0 deletions example/CI/doc_preview/get_doc_preview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: false,
},
},
})

key := "input/doc_preview.ppt"
opt := &cos.DocPreviewOptions{
DstType: "pdf",
}
resp, err := c.CI.DocPreview(context.Background(), key, opt)
log_status(err)
fd, _ := os.OpenFile("doc_preview.pdf", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
io.Copy(fd, resp.Body)
fd.Close()
}

0 comments on commit 01fad12

Please sign in to comment.