Skip to content

Instantly share code, notes, and snippets.

@joshwcomeau
Last active May 12, 2016 13:11
Show Gist options
  • Save joshwcomeau/89b640c0730da1e582fde76d69acc9ef to your computer and use it in GitHub Desktop.
Save joshwcomeau/89b640c0730da1e582fde76d69acc9ef to your computer and use it in GitHub Desktop.
convert-to-async
app.post('/process-upload', upload.single('image'), (req, res) => {
// Define some helper methods that provide semantic names to the
// actions in our route.
const resizeAndConvert = buffer => (
imConvertPromise({
srcData: buffer,
width: 32,
height: 16,
format: 'PNG'
})
);
const readPixelsFromImage = buffer => (
imageMagick.getConstPixels({
srcData: buffer,
x: 0,
y: 0,
columns: 32,
rows: 16
})
);
const saveToDisk = file => buffer => (
// Save the file to disk, and then return the original buffer.
// This is to preserve the data so it can be used further down the chain.
writeFilePromise(getPathForNewFile(file), buffer)
.then(() => buffer)
);
readFilePromise(req.file.path)
.then(resizeAndConvert)
.then(saveToDisk(req.file))
.then(readPixelsFromImage)
.then(pixels => {
res.json({ done: true, pixels });
}).catch(err => { throw err });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment