-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathutils.go
More file actions
41 lines (34 loc) · 1.24 KB
/
utils.go
File metadata and controls
41 lines (34 loc) · 1.24 KB
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
40
41
package csv
import (
"fmt"
"net/http"
"time"
"github.com/graph-gophers/graphql-go"
"github.com/stackrox/rox/pkg/grpc/errors"
)
const (
// utf8BOM is the UTF-8 BOM byte sequence.
utf8BOM = "\uFEFF"
)
// Utility functions to be used for CSV exporting.
// WriteError responds with the error message and HTTP status code deduced from
// the error class. Appropriate response headers are set. Note that once data
// have been written to ResponseWriter, depending on its implementation, headers
// and the status code might have already been sent over HTTP. In such case,
// calling WriteError will not have the desired effect.
func WriteError(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), errors.ErrToHTTPStatus(err))
}
// writeHeaders sets appropriate HTTP headers for CSV response.
func writeHeaders(w http.ResponseWriter, filename string) {
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
w.WriteHeader(http.StatusOK)
}
// FromGraphQLTime create a string representation of the given graphQL.Time.
func FromGraphQLTime(timestamp *graphql.Time) string {
if timestamp == nil {
return "-"
}
return timestamp.Time.Format(time.RFC1123)
}