Skip to content

Commit

Permalink
change: remove inferred domain settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Wsine committed Apr 9, 2024
1 parent 404356f commit fde86fa
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 109 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ clean: ## Clean build bundles
.PHONY: format
format:
gofmt -l -w .

.PHONY: all
all: build server
@echo "Build all done"
5 changes: 2 additions & 3 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

type DownloadOpts struct {
allowHost string
outputDir string
dump bool
}
Expand All @@ -24,7 +23,7 @@ var downloadOpts = DownloadOpts{}

func handleDownloadCommand(url string, opts *DownloadOpts) error {
// Validate the url to download
domain, docType, docToken, err := utils.ValidateDownloadURL(url, opts.allowHost)
docType, docToken, err := utils.ValidateDownloadURL(url)
utils.CheckErr(err)
fmt.Println("Captured document token:", docToken)

Expand All @@ -38,7 +37,7 @@ func handleDownloadCommand(url string, opts *DownloadOpts) error {
ctx := context.WithValue(context.Background(), "output", config.Output)

client := core.NewClient(
config.Feishu.AppId, config.Feishu.AppSecret, domain,
config.Feishu.AppId, config.Feishu.AppSecret,
)

// for a wiki page, we need to renew docType and docToken first
Expand Down
58 changes: 0 additions & 58 deletions cmd/dump.go

This file was deleted.

6 changes: 0 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ func main() {
Usage: "Specify the output directory for the markdown files",
Destination: &downloadOpts.outputDir,
},
&cli.StringFlag{
Name: "allowHost",
Value: "",
Usage: "Additional allow host for the OPEN API",
Destination: &downloadOpts.allowHost,
},
&cli.BoolFlag{
Name: "dump",
Value: false,
Expand Down
3 changes: 1 addition & 2 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ type Client struct {
larkClient *lark.Lark
}

func NewClient(appID, appSecret, domain string) *Client {
func NewClient(appID, appSecret string) *Client {
return &Client{
larkClient: lark.New(
lark.WithAppCredential(appID, appSecret),
lark.WithOpenBaseURL("https://open."+domain),
lark.WithTimeout(60*time.Second),
),
}
Expand Down
8 changes: 4 additions & 4 deletions core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func getIdAndSecretFromEnv(t *testing.T) (string, string) {

func TestNewClient(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
c := core.NewClient(appID, appSecret)
if c == nil {
t.Errorf("Error creating DocClient")
}
}

func TestDownloadImage(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
c := core.NewClient(appID, appSecret)
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM"
filename, err := c.DownloadImage(
context.Background(),
Expand All @@ -63,7 +63,7 @@ func TestDownloadImage(t *testing.T) {

func TestGetDocxContent(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
c := core.NewClient(appID, appSecret)
docx, blocks, err := c.GetDocxContent(
context.Background(),
"doxcnXhd93zqoLnmVPGIPTy7AFe",
Expand All @@ -83,7 +83,7 @@ func TestGetDocxContent(t *testing.T) {

func TestGetWikiNodeInfo(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
c := core.NewClient(appID, appSecret)
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah"
node, err := c.GetWikiNodeInfo(context.Background(), token)
if err != nil {
Expand Down
18 changes: 7 additions & 11 deletions utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"net/url"
"regexp"
"strings"

"github.com/pkg/errors"
)
Expand All @@ -15,16 +14,13 @@ func UnescapeURL(rawURL string) string {
return rawURL
}

func ValidateDownloadURL(url, allowHost string) (string, string, string, error) {
hosts := []string{"feishu.cn", "larksuite.com"}
if allowHost != "" {
hosts = append(hosts, allowHost)
}

reg := regexp.MustCompile("^https://([\\w-]+.)?(" + strings.Join(hosts, "|") + ")/(docs|docx|wiki)/([a-zA-Z0-9]+)")
func ValidateDownloadURL(url string) (string, string, error) {
reg := regexp.MustCompile("^https://[\\w-.]+/(docx|wiki)/([a-zA-Z0-9]+)")
matchResult := reg.FindStringSubmatch(url)
if matchResult == nil || len(matchResult) != 5 {
return "", "", "", errors.Errorf("Invalid feishu/larksuite/allowHost URL format")
if matchResult == nil || len(matchResult) != 3 {
return "", "", errors.Errorf("Invalid feishu/larksuite URL format")
}
return matchResult[2], matchResult[3], matchResult[4], nil
docType := matchResult[1]
docToken := matchResult[2]
return docType, docToken, nil
}
34 changes: 11 additions & 23 deletions utils/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,53 +36,41 @@ func TestUnescapeURL(t *testing.T) {
}

func TestValidateDownloadURL(t *testing.T) {
type Args struct {
url string
allowHost string
}
tests := []struct {
name string
args Args
url string
noErr bool
}{
{
name: "validate feishu url success",
args: Args{
url: "https://sample.feishu.cn/docx/doccnByZP6puODElAYySJkPIfUb",
allowHost: "",
},
url: "https://sample.feishu.cn/docx/doccnByZP6puODElAYySJkPIfUb",
noErr: true,
},
{
name: "validate larksuite url success",
args: Args{
url: "https://sample.larksuite.com/wiki/doccnByZP6puODElAYySJkPIfUb",
allowHost: "",
},
noErr: true,
},
{
name: "validate feishu url success with allow host",
args: Args{
url: "https://f.mioffice.cn/docx/doccnByZP6puODElAYySJkPIfUb",
allowHost: "f.mioffice.cn",
},
name: "validate larksuite url success",
url: "https://sample.sg.larksuite.com/wiki/doccnByZP6puODElAYySJkPIfUb",
noErr: true,
},
{
name: "validate feishu url success",
url: "https://sample.f.mioffice.cn/docx/doccnByZP6puODElAYySJkPIfUb",
noErr: true,
},
{
name: "validate arbitrary url failed",
args: Args{
url: "https://google.com",
allowHost: "",
},
noErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url, allowHost := tt.args.url, tt.args.allowHost
if _, _, _, got := ValidateDownloadURL(url, allowHost); (got == nil) != tt.noErr {
t.Errorf("ValidateDownloadURL(%v, %v)", url, allowHost)
if _, _, got := ValidateDownloadURL(tt.url); (got == nil) != tt.noErr {
t.Errorf("ValidateDownloadURL(%v)", tt.url)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions web/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func downloadHandler(c *gin.Context) {
}

// Validate the url to download
domain, docType, docToken, err := utils.ValidateDownloadURL(feishu_docx_url, "")
docType, docToken, err := utils.ValidateDownloadURL(feishu_docx_url)
fmt.Println("Captured document token:", docToken)

// Create client with context
Expand All @@ -36,7 +36,7 @@ func downloadHandler(c *gin.Context) {
os.Getenv("FEISHU_APP_SECRET"),
)
client := core.NewClient(
config.Feishu.AppId, config.Feishu.AppSecret, domain,
config.Feishu.AppId, config.Feishu.AppSecret,
)

// Process the download
Expand Down

0 comments on commit fde86fa

Please sign in to comment.