Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const fs = require('fs');
  4. const os = require('os');
  5. const tmp = require('tmp');
  6. const child = require('child_process');
  7. const Promise = require('bluebird');
  8. const PassThrough = require('stream').PassThrough;
  9. const shellescape = require('shell-escape');
  10.  
  11. const execFile = Promise.promisify(child.execFile);
  12. const exec = Promise.promisify(child.exec);
  13. const readFile = Promise.promisify(fs.readFile);
  14. const writeFile = Promise.promisify(fs.writeFile);
  15.  
  16. const isWindows = os.platform() === 'win32';
  17.  
  18. const genHandle = (num) => {
  19. let s = '', t;
  20.  
  21. while (num > 0) {
  22. t = (num - 1) % 26;
  23. s = String.fromCharCode(65 + t) + s;
  24. num = (num - t) / 26 | 0;
  25. }
  26. return s;
  27. }
  28.  
  29. module.exports = (files, options) => new Promise((resolve, reject) => {
  30. if (!Array.isArray(files)) {
  31. reject(new TypeError('Expected files to be an array of paths to PDF files.'));
  32.  
  33. return;
  34. }
  35.  
  36. files = files
  37. .map((file) => typeof file === 'string' ? ({ file }) : file)
  38. // Object.keys(file)[0] !== '0' -> Check if is not a Buffer object
  39. .filter((file) => (file !== null && typeof file === 'object' && Object.keys(file)[0] !== '0' && typeof file.file === 'string'));
  40. if (files.length === 0) {
  41. reject(new Error('No files were submitted for merging.'));
  42.  
  43. return;
  44. }
  45.  
  46. const output = (buffer) => {
  47. if (options.output === Buffer || String(options.output).toUpperCase() === 'BUFFER') {
  48. return buffer;
  49. }
  50.  
  51. if (options.output === PassThrough || ['STREAM', 'READSTREAM'].indexOf(String(options.output).toUpperCase()) !== -1) {
  52. const stream = new PassThrough();
  53.  
  54. stream.end(buffer);
  55.  
  56. return stream;
  57. }
  58.  
  59. return writeFile(options.output, buffer)
  60. .then(() => buffer);
  61. }
  62.  
  63. options = Object.assign({
  64. libPath: 'pdftk',
  65. output: Buffer,
  66. }, options);
  67.  
  68. if (files.length === 1) {
  69. const fileObjKeys = Object.keys(files[0])
  70. if (fileObjKeys.length === 1 && fileObjKeys[0] === 'file') {
  71. readFile(files[0].file)
  72. .then((buffer) => {
  73. return output(buffer);
  74. })
  75. .then(resolve)
  76. .catch(reject);
  77.  
  78. return;
  79. }
  80. }
  81.  
  82. const tmpFilePath = isWindows
  83. ? tmp.tmpNameSync()
  84. : shellescape([tmp.tmpNameSync()]);
  85.  
  86. const inputPws = []
  87. const catOpts=[]
  88. const args = files.map((value, idx) => {
  89. let file = value.file;
  90.  
  91. let handle = null;
  92. // Check if we need use handles
  93. if (typeof value.inputPw === 'string' && value.inputPw.length > 0) {
  94. handle = genHandle(idx + 1);
  95. inputPws.push({ handle, inputPw: value.inputPw });
  96. file = `${handle}=${file}`;
  97. }
  98.  
  99. if (typeof value.catOpt === 'string' && value.catOpt.length > 0) {
  100. handle = genHandle(idx + 1);
  101. catOpts.push({ handle, catOpt: value.catOpt });
  102. file = `${handle}=${file}`;
  103. }
  104.  
  105. return isWindows
  106. ? `"${file}"`
  107. : shellescape([file.replace(/\\/g, '/')]);
  108. })
  109.  
  110. if (inputPws.length > 0) {
  111. args.push('input_pw');
  112. Array.prototype.push.apply(args, inputPws.map(item => `${item.handle}=${item.inputPw}`));
  113. }
  114. if (catOpts.length > 0) {
  115. args.push('cat');
  116. Array.prototype.push.apply(args, catOpts.map(item => `${item.handle}${item.catOpt}`));
  117. args.push('output');
  118. args.push(tmpFilePath);
  119. }else{
  120. args.push('cat', 'output', tmpFilePath);
  121. }
  122.  
  123. if (options.execOptions) {
  124. args.push(options.execOptions);
  125. }
  126.  
  127. console.log(options.libPath, args.toString())
  128.  
  129. const childPromise = (isWindows && options.libPath !== 'pdftk')
  130. ? execFile(options.libPath, args)
  131. : exec(`${options.libPath} ${args.join(' ')}`);
  132.  
  133. childPromise
  134. .then(() =>
  135. readFile(tmpFilePath)
  136. )
  137. .then((buffer) =>
  138. new Promise((resolve) => {
  139. fs.unlink(tmpFilePath, () => resolve(buffer));
  140. })
  141. )
  142. .then((buffer) => {
  143. return output(buffer);
  144. })
  145. .then(resolve)
  146. .catch(reject);
  147. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement