MCPcopy Index your code
hub / github.com/Threadfin/Threadfin / downloadFileFromServer

Function downloadFileFromServer

src/provider.go:322–384  ·  view source on GitHub ↗
(providerURL string, proxyUrl string)

Source from the content-addressed store, hash-verified

320}
321
322func downloadFileFromServer(providerURL string, proxyUrl string) (filename string, body []byte, err error) {
323 _, err = url.ParseRequestURI(providerURL)
324 if err != nil {
325 return
326 }
327
328 httpClient := &http.Client{}
329
330 if proxyUrl != "" {
331 proxyURL, err := url.Parse(proxyUrl)
332 if err != nil {
333 return "", nil, err
334 }
335
336 httpClient = &http.Client{
337 Transport: &http.Transport{
338 Proxy: http.ProxyURL(proxyURL),
339 },
340 }
341 }
342
343 req, err := http.NewRequest("GET", providerURL, nil)
344 if err != nil {
345 return
346 }
347
348 req.Header.Set("User-Agent", Settings.UserAgent)
349
350 resp, err := httpClient.Do(req)
351 if err != nil {
352 return
353 }
354 defer resp.Body.Close()
355
356 resp.Header.Set("User-Agent", Settings.UserAgent)
357
358 if resp.StatusCode != http.StatusOK {
359 err = fmt.Errorf("%d: %s %s", resp.StatusCode, providerURL, http.StatusText(resp.StatusCode))
360 return
361 }
362
363 // Get filename from the header
364 var index = strings.Index(resp.Header.Get("Content-Disposition"), "filename")
365
366 if index > -1 {
367 var headerFilename = resp.Header.Get("Content-Disposition")[index:]
368 var value = strings.Split(headerFilename, `=`)
369 var f = strings.Replace(value[1], `"`, "", -1)
370 f = strings.Replace(f, `;`, "", -1)
371 filename = f
372 showInfo("Header filename:" + filename)
373 } else {
374 var cleanFilename = strings.SplitN(getFilenameFromPath(providerURL), "?", 2)
375 filename = cleanFilename[0]
376 }
377
378 body, err = io.ReadAll(resp.Body)
379 if err != nil {

Callers 1

getProviderDataFunction · 0.85

Calls 2

showInfoFunction · 0.85
getFilenameFromPathFunction · 0.70

Tested by

no test coverage detected