Last active
June 21, 2017 17:06
-
-
Save mjlescano/4856a378d0e3aebf7dbd88283d6f3348 to your computer and use it in GitHub Desktop.
Filstack PDF Preview - PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dist | |
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const fetch = require('node-fetch') | |
const minimist = require('minimist') | |
const { key, file } = minimist(process.argv.slice(2)) | |
request({ docinfo: true }) | |
.then((res) => res.json()) | |
.then((info) => { | |
if (!info || !info.numpages) throw new Error('Invalid doc') | |
const pages = times(info.numpages).map((page) => { | |
return request({ | |
secure: true, | |
format: 'jpg', | |
page | |
}).then((res) => new Promise((resolve, reject) => { | |
const dest = path.join(__dirname, 'dist', `page-${page}.jpg`) | |
const stream = fs.createWriteStream(dest) | |
stream.on('error', reject) | |
stream.on('finish', resolve) | |
res.body.pipe(stream) | |
})) | |
}) | |
return Promise.all(pages) | |
}) | |
.then(() => { | |
console.log('Done!') | |
process.exit(0) | |
}) | |
.catch((err) => { | |
console.error(err) | |
process.exit(1) | |
}) | |
function request (params = {}) { | |
const output = Object.keys(params).map((param) => { | |
return `${param}:${params[param]}` | |
}).join(',') | |
const url = `https://process.filestackapi.com/${key}/output=${output}/${file}` | |
return fetch(url).then((res) => { | |
if (!res.ok) throw res | |
return res | |
}).catch((err) => { throw err }) | |
} | |
function times (length) { | |
return Array.from(Array(length)).map((item, index) => index + 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "filestack-preview", | |
"version": "1.0.0", | |
"lockfileVersion": 1, | |
"dependencies": { | |
"encoding": { | |
"version": "0.1.12", | |
"resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", | |
"integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=" | |
}, | |
"iconv-lite": { | |
"version": "0.4.18", | |
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.18.tgz", | |
"integrity": "sha512-sr1ZQph3UwHTR0XftSbK85OvBbxe/abLGzEnPENCQwmHf7sck8Oyu4ob3LgBxWWxRoM+QszeUyl7jbqapu2TqA==" | |
}, | |
"is-stream": { | |
"version": "1.1.0", | |
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", | |
"integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" | |
}, | |
"minimist": { | |
"version": "1.2.0", | |
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", | |
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" | |
}, | |
"node-fetch": { | |
"version": "1.7.1", | |
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.1.tgz", | |
"integrity": "sha512-j8XsFGCLw79vWXkZtMSmmLaOk9z5SQ9bV/tkbZVCqvgwzrjAGq66igobLofHtF63NvMTp2WjytpsNTGKa+XRIQ==" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "filestack-preview", | |
"version": "1.0.0", | |
"private": true, | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://git@gist.github.com/4856a378d0e3aebf7dbd88283d6f3348.git" | |
}, | |
"author": "mjlescano", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/4856a378d0e3aebf7dbd88283d6f3348" | |
}, | |
"homepage": "https://gist.github.com/4856a378d0e3aebf7dbd88283d6f3348", | |
"dependencies": { | |
"minimist": "^1.2.0", | |
"node-fetch": "^1.7.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment