Skip to content

Instantly share code, notes, and snippets.

@panki
Created September 12, 2016 22:46
Show Gist options
  • Save panki/d1a523ecb1f077fee53255af040fd65d to your computer and use it in GitHub Desktop.
Save panki/d1a523ecb1f077fee53255af040fd65d to your computer and use it in GitHub Desktop.
_prepareDocumentFiles (release, content) {
const pagesCount = content.length;
const pages = content.map(page => Object.assign({}, page)); // clone
const resolutions = portals.getDocumentResolutions(release.portal_id);
let watermark = null; // path to temp file with portal watermark
let fullDocument = null; // path to temp file with combined pdf
console.log(release, pages);
return portals.getDocumentWatermark(release.portal_id)
.then(_watermark => { watermark = _watermark; })
// Make full document
.then(pages => pdf.combineDocument(pages.map(p => p.filename)))
.then(path => { fullDocument = path; })
// Convert pages to image with max resolution
// and prepare censored version with applied watermark
.then(() => Promise
.map(pages, page => pdf
.documentToImages(page.filename, 300) // max resolution 2480x3508 for A4 page size
.then(filename => {
page.uncensored = {};
page.censored = {};
page.uncensoredPath = filename;
page.censoredPath = watermark ? images.combine(filename, watermark) : filename;
return Promise.props(page);
}),
{ concurrency: 1 }
)
)
// Prepare resolutions for each page
.then(pages => Promise
.map(pages, page => {
// Resize uncensored versions
page.uncensored = Promise
.map(resolutions, res => {
const [ w ] = res.split('x');
return images.resize(page.uncensoredPath, res).then(file => ({ [w]: file }))
}, { concurrency: 1 })
.then(result => Object.assign({}, ...result));
// Resize censored versions
page.censored = Promise
.map(resolutions, res => {
const [ w ] = res.split('x');
return images.resize(page.censoredPath, res).then(file => ({ [w]: file }))
}, { concurrency: 1 })
.then(result => Object.assign({}, ...result))
return Promise.props(page);
},
{ concurrency: 1 }
)
)
// All files prepared, put them to the store
.then(pages => {
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment