|
| 1 | +package document |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "go.uber.org/atomic" |
| 10 | +) |
| 11 | + |
| 12 | +type Http struct { |
| 13 | + status *atomic.Int32 |
| 14 | + client *http.Client |
| 15 | + httpReq *http.Request |
| 16 | + Document |
| 17 | +} |
| 18 | + |
| 19 | +var ( |
| 20 | + _ ReadableDocument = (*File)(nil) |
| 21 | + _ ClosableDocument = (*File)(nil) |
| 22 | +) |
| 23 | + |
| 24 | +type HttpConfig struct { |
| 25 | + client *http.Client |
| 26 | + link string |
| 27 | + method string |
| 28 | + payload io.Reader |
| 29 | +} |
| 30 | + |
| 31 | +type HttpOption func(*HttpConfig) |
| 32 | + |
| 33 | +func WithHttpMethod(method string) HttpOption { |
| 34 | + return func(h *HttpConfig) { |
| 35 | + h.method = method |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func WithHttpURL(link string) HttpOption { |
| 40 | + return func(h *HttpConfig) { |
| 41 | + h.link = link |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func WithPayload(payload io.Reader) HttpOption { |
| 46 | + return func(h *HttpConfig) { |
| 47 | + h.payload = payload |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func WithHttpClient(client *http.Client) HttpOption { |
| 52 | + return func(h *HttpConfig) { |
| 53 | + h.client = client |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func NewHttp(opts ...HttpOption) (*Http, error) { |
| 58 | + var cfg HttpConfig |
| 59 | + for _, opt := range opts { |
| 60 | + opt(&cfg) |
| 61 | + } |
| 62 | + if cfg.client == nil { |
| 63 | + cfg.client = http.DefaultClient |
| 64 | + } |
| 65 | + httpReq, err := http.NewRequest(cfg.method, cfg.link, cfg.payload) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return &Http{ |
| 70 | + status: atomic.NewInt32(Unread), |
| 71 | + client: cfg.client, |
| 72 | + httpReq: httpReq, |
| 73 | + Document: Document{ |
| 74 | + buffer: new(bytes.Buffer), |
| 75 | + Meta: map[string]string{ |
| 76 | + "url": cfg.link, |
| 77 | + "method": cfg.method, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (h *Http) ReadStatus() ReadStatus { |
| 84 | + return h.status.Load() |
| 85 | +} |
| 86 | + |
| 87 | +func (h *Http) ReadAll() error { |
| 88 | + if h.ReadStatus() == Reading { |
| 89 | + return ErrReading |
| 90 | + } else if h.ReadStatus() == ReadCompleted { |
| 91 | + return nil |
| 92 | + } |
| 93 | + httpResp, err := h.client.Do(h.httpReq) |
| 94 | + if err != nil { |
| 95 | + h.status.Store(Unread) |
| 96 | + return err |
| 97 | + } |
| 98 | + defer httpResp.Body.Close() |
| 99 | + if _, err = io.Copy(h.buffer, httpResp.Body); err != nil { |
| 100 | + h.status.Store(Unread) |
| 101 | + } |
| 102 | + h.status.Store(ReadCompleted) |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (h *Http) Read() (chan<- []byte, error) { |
| 107 | + ch := make(chan<- []byte) |
| 108 | + if h.ReadStatus() == Reading { |
| 109 | + return nil, ErrReading |
| 110 | + } else if h.ReadStatus() == ReadCompleted { |
| 111 | + go func() { |
| 112 | + defer close(ch) |
| 113 | + h.status.Store(Reading) |
| 114 | + reader := bytes.NewReader(h.buffer.Bytes()) |
| 115 | + tmp := make([]byte, 1024) |
| 116 | + for { |
| 117 | + n, err := reader.Read(tmp) |
| 118 | + if err != nil { |
| 119 | + h.status.Store(ReadCompleted) |
| 120 | + return |
| 121 | + } |
| 122 | + bs := make([]byte, n) |
| 123 | + copy(bs, tmp[:n]) |
| 124 | + ch <- bs |
| 125 | + } |
| 126 | + }() |
| 127 | + return ch, nil |
| 128 | + } |
| 129 | + go func() { |
| 130 | + defer close(ch) |
| 131 | + h.status.Store(Reading) |
| 132 | + httpResp, err := h.client.Do(h.httpReq) |
| 133 | + if err != nil { |
| 134 | + h.status.Store(Unread) |
| 135 | + return |
| 136 | + } |
| 137 | + defer httpResp.Body.Close() |
| 138 | + tmp := make([]byte, 1024) |
| 139 | + for { |
| 140 | + n, err := httpResp.Body.Read(tmp) |
| 141 | + if err != nil { |
| 142 | + if errors.Is(err, io.EOF) { |
| 143 | + h.status.Store(ReadCompleted) |
| 144 | + } else { |
| 145 | + h.buffer.Reset() |
| 146 | + h.status.Store(Unread) |
| 147 | + } |
| 148 | + return |
| 149 | + } |
| 150 | + h.buffer.Write(tmp[:n]) |
| 151 | + } |
| 152 | + }() |
| 153 | + return ch, nil |
| 154 | +} |
0 commit comments