MCPcopy Index your code
hub / github.com/pocketbase/pocketbase / NewFileFromURL

Function NewFileFromURL

tools/filesystem/file.go:101–124  ·  view source on GitHub ↗

NewFileFromURL creates a new File from the provided url by downloading the resource and load it as BytesReader. Example ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() file, err := filesystem.NewFileFromURL(ctx, "https://example.com/image.png")

(ctx context.Context, url string)

Source from the content-addressed store, hash-verified

99//
100// file, err := filesystem.NewFileFromURL(ctx, "https://example.com/image.png")
101func NewFileFromURL(ctx context.Context, url string) (*File, error) {
102 req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
103 if err != nil {
104 return nil, err
105 }
106
107 res, err := http.DefaultClient.Do(req)
108 if err != nil {
109 return nil, err
110 }
111 defer res.Body.Close()
112
113 if res.StatusCode < 200 || res.StatusCode > 399 {
114 return nil, fmt.Errorf("failed to download url %s (%d)", url, res.StatusCode)
115 }
116
117 var buf bytes.Buffer
118
119 if _, err = io.Copy(&buf, res.Body); err != nil {
120 return nil, err
121 }
122
123 return NewFileFromBytes(buf.Bytes(), path.Base(url))
124}
125
126// -------------------------------------------------------------------
127

Callers 2

BindFilesystemFunction · 0.92

Calls 4

NewFileFromBytesFunction · 0.85
DoMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.65

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…