Skip to content

Commit 86373fe

Browse files
committed
change: remove inferred domain settings
1 parent 2e1e70d commit 86373fe

File tree

9 files changed

+36
-114
lines changed

9 files changed

+36
-114
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ clean: ## Clean build bundles
3131
.PHONY: format
3232
format:
3333
gofmt -l -w .
34+
35+
.PHONY: all
36+
all: build server
37+
@echo "Build all done"

cmd/download.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
)
1616

1717
type DownloadOpts struct {
18-
allowHost string
1918
outputDir string
2019
dump bool
2120
}
@@ -24,7 +23,7 @@ var downloadOpts = DownloadOpts{}
2423

2524
func handleDownloadCommand(url string, opts *DownloadOpts) error {
2625
// Validate the url to download
27-
domain, docType, docToken, err := utils.ValidateDownloadURL(url, opts.allowHost)
26+
docType, docToken, err := utils.ValidateDownloadURL(url)
2827
utils.CheckErr(err)
2928
fmt.Println("Captured document token:", docToken)
3029

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

4039
client := core.NewClient(
41-
config.Feishu.AppId, config.Feishu.AppSecret, domain,
40+
config.Feishu.AppId, config.Feishu.AppSecret,
4241
)
4342

4443
// for a wiki page, we need to renew docType and docToken first

cmd/dump.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

cmd/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ func main() {
5353
Usage: "Specify the output directory for the markdown files",
5454
Destination: &downloadOpts.outputDir,
5555
},
56-
&cli.StringFlag{
57-
Name: "allowHost",
58-
Value: "",
59-
Usage: "Additional allow host for the OPEN API",
60-
Destination: &downloadOpts.allowHost,
61-
},
6256
&cli.BoolFlag{
6357
Name: "dump",
6458
Value: false,

core/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ type Client struct {
1616
larkClient *lark.Lark
1717
}
1818

19-
func NewClient(appID, appSecret, domain string) *Client {
19+
func NewClient(appID, appSecret string) *Client {
2020
return &Client{
2121
larkClient: lark.New(
2222
lark.WithAppCredential(appID, appSecret),
23-
lark.WithOpenBaseURL("https://open."+domain),
2423
lark.WithTimeout(60*time.Second),
2524
),
2625
}

core/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ func getIdAndSecretFromEnv(t *testing.T) (string, string) {
3434

3535
func TestNewClient(t *testing.T) {
3636
appID, appSecret := getIdAndSecretFromEnv(t)
37-
c := core.NewClient(appID, appSecret, "feishu.cn")
37+
c := core.NewClient(appID, appSecret)
3838
if c == nil {
3939
t.Errorf("Error creating DocClient")
4040
}
4141
}
4242

4343
func TestDownloadImage(t *testing.T) {
4444
appID, appSecret := getIdAndSecretFromEnv(t)
45-
c := core.NewClient(appID, appSecret, "feishu.cn")
45+
c := core.NewClient(appID, appSecret)
4646
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM"
4747
filename, err := c.DownloadImage(
4848
context.Background(),
@@ -63,7 +63,7 @@ func TestDownloadImage(t *testing.T) {
6363

6464
func TestGetDocxContent(t *testing.T) {
6565
appID, appSecret := getIdAndSecretFromEnv(t)
66-
c := core.NewClient(appID, appSecret, "feishu.cn")
66+
c := core.NewClient(appID, appSecret)
6767
docx, blocks, err := c.GetDocxContent(
6868
context.Background(),
6969
"doxcnXhd93zqoLnmVPGIPTy7AFe",
@@ -83,7 +83,7 @@ func TestGetDocxContent(t *testing.T) {
8383

8484
func TestGetWikiNodeInfo(t *testing.T) {
8585
appID, appSecret := getIdAndSecretFromEnv(t)
86-
c := core.NewClient(appID, appSecret, "feishu.cn")
86+
c := core.NewClient(appID, appSecret)
8787
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah"
8888
node, err := c.GetWikiNodeInfo(context.Background(), token)
8989
if err != nil {

utils/url.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package utils
33
import (
44
"net/url"
55
"regexp"
6-
"strings"
76

87
"github.com/pkg/errors"
98
)
@@ -15,16 +14,13 @@ func UnescapeURL(rawURL string) string {
1514
return rawURL
1615
}
1716

18-
func ValidateDownloadURL(url, allowHost string) (string, string, string, error) {
19-
hosts := []string{"feishu.cn", "larksuite.com"}
20-
if allowHost != "" {
21-
hosts = append(hosts, allowHost)
22-
}
23-
24-
reg := regexp.MustCompile("^https://([\\w-]+.)?(" + strings.Join(hosts, "|") + ")/(docs|docx|wiki)/([a-zA-Z0-9]+)")
17+
func ValidateDownloadURL(url string) (string, string, error) {
18+
reg := regexp.MustCompile("^https://[\\w-.]+/(docx|wiki)/([a-zA-Z0-9]+)")
2519
matchResult := reg.FindStringSubmatch(url)
26-
if matchResult == nil || len(matchResult) != 5 {
27-
return "", "", "", errors.Errorf("Invalid feishu/larksuite/allowHost URL format")
20+
if matchResult == nil || len(matchResult) != 3 {
21+
return "", "", errors.Errorf("Invalid feishu/larksuite URL format")
2822
}
29-
return matchResult[2], matchResult[3], matchResult[4], nil
23+
docType := matchResult[1]
24+
docToken := matchResult[2]
25+
return docType, docToken, nil
3026
}

utils/url_test.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,41 @@ func TestUnescapeURL(t *testing.T) {
3636
}
3737

3838
func TestValidateDownloadURL(t *testing.T) {
39-
type Args struct {
40-
url string
41-
allowHost string
42-
}
4339
tests := []struct {
4440
name string
45-
args Args
41+
url string
4642
noErr bool
4743
}{
4844
{
49-
name: "validate feishu url success",
50-
args: Args{
51-
url: "https://sample.feishu.cn/docx/doccnByZP6puODElAYySJkPIfUb",
52-
allowHost: "",
53-
},
45+
name: "validate feishu url success",
46+
url: "https://sample.feishu.cn/docx/doccnByZP6puODElAYySJkPIfUb",
47+
noErr: true,
48+
},
49+
{
50+
name: "validate larksuite url success",
51+
url: "https://sample.larksuite.com/wiki/doccnByZP6puODElAYySJkPIfUb",
5452
noErr: true,
5553
},
5654
{
57-
name: "validate larksuite url success",
58-
args: Args{
59-
url: "https://sample.larksuite.com/wiki/doccnByZP6puODElAYySJkPIfUb",
60-
allowHost: "",
61-
},
55+
name: "validate larksuite url success",
56+
url: "https://sample.sg.larksuite.com/wiki/doccnByZP6puODElAYySJkPIfUb",
6257
noErr: true,
6358
},
6459
{
65-
name: "validate feishu url success with allow host",
66-
args: Args{
67-
url: "https://f.mioffice.cn/docx/doccnByZP6puODElAYySJkPIfUb",
68-
allowHost: "f.mioffice.cn",
69-
},
60+
name: "validate feishu url success",
61+
url: "https://sample.f.mioffice.cn/docx/doccnByZP6puODElAYySJkPIfUb",
7062
noErr: true,
7163
},
7264
{
73-
name: "validate arbitrary url failed",
74-
args: Args{
75-
url: "https://google.com",
76-
allowHost: "",
77-
},
65+
name: "validate arbitrary url failed",
66+
url: "https://google.com",
7867
noErr: false,
7968
},
8069
}
8170
for _, tt := range tests {
8271
t.Run(tt.name, func(t *testing.T) {
83-
url, allowHost := tt.args.url, tt.args.allowHost
84-
if _, _, _, got := ValidateDownloadURL(url, allowHost); (got == nil) != tt.noErr {
85-
t.Errorf("ValidateDownloadURL(%v, %v)", url, allowHost)
72+
if _, _, got := ValidateDownloadURL(tt.url); (got == nil) != tt.noErr {
73+
t.Errorf("ValidateDownloadURL(%v)", tt.url)
8674
}
8775
})
8876
}

web/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func downloadHandler(c *gin.Context) {
2626
}
2727

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

3232
// Create client with context
@@ -36,7 +36,7 @@ func downloadHandler(c *gin.Context) {
3636
os.Getenv("FEISHU_APP_SECRET"),
3737
)
3838
client := core.NewClient(
39-
config.Feishu.AppId, config.Feishu.AppSecret, domain,
39+
config.Feishu.AppId, config.Feishu.AppSecret,
4040
)
4141

4242
// Process the download

0 commit comments

Comments
 (0)