forked from daptin/daptin
/
action_export_csv_data.go
126 lines (94 loc) · 3.18 KB
/
action_export_csv_data.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package resource
import (
"encoding/base64"
"fmt"
"github.com/artpar/api2go"
log "github.com/sirupsen/logrus"
"github.com/gocarina/gocsv"
"encoding/csv"
"time"
"io/ioutil"
)
type ExportCsvDataPerformer struct {
cmsConfig *CmsConfig
cruds map[string]*DbResource
}
func (d *ExportCsvDataPerformer) Name() string {
return "__csv_data_export"
}
func (d *ExportCsvDataPerformer) DoAction(request ActionRequest, inFields map[string]interface{}) (api2go.Responder, []ActionResponse, []error) {
responses := make([]ActionResponse, 0)
tableName, ok := inFields["table_name"]
finalName := "complete"
result := make(map[string]interface{})
if ok && tableName != nil {
tableNameStr := tableName.(string)
log.Infof("Export data for table: %v", tableNameStr)
objects, err := d.cruds[tableNameStr].GetAllRawObjects(tableNameStr)
if err != nil {
log.Errorf("Failed to get all objects of type [%v] : %v", tableNameStr, err)
}
result[tableNameStr] = objects
finalName = tableNameStr
} else {
for _, tableInfo := range d.cmsConfig.Tables {
data, err := d.cruds[tableInfo.TableName].GetAllRawObjects(tableInfo.TableName)
if err != nil {
log.Errorf("Failed to export objects of type [%v]: %v", tableInfo.TableName, err)
continue
}
result[tableInfo.TableName] = data
}
}
currentDate := time.Now()
prefix := currentDate.Format("2006-01-02-15-04-05")
csvFile, err := ioutil.TempFile("", prefix)
for outTableName, contents := range result {
if tableName != nil {
csvFile.WriteString(outTableName)
}
csvFileWriter := csv.NewWriter(csvFile)
contentArray := contents.([]map[string]interface{})
if len(contentArray) == 0 {
csvFile.WriteString("No data\n")
}
var columnKeys []string
csvWriter := gocsv.NewSafeCSVWriter(csvFileWriter)
firstRow := contentArray[0]
for colName, _ := range firstRow {
columnKeys = append(columnKeys, colName)
}
csvWriter.Write(columnKeys)
for _, row := range contentArray {
var dataRow []string
for _, colName := range columnKeys {
dataRow = append(dataRow, fmt.Sprintf("%v", row[colName]))
}
csvWriter.Write(dataRow)
}
csvFile.WriteString("\n")
}
csvFile.Close()
csvFileName := csvFile.Name()
csvFileContents, err := ioutil.ReadFile(csvFileName)
if InfoErr(err, "Failed to read csv file to download") {
actionResponse := NewActionResponse("client.notify", NewClientNotification("error", "Failed to generate csv: "+err.Error(), "Failed"))
responses = append(responses, actionResponse)
return nil, responses, nil
}
responseAttrs := make(map[string]interface{})
responseAttrs["content"] = base64.StdEncoding.EncodeToString(csvFileContents)
responseAttrs["name"] = fmt.Sprintf("daptin_dump_%v.csv", finalName)
responseAttrs["contentType"] = "application/csv"
responseAttrs["message"] = "Downloading csv "
actionResponse := NewActionResponse("client.file.download", responseAttrs)
responses = append(responses, actionResponse)
return nil, responses, nil
}
func NewExportCsvDataPerformer(initConfig *CmsConfig, cruds map[string]*DbResource) (ActionPerformerInterface, error) {
handler := ExportCsvDataPerformer{
cmsConfig: initConfig,
cruds: cruds,
}
return &handler, nil
}