67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// captainDocumentPageParserBackendImpl is the production implementation of
|
|
// CaptainDocumentPageParserBackend. It fetches a single web page and
|
|
// extracts its title and visible text content — the same logic used by
|
|
// the sync backend for individual page URLs.
|
|
type captainDocumentPageParserBackendImpl struct {
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// NewCaptainDocumentPageParserBackend creates the production page parser backend.
|
|
func NewCaptainDocumentPageParserBackend() CaptainDocumentPageParserBackend {
|
|
return &captainDocumentPageParserBackendImpl{
|
|
httpClient: &http.Client{Timeout: 30 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (b *captainDocumentPageParserBackendImpl) ParseCaptainDocumentPage(ctx context.Context, pageLink string) (*CaptainDocumentSyncResult, error) {
|
|
if pageLink == "" {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "not_found"}, nil
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageLink, nil)
|
|
if err != nil {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "fetch_failed"}, fmt.Errorf("create request: %w", err)
|
|
}
|
|
req.Header.Set("User-Agent", "GoChat-Captain/1.0")
|
|
|
|
resp, err := b.httpClient.Do(req)
|
|
if err != nil {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "fetch_failed"}, fmt.Errorf("fetch url: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "fetch_failed"}, fmt.Errorf("HTTP %d for %s", resp.StatusCode, pageLink)
|
|
}
|
|
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 5<<20)) // 5MB max
|
|
if err != nil {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "fetch_failed"}, fmt.Errorf("read body: %w", err)
|
|
}
|
|
|
|
title, content := extractHTMLText(string(body))
|
|
content = strings.TrimSpace(content)
|
|
if content == "" {
|
|
return &CaptainDocumentSyncResult{ErrorCode: "content_empty"}, nil
|
|
}
|
|
|
|
if title == "" {
|
|
title = pageLink
|
|
}
|
|
return &CaptainDocumentSyncResult{
|
|
Content: content,
|
|
Title: title,
|
|
}, nil
|
|
}
|