-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Closed
Labels
Description
Ok, guys. I have a json includes a remote url of image. trying to get images and add to worksheet by Promise.all http.get. So code is:
`
const getImage = (url, book, sheet, sizeOf, row, column) => new Promise((resolve, reject) => {
const request = https.get(url, response => {
if(response.statusCode === 200){
let chunks = [];
const imageColumn = sheet.getColumn('image')
const imageRow = sheet.getRow(row + 1)
response.on('data', chunk => {
chunks.push(chunk)
}).on('end', () => {
const buffer = Buffer.concat(chunks)
const dimension = sizeOf(buffer)
imageColumn.width = dimension.width
imageRow.height = dimension.height
const imageId = book.addImage({
buffer: buffer,
extension: 'png',
});
sheet.addImage(imageId, {
tl: { col: column, row: row },
br: { col: column + 1, row: row + 1 },
editAs: "oneCell"
});
resolve(book)
})
}
})
request.on('error', (err) => reject(err))
})
sheet.columns = Object.keys(header).map((index) => ({Header: header[index], key: index}))
let promises = []
req.body.forEach((row, index) => {
sheet.addRow(row)
row.image && /\.(jpg|png|gif)/.test(row.image) && promises.push(getImage(url.parse(row.image), workbook, sheet, sizeOf, index, imageColumn))
})
promises.length
&& Promise.all(promises).then(book => book.pop().xlsx.write(res).then(() => res.end()), err => console.log(err))
|| writer.write(res).then(() => res.end())`
And i have an 504 timeout on a rows more than 500. Are image operation with excel blocking my event-loop, or buffer operations? How can i get all images and then add it to book? Maybe i should use a stremaing i/o to write file in child processes concurrently?