-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecord.go
39 lines (31 loc) · 933 Bytes
/
record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package warc
import (
"io"
)
func (c *CustomHTTPClient) WriteRecord(WARCTargetURI, WARCType, contentType, payloadString string, payloadReader io.Reader) {
// Initialize the record
metadataRecord := NewRecord("", false)
// Set the headers
metadataRecord.Header.Set("WARC-Type", WARCType)
metadataRecord.Header.Set("WARC-Target-URI", WARCTargetURI)
if contentType != "" {
metadataRecord.Header.Set("Content-Type", contentType)
}
// Write the payload
if payloadString != "" {
metadataRecord.Content.Write([]byte(payloadString))
} else if payloadReader != nil {
_, err := io.Copy(metadataRecord.Content, payloadReader)
if err != nil {
panic(err)
}
} else {
panic("no payload provided")
}
// Add it to the batch
batch := NewRecordBatch(make(chan struct{}, 1))
batch.Records = append(batch.Records, metadataRecord)
c.WARCWriter <- batch
// Wait for the record to be written
<-batch.FeedbackChan
}