-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_upload.js
169 lines (155 loc) · 6.26 KB
/
csv_upload.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const node_xj = require("xls-to-json");
const ExcelJS = require('exceljs');
const sharp = require('sharp');
const csvParser = require('csv-parse');
const csv = require('csvtojson');
const Neume = require('./model/neumes');
function xlsxAdd(buffer, projectID, socket) {
console.log(`xlsx add to ${projectID}`);
const workbook = new ExcelJS.Workbook();
const columnNames = [
{header: 'images', key: 'images'},
{header: 'name', key: 'name'},
{header: 'genericName', key: 'genericName'},
//{header: 'width', key: 'width'},
{header: 'folio', key: 'folio'},
{header: 'description', key: 'description'},
{header: 'classification', key: 'classification'},
{header: 'encoding', key: 'encoding'},
]
console.log('good so far!')
workbook.xlsx.load(buffer).then(wb => {
// console.log(workbook, 'workbook instance');
workbook.eachSheet(async (sheet, id) => {
var neumes = [];
var keys = ['images']
sheet.columns = columnNames;
console.log(sheet);
sheet.eachRow({includeEmpty: true}, (row, index) => {
var neume = {}
console.log('Row ' + index + ' = ' + JSON.stringify(row.values));
if (index - 1) {
columnNames.forEach((name, i) => {
neume[name['key']] = row.getCell(name['key']).value;
})
neumes.push(neume);
}
})
for (const image of sheet.getImages()) {
console.log('processing image row', image.range.tl.nativeRow, 'col', image.range.tl.nativeCol, 'imageId', image.imageId);
const img = workbook.model.media.find(m => m.index === image.imageId);
// console.log(img);
neumes[image.range.tl.nativeRow - 1]['images'] = img;
}
console.log(neumes)
for (const neume of neumes) {
let resizedImageData = '';
let resizedBase64 = ''
var mimType = 'image/jpeg';
var resizedImg = '';
console.log('Neume image buffer: ' + neume['images']);
// If row has images or not
if (neume['images']) {
const buffer = await sharp(neume['images'].buffer)
.flatten({ background: { r: 255, g: 255, b: 255 } })
.resize(128,128, {
fit: 'contain',
background: "rgb(255,255,255,1)"
})
.toBuffer()
let resizedImageData = buffer.toString('base64');
let resizedBase64 = `data:${mimType};base64,${resizedImageData}`;
const neumeAdd = new Neume({
name: neume['name'] ? neume['name'] : '',
folio: neume['folio'] ? neume['folio'] : '',
description: neume['description'] ? neume['description'] : '',
classification: neume['classification'] ? neume['classification'] : '',
mei: neume['encoding'] ? neume['encoding'] : '',
review: '',
dob: '',
imagesBinary: [resizedBase64.split(',')[1]],
imagePath: neume['images'].name + '.jpg',
project: projectID,
neumeSection: '',
neumeSectionName: '',
source: '',
genericName: neume['genericName'] ? neume['genericName'] : '',
//width: neume['width'] ? neume['width']: ''
})
// Wait for this neume to be added before the next
await neumeAdd.save();
console.log(`message: created neume in project id: ${projectID}\t\t\tneume id: ${neumeAdd._id}`);
socket.emit('new neume info spreadsheet', [projectID, neumeAdd]);
} else {
console.log('entered catch');
const neumeAdd = new Neume({
name: neume['name'] ? neume['name'] : '',
folio: neume['folio'] ? neume['folio'] : '',
description: neume['description'] ? neume['description'] : '',
classification: neume['classification'] ? neume['classification'] : '',
mei: neume['encoding'] ? neume['encoding'] : '',
review: '',
dob: '',
imagesBinary: '',
imagePath: '',
project: projectID,
neumeSection: '',
neumeSectionName: '',
source: '',
genericName: neume['genericName'] ? neume['genericName'] : '',
//width: neume['width'] ? neume['width']: ''
})
// Wait for this neume to be added before the next
await neumeAdd.save();
console.log(`message: created neume in project id: ${projectID}\t\t\tneume id: ${neumeAdd._id}`);
socket.emit('new neume info spreadsheet', [projectID, neumeAdd]);
}
}
})
})
// let sheetData = xlsx.utils.sheet_to_json(workbook)
// var sheet_name_list = workbook.SheetNames;
// var neumeRows = xlsx.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
// neumeRows.forEach(function (neume) {
// console.log(neume);
// })
}
function csvUpload(buffer, projectID, socket) {
console.log(`csv add to ${projectID}`);
console.log(buffer.toString());
csv()
.fromString(buffer.toString())
.then((neumes) => {
neumes.forEach((neume, index) => {
console.log(neume)
if (neume['images'] == '') {
mongoose.model('neume').create({
name: neume['name'] ? neume['name'] : '',
folio: neume['folio'] ? neume['folio'] : '',
description: neume['description'] ? neume['description'] : '',
classification: neume['classification'] ? neume['classification'] : '',
mei: neume['encoding'] ? neume['encoding'] : '',
review: '',
dob: '',
imagesBinary: '',
imagePath: '',
project: projectID,
neumeSection: '',
neumeSectionName: '',
source: '',
genericName: neume['genericName'] ? neume['genericName'] : '',
//width: neume['width'] ? neume['width']: ''
}, function(err, neume) {
if (err) {
return renderError(res, err);
} else {
console.log(`message: created neume in project id: ${projectID}\t\t\tneume id: ${neume._id}`);
socket.emit('new neume info spreadsheet', [projectID, neume]);
}
})
}
});
});
// console.log(csv_json);
}
module.exports = { xlsxAdd, csvUpload }