forked from googlecodelabs/tools
/
fetch.go
483 lines (437 loc) · 12.2 KB
/
fetch.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2016-2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fetch
import (
"encoding/json"
"errors"
"fmt"
"hash/crc64"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/googlecodelabs/tools/claat/fetch/drive/auth"
"github.com/googlecodelabs/tools/claat/parser"
"github.com/googlecodelabs/tools/claat/types"
"github.com/googlecodelabs/tools/claat/util"
)
const (
// supported codelab source types must be registered parsers
// TODO: define these in claat/parser/..., e.g. in parser/gdoc
// alternate TODO: make this an iota-based enum?
SrcInvalid srcType = ""
SrcGoogleDoc srcType = "gdoc" // Google Docs doc
SrcMarkdown srcType = "md" // Markdown text
// driveAPI is a base URL for Drive API
driveAPI = "https://www.googleapis.com/drive/v3"
)
// TODO: create an enum for use with "nometa" for readability's sake
// srcType is codelab source type
type srcType string
// resource is a codelab resource, loaded from local file
// or fetched from remote location.
type resource struct {
typ srcType // source type
body io.ReadCloser // resource body
mod time.Time // last update of content
}
// codelab wraps types.Codelab, while adding source type
// and modified timestamp fields.
type codelab struct {
*types.Codelab
Typ srcType // source type
Mod time.Time // last modified timestamp
}
type MemoryFetcher struct {
passMetadata map[string]bool
mdParser parser.MarkdownParser
}
func NewMemoryFetcher(pm map[string]bool, mdp parser.MarkdownParser) *MemoryFetcher {
return &MemoryFetcher{
passMetadata: pm,
mdParser: mdp,
}
}
func (m *MemoryFetcher) SlurpCodelab(rc io.ReadCloser) (*codelab, error) {
r := &resource{
body: rc,
typ: SrcMarkdown,
mod: time.Now(),
}
defer r.body.Close()
opts := *parser.NewOptions(m.mdParser)
opts.PassMetadata = m.passMetadata
clab, err := parser.Parse(string(r.typ), r.body, opts)
if err != nil {
return nil, err
}
return &codelab{
Codelab: clab,
Typ: r.typ,
Mod: r.mod,
}, nil
}
type Fetcher struct {
authHelper *auth.Helper
authToken string
crcTable *crc64.Table
mdParser parser.MarkdownParser
passMetadata map[string]bool
roundTripper http.RoundTripper
}
func NewFetcher(at string, pm map[string]bool, rt http.RoundTripper, mdp parser.MarkdownParser) (*Fetcher, error) {
return &Fetcher{
authHelper: nil,
authToken: at,
crcTable: crc64.MakeTable(crc64.ECMA),
mdParser: mdp,
passMetadata: pm,
roundTripper: rt,
}, nil
}
// SlurpCodelab retrieves and parses codelab source.
// It takes the source, plus an auth token and a set of extra metadata to pass along.
// It returns parsed codelab and its source type.
//
// The function will also fetch and parse fragments included
// with types.ImportNode.
func (f *Fetcher) SlurpCodelab(src string) (*codelab, error) {
_, err := os.Stat(src)
// Only setup oauth if this source is not a local file.
if os.IsNotExist(err) {
if f.authHelper == nil {
f.authHelper, err = auth.NewHelper(f.authToken, auth.ProviderGoogle, f.roundTripper)
if err != nil {
return nil, err
}
}
}
res, err := f.fetch(src)
if err != nil {
return nil, err
}
defer res.body.Close()
opts := *parser.NewOptions(f.mdParser)
opts.PassMetadata = f.passMetadata
clab, err := parser.Parse(string(res.typ), res.body, opts)
if err != nil {
return nil, err
}
// fetch imports and parse them as fragments
var imports []*types.ImportNode
for _, st := range clab.Steps {
imports = append(imports, types.ImportNodes(st.Content.Nodes)...)
}
ch := make(chan error, len(imports))
defer close(ch)
for _, imp := range imports {
go func(n *types.ImportNode) {
frag, err := f.slurpFragment(n.URL)
if err != nil {
ch <- fmt.Errorf("%s: %v", n.URL, err)
return
}
n.Content.Nodes = frag
ch <- nil
}(imp)
}
for range imports {
if err := <-ch; err != nil {
return nil, err
}
}
v := &codelab{
Codelab: clab,
Typ: res.typ,
Mod: res.mod,
}
return v, nil
}
func (f *Fetcher) SlurpImages(src, dir string, steps []*types.Step) (map[string]string, error) {
// make sure img dir exists
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
type res struct {
url, file string
err error
}
ch := make(chan *res, 100)
defer close(ch)
var count int
for _, st := range steps {
nodes := types.ImageNodes(st.Content.Nodes)
count += len(nodes)
for _, n := range nodes {
go func(n *types.ImageNode) {
url := n.Src
file, err := f.slurpBytes(src, dir, url)
if err == nil {
n.Src = filepath.Join(util.ImgDirname, file)
}
ch <- &res{url, file, err}
}(n)
}
}
imap := make(map[string]string, count)
var errStr string
for i := 0; i < count; i++ {
r := <-ch
imap[r.file] = r.url
if r.err != nil {
errStr += fmt.Sprintf("%s => %s: %v\n", r.url, r.file, r.err)
}
}
if len(errStr) > 0 {
return nil, errors.New(errStr)
}
return imap, nil
}
func (f *Fetcher) slurpBytes(codelabSrc, dir, imgURL string) (string, error) {
// images can be local in Markdown cases or remote.
// Only proceed a simple copy on local reference.
var b []byte
var ext string
u, err := url.Parse(imgURL)
if err != nil {
return "", err
}
// If the codelab source is being downloaded from the network, then we should interpret
// the image URL in the same way.
srcUrl, err := url.Parse(codelabSrc)
if err == nil && srcUrl.Host != "" {
u = srcUrl.ResolveReference(u)
}
if u.Host == "" {
if imgURL, err = restrictPathToParent(imgURL, filepath.Dir(codelabSrc)); err != nil {
return "", err
}
b, err = ioutil.ReadFile(imgURL)
ext = filepath.Ext(imgURL)
} else {
b, err = f.slurpRemoteBytes(u.String(), 5)
if string(b[6:10]) == "JFIF" {
ext = ".jpeg"
} else if string(b[0:3]) == "GIF" {
ext = ".gif"
} else {
ext = ".png"
}
}
if err != nil {
return "", err
}
crc := crc64.Checksum(b, f.crcTable)
file := fmt.Sprintf("%x%s", crc, ext)
dst := filepath.Join(dir, file)
return file, ioutil.WriteFile(dst, b, 0644)
}
func (f *Fetcher) slurpFragment(url string) ([]types.Node, error) {
res, err := f.fetch(url)
if err != nil {
return nil, err
}
defer res.body.Close()
opts := *parser.NewOptions(f.mdParser)
opts.PassMetadata = f.passMetadata
return parser.ParseFragment(string(res.typ), res.body, opts)
}
// fetch retrieves codelab doc either from local disk
// or a remote location.
// The caller is responsible for closing returned stream.
func (f *Fetcher) fetch(name string) (*resource, error) {
fi, err := os.Stat(name)
if os.IsNotExist(err) {
return f.fetchRemote(name, false)
}
r, err := os.Open(name)
if err != nil {
return nil, err
}
return &resource{
body: r,
typ: SrcMarkdown,
mod: fi.ModTime(),
}, nil
}
// fetchRemote retrieves resource r from the network.
//
// If urlStr is not a URL, i.e. does not have the host part, it is considered to be
// a Google Doc ID and fetched accordingly. Otherwise, a simple GET request
// is used to retrieve the contents.
//
// The caller is responsible for closing returned stream.
// If nometa is true, resource.mod may have zero value.
func (f *Fetcher) fetchRemote(urlStr string, nometa bool) (*resource, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
if u.Host == "" || u.Host == "docs.google.com" {
return f.fetchDriveFile(urlStr, nometa)
}
return f.fetchRemoteFile(urlStr)
}
// fetchRemoteFile retrieves codelab resource from url.
// It is a special case of fetchRemote function.
func (f *Fetcher) fetchRemoteFile(url string) (*resource, error) {
res, err := retryGet(f.authHelper.DriveClient(), url, 3)
if err != nil {
return nil, err
}
t, err := http.ParseTime(res.Header.Get("last-modified"))
if err != nil {
t = time.Now()
}
return &resource{
body: res.Body,
mod: t,
typ: SrcMarkdown,
}, nil
}
// fetchDriveFile uses Drive API to retrieve HTML representation of a Google Doc.
// See https://developers.google.com/drive/web/manage-downloads#downloading_google_documents
// for more details.
//
// If nometa is true, resource.mod will have zero value.
func (f *Fetcher) fetchDriveFile(id string, nometa bool) (*resource, error) {
id = gdocID(id)
exportURL := gdocExportURL(id)
if nometa {
res, err := retryGet(f.authHelper.DriveClient(), exportURL, 7)
if err != nil {
return nil, err
}
return &resource{body: res.Body, typ: SrcGoogleDoc}, nil
}
q := url.Values{
"fields": {"id,mimeType,modifiedTime"},
"supportsTeamDrives": {"true"},
}
u := fmt.Sprintf("%s/files/%s?%s", driveAPI, id, q.Encode())
res, err := retryGet(f.authHelper.DriveClient(), u, 7)
if err != nil {
return nil, err
}
defer res.Body.Close()
meta := &struct {
ID string `json:"id"`
MimeType string `json:"mimeType"`
Modified time.Time `json:"modifiedTime"`
}{}
if err := json.NewDecoder(res.Body).Decode(meta); err != nil {
return nil, err
}
if meta.MimeType != "application/vnd.google-apps.document" {
return nil, fmt.Errorf("%s: invalid mime type: %s", id, meta.MimeType)
}
if res, err = retryGet(f.authHelper.DriveClient(), exportURL, 7); err != nil {
return nil, err
}
return &resource{
body: res.Body,
mod: meta.Modified,
typ: SrcGoogleDoc,
}, nil
}
func (f *Fetcher) slurpRemoteBytes(url string, n int) ([]byte, error) {
res, err := retryGet(f.authHelper.DriveClient(), url, n)
if err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// retryGet tries to GET specified url up to n times.
// Attempts are spaced out with exponential backoff.
// Default client will be used if not provided.
func retryGet(client *http.Client, url string, n int) (*http.Response, error) {
if client == nil {
client = http.DefaultClient
}
for i := 0; i <= n; i++ {
if i > 0 {
t := time.Duration((math.Pow(2, float64(i)) + rand.Float64()) * float64(time.Second))
time.Sleep(t)
}
res, err := client.Get(url)
// return early with a good response
// the rest is error handling
if err == nil && res.StatusCode == http.StatusOK {
return res, nil
}
// sometimes Drive API wouldn't even start a response,
// we get net/http: TLS handshake timeout instead:
// consider this a temporary failure and retry again
if err != nil {
continue
}
// otherwise, decode error response and check for "rate limit"
defer res.Body.Close()
var erres struct {
Error struct {
Errors []struct{ Reason string }
}
}
b, _ := ioutil.ReadAll(res.Body)
json.Unmarshal(b, &erres)
var rateLimit bool
for _, e := range erres.Error.Errors {
if e.Reason == "rateLimitExceeded" || e.Reason == "userRateLimitExceeded" {
rateLimit = true
break
}
}
// this is neither a rate limit error, nor a server error:
// retrying is useless
if !rateLimit && res.StatusCode < http.StatusInternalServerError {
return nil, fmt.Errorf("fetch %s: %s; %s", url, res.Status, b)
}
}
return nil, fmt.Errorf("%s: failed after %d retries", url, n)
}
func gdocID(url string) string {
const s = "/document/d/"
if i := strings.Index(url, s); i >= 0 {
url = url[i+len(s):]
}
if i := strings.IndexRune(url, '/'); i > 0 {
url = url[:i]
}
return url
}
func gdocExportURL(id string) string {
return fmt.Sprintf("%s/files/%s/export?mimeType=text/html", driveAPI, id)
}
// restrictPathToParent will ensure that assetPath is in parent.
// It will thus return an absolute path to the asset.
func restrictPathToParent(assetPath, parent string) (string, error) {
parent, err := filepath.Abs(parent)
if err != nil {
return "", err
}
if !filepath.IsAbs(assetPath) {
assetPath = filepath.Join(parent, assetPath)
}
if !strings.HasPrefix(assetPath, parent) {
return "", fmt.Errorf("%s isn't a subdirectory of %s", assetPath, parent)
}
return assetPath, nil
}