MCPcopy
hub / github.com/pocketbase/pocketbase / safeFileFromURL

Function safeFileFromURL

apis/record_auth_with_oauth2.go:484–510  ·  view source on GitHub ↗

safeFileFromURL downloads the file from the specified url (using safeHTTPClient) and creates a new filesystem.File value from its content (limited to DefaultMaxBodySize). @todo Evaluate with the refactoring if worth exporting/replacing filesystem.NewFileFromURL (or redefine as NewUnsafeFileFromURL)

(ctx context.Context, url string)

Source from the content-addressed store, hash-verified

482//
483// @todo Evaluate with the refactoring if worth exporting/replacing filesystem.NewFileFromURL (or redefine as NewUnsafeFileFromURL).
484func safeFileFromURL(ctx context.Context, url string) (*filesystem.File, error) {
485 req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
486 if err != nil {
487 return nil, err
488 }
489
490 client := safeHTTPClient()
491
492 res, err := client.Do(req)
493 if err != nil {
494 return nil, err
495 }
496 defer res.Body.Close()
497
498 if res.StatusCode < 200 || res.StatusCode > 399 {
499 return nil, fmt.Errorf("failed to download url %s (%d)", url, res.StatusCode)
500 }
501
502 body := io.LimitReader(res.Body, DefaultMaxBodySize)
503
504 var buf bytes.Buffer
505 if _, err = io.Copy(&buf, body); err != nil {
506 return nil, err
507 }
508
509 return filesystem.NewFileFromBytes(buf.Bytes(), path.Base(url))
510}

Callers 1

oauth2SubmitFunction · 0.85

Calls 5

NewFileFromBytesFunction · 0.92
safeHTTPClientFunction · 0.85
DoMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…