53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
|
|
"orientator/internal/pipeline"
|
|
)
|
|
|
|
type combinedZip struct {
|
|
results []*pipeline.Result
|
|
errors map[string]error
|
|
}
|
|
|
|
func (c *combinedZip) add(r *pipeline.Result) {
|
|
c.results = append(c.results, r)
|
|
}
|
|
|
|
func (c *combinedZip) addError(name string, err error) {
|
|
if c.errors == nil {
|
|
c.errors = map[string]error{}
|
|
}
|
|
c.errors[name] = err
|
|
}
|
|
|
|
func (c *combinedZip) finalize() ([]byte, []pipeline.PageReport, error) {
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
var allReports []pipeline.PageReport
|
|
for _, r := range c.results {
|
|
w, err := zw.Create(r.OutputName)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if _, err := io.Copy(w, bytes.NewReader(r.OutputBytes)); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
allReports = append(allReports, r.Reports...)
|
|
}
|
|
for name, err := range c.errors {
|
|
w, e := zw.Create(name + ".error.txt")
|
|
if e != nil {
|
|
return nil, nil, e
|
|
}
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return buf.Bytes(), allReports, nil
|
|
}
|