Created
September 12, 2016 22:48
-
-
Save panki/855310dd6e2ba0d033f766e833564c58 to your computer and use it in GitHub Desktop.
prepare files
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
_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(([image]) => { | |
page.uncensoredPath = image; | |
page.censoredPath = watermark ? images.combine(image, watermark) : image; | |
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