mirror of
https://github.com/Tencent/WeKnora.git
synced 2026-08-30 16:53:21 +08:00
4a9977b11f
- Introduced a loading state (`loadingChunks`) to manage document chunk loading more effectively. - Updated the scroll handling logic to prevent multiple requests while loading chunks. - Adjusted the pagination logic to ensure correct page increments based on the total document count. This update improves the user experience by ensuring smoother document loading and better state management during scrolling interactions.
33 lines
846 B
Go
33 lines
846 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var defaultHTTPClient = &http.Client{Timeout: 60 * time.Second}
|
|
|
|
// DownloadBytes fetches the content at the given HTTP(S) URL and returns the
|
|
// raw bytes. It reuses a package-level http.Client with a 60-second timeout.
|
|
func DownloadBytes(url string) ([]byte, error) {
|
|
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
|
return nil, fmt.Errorf("unsupported URL scheme: %s", url)
|
|
}
|
|
resp, err := defaultHTTPClient.Get(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("HTTP GET: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("HTTP %d for %s", resp.StatusCode, url)
|
|
}
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read body: %w", err)
|
|
}
|
|
return data, nil
|
|
}
|