github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/core/download.go (about)

     1  package core
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  var httpClient = &http.Client{Timeout: time.Minute}
    10  
    11  // DownloadBookmark downloads bookmarked page from specified URL.
    12  // Return response body, make sure to close it later.
    13  func DownloadBookmark(url string) (io.ReadCloser, string, error) {
    14  	// Prepare download request
    15  	req, err := http.NewRequest("GET", url, nil)
    16  	if err != nil {
    17  		return nil, "", err
    18  	}
    19  
    20  	// Send download request
    21  	req.Header.Set("User-Agent", userAgent)
    22  	resp, err := httpClient.Do(req)
    23  	if err != nil {
    24  		return nil, "", err
    25  	}
    26  
    27  	// Get content type
    28  	contentType := resp.Header.Get("Content-Type")
    29  
    30  	return resp.Body, contentType, nil
    31  }