Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinB committed Nov 18, 2020
1 parent e4b9d27 commit 154aea7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 0 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ import (
"github.com/klauspost/compress/zstd"
)

func tempFileDone(filePath string) {
err := os.Rename(filePath, filePath+".done")
if err != nil {
panic(err)
}
}

// GetSHA1 return the SHA1 of a []byte,
// can be used to fill the WARC-Payload-Digest header
func GetSHA1(content []byte) string {
Expand Down
13 changes: 9 additions & 4 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ type Record struct {
// CLRF
// CLRF
func (w *Writer) WriteRecord(r *Record) (recordID string, err error) {
defer func(r *Record) {
if r.PayloadPath != "" {
err := os.Rename(r.PayloadPath, r.PayloadPath+".done")
if err != nil {
panic(err)
}
}
}(r)

// Generate record ID
recordID = uuid.NewV4().String()

Expand Down Expand Up @@ -92,7 +101,6 @@ func (w *Writer) WriteRecord(r *Record) (recordID string, err error) {

_, err = io.WriteString(w.fileWriter, "\r\n")
if err != nil {
tempFileDone(r.PayloadPath)
return recordID, err
}

Expand All @@ -102,13 +110,11 @@ func (w *Writer) WriteRecord(r *Record) (recordID string, err error) {
for {
count, err := bufferedReader.Read(buffer)
if err != nil && err != io.EOF {
tempFileDone(r.PayloadPath)
return recordID, err
}

_, err = io.WriteString(w.fileWriter, string(buffer))
if err != nil {
tempFileDone(r.PayloadPath)
return recordID, err
}

Expand All @@ -117,7 +123,6 @@ func (w *Writer) WriteRecord(r *Record) (recordID string, err error) {
break
}
}
tempFileDone(r.PayloadPath)

_, err = io.WriteString(w.fileWriter, "\r\n\r\n")
if err != nil {
Expand Down

0 comments on commit 154aea7

Please sign in to comment.