forked from kjk/notionapi
/
get_signed_file_urls.go
176 lines (158 loc) · 4.83 KB
/
get_signed_file_urls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package notionapi
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const (
signedURLPrefix = "https://www.notion.so/signed/"
s3URLPrefix = "https://s3-us-west-2.amazonaws.com/secure.notion-static.com/"
//s3URLPrefixEncoded = "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/"
)
/*
{"urls": [
{
"url" : "https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e5661303-82e1-43e4-be8e-662d1598cd53/untitled",
"permissionRecord": {
"table":"block",
"id":"845fd39c-f048-4d41-a0f8-a83851d17afd"
}
}
]}
*/
type getSignedURLPermissionRecord struct {
Table string `json:"table"`
ID string `json:"id"`
}
type getSignedFileURL struct {
URL string `json:"url"`
Permission getSignedURLPermissionRecord `json:"permissionRecord"`
}
type getSignedFileUrlsRequest struct {
Urls []getSignedFileURL `json:"urls"`
}
// GetSignedFileUrlsResponse is a response of GetSignedFileUrls()
type GetSignedFileUrlsResponse struct {
SignedUrls []string `json:"signedUrls"`
RawJSON map[string]interface{} `json:"-"`
}
// GetSignedFileUrls executes a raw API call /api/v3/getSignedFileUrls
// For files (e.g. images) stored in Notion we need to get a temporary
// download url (which will be valid for only a short period of time)
func (c *Client) GetSignedFileUrls(urls []string, blockIDs []string) (*GetSignedFileUrlsResponse, error) {
req := &getSignedFileUrlsRequest{}
for i, url := range urls {
perm := getSignedURLPermissionRecord{
Table: "block",
ID: blockIDs[i],
}
fu := getSignedFileURL{URL: url, Permission: perm}
req.Urls = append(req.Urls, fu)
}
apiURL := "/api/v3/getSignedFileUrls"
var rsp GetSignedFileUrlsResponse
var err error
rsp.RawJSON, err = doNotionAPI(c, apiURL, req, &rsp)
if err != nil {
return nil, err
}
return &rsp, nil
}
// DownloadFileResponse is a result of DownloadFile()
type DownloadFileResponse struct {
URL string
CacheFileName string
Data []byte
Header http.Header
}
// sometimes image url in "source" is not accessible but can
// be accessed when proxied via notion server as
// www.notion.so/image/${source}
// This also allows resizing via ?width=${n} arguments
//
// from: /images/page-cover/met_vincent_van_gogh_cradle.jpg
// =>
// https://www.notion.so/image/https%3A%2F%2Fwww.notion.so%2Fimages%2Fpage-cover%2Fmet_vincent_van_gogh_cradle.jpg?width=3290
func maybeProxyImageURL(uri string) string {
if strings.HasPrefix(uri, s3URLPrefix) {
return signedURLPrefix + url.PathEscape(uri)
}
// don't proxy external images
if !strings.Contains(uri, "notion.so") {
return uri
}
if strings.Contains(uri, "//www.notion.so/image/") {
return uri
}
// if the url has https://, it's already in s3.
// If not, it's only a relative URL (like those for built-in
// cover pages)
if !strings.HasPrefix(uri, "https://") {
uri = "https://www.notion.so" + uri
}
return "https://www.notion.so/image/" + url.PathEscape(uri)
}
func (c *Client) maybeSignImageURL(uri string, blockID string) string {
if !strings.HasPrefix(uri, s3URLPrefix) {
return maybeProxyImageURL(uri)
}
/* notionapi-py does:
if url.startswith(S3_URL_PREFIX):
url = SIGNED_URL_PREFIX + quote_plus(url)
if client:
url = client.session.head(url).headers.get("Location")
*/
rsp, err := c.GetSignedFileUrls([]string{uri}, []string{blockID})
if err != nil {
return uri
}
return rsp.SignedUrls[0]
}
func (c *Client) downloadFile(uri string) (*DownloadFileResponse, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
//fmt.Printf("DownloadFile: NewRequest() for '%s' failed with '%s'\n", uri, err)
return nil, err
}
if c.AuthToken != "" {
req.Header.Set("cookie", fmt.Sprintf("token_v2=%v", c.AuthToken))
}
httpClient := c.getHTTPClient()
resp, err := httpClient.Do(req)
if err != nil {
//fmt.Printf("DownloadFile: httpClient.Do() for '%s' failed with '%s'\n", uri, err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
//fmt.Printf("DownloadFile: httpClient.Do() for '%s' failed with '%s'\n", uri, resp.Status)
return nil, fmt.Errorf("http GET '%s' failed with status %s", uri, resp.Status)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
return nil, err
}
rsp := &DownloadFileResponse{
Data: buf.Bytes(),
Header: resp.Header,
}
return rsp, nil
}
// DownloadFile downloads a file stored in Notion
func (c *Client) DownloadFile(uri string, blockID string) (*DownloadFileResponse, error) {
//fmt.Printf("DownloadFile: '%s'\n", uri)
// try proxing through www.notion.so/image/
if strings.Contains(uri, "s3.us-west-2.amazonaws.com") {
uri2 := "https://www.notion.so/image/" + url.PathEscape(uri)
res, err := c.downloadFile(uri2)
if err == nil {
return res, nil
}
}
uri2 := c.maybeSignImageURL(uri, blockID)
return c.downloadFile(uri2)
}