@resolveio/server-lib 9.2.19 → 9.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/client-server-app.js +55 -1
  2. package/client-server-app.js.map +1 -1
  3. package/collections/app-status.collection.js +40 -1
  4. package/collections/app-status.collection.js.map +1 -1
  5. package/collections/app-version.collection.js +40 -1
  6. package/collections/app-version.collection.js.map +1 -1
  7. package/collections/counter.collection.js +43 -1
  8. package/collections/counter.collection.js.map +1 -1
  9. package/collections/cron-job-history.collection.js +71 -1
  10. package/collections/cron-job-history.collection.js.map +1 -1
  11. package/collections/cron-job.collection.js +74 -1
  12. package/collections/cron-job.collection.js.map +1 -1
  13. package/collections/email-history.collection.js +55 -1
  14. package/collections/email-history.collection.js.map +1 -1
  15. package/collections/email-verified.collection.js +50 -1
  16. package/collections/email-verified.collection.js.map +1 -1
  17. package/collections/file.collection.js +62 -1
  18. package/collections/file.collection.js.map +1 -1
  19. package/collections/flag.collection.js +44 -1
  20. package/collections/flag.collection.js.map +1 -1
  21. package/collections/log-method-latency.collection.js +53 -1
  22. package/collections/log-method-latency.collection.js.map +1 -1
  23. package/collections/log-subscription.collection.js +57 -1
  24. package/collections/log-subscription.collection.js.map +1 -1
  25. package/collections/log.collection.js +72 -1
  26. package/collections/log.collection.js.map +1 -1
  27. package/collections/logged-in-users.collection.js +58 -1
  28. package/collections/logged-in-users.collection.js.map +1 -1
  29. package/collections/method-call.collection.js +51 -1
  30. package/collections/method-call.collection.js.map +1 -1
  31. package/collections/method-response.collection.js +55 -1
  32. package/collections/method-response.collection.js.map +1 -1
  33. package/collections/monitor-cpu.collection.js +57 -1
  34. package/collections/monitor-cpu.collection.js.map +1 -1
  35. package/collections/monitor-https-inbound.collection.js +66 -1
  36. package/collections/monitor-https-inbound.collection.js.map +1 -1
  37. package/collections/monitor-https-outbound.collection.js +74 -1
  38. package/collections/monitor-https-outbound.collection.js.map +1 -1
  39. package/collections/monitor-memory.collection.js +68 -1
  40. package/collections/monitor-memory.collection.js.map +1 -1
  41. package/collections/monitor-mongo.collection.js +62 -1
  42. package/collections/monitor-mongo.collection.js.map +1 -1
  43. package/collections/notification.collection.js +40 -1
  44. package/collections/notification.collection.js.map +1 -1
  45. package/collections/report-builder-dashboard-builder.collection.js +93 -1
  46. package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
  47. package/collections/report-builder-library.collection.js +76 -1
  48. package/collections/report-builder-library.collection.js.map +1 -1
  49. package/collections/report-builder-report.collection.js +138 -1
  50. package/collections/report-builder-report.collection.js.map +1 -1
  51. package/collections/user-group.collection.js +79 -1
  52. package/collections/user-group.collection.js.map +1 -1
  53. package/collections/user-guide.collection.js +46 -1
  54. package/collections/user-guide.collection.js.map +1 -1
  55. package/collections/user.collection.js +248 -1
  56. package/collections/user.collection.js.map +1 -1
  57. package/cron/cron.js +161 -1
  58. package/cron/cron.js.map +1 -1
  59. package/fixtures/cron-jobs.js +72 -1
  60. package/fixtures/cron-jobs.js.map +1 -1
  61. package/fixtures/init.js +62 -1
  62. package/fixtures/init.js.map +1 -1
  63. package/http/auth.js +697 -1
  64. package/http/auth.js.map +1 -1
  65. package/http/health.js +10 -1
  66. package/http/health.js.map +1 -1
  67. package/http/home.js +64 -1
  68. package/http/home.js.map +1 -1
  69. package/index.js +123 -1
  70. package/index.js.map +1 -1
  71. package/managers/cron.manager.js +205 -1
  72. package/managers/cron.manager.js.map +1 -1
  73. package/managers/method.manager.js +599 -1
  74. package/managers/method.manager.js.map +1 -1
  75. package/managers/mongo.manager.js +1682 -1
  76. package/managers/mongo.manager.js.map +1 -1
  77. package/managers/monitor.manager.js +334 -1
  78. package/managers/monitor.manager.js.map +1 -1
  79. package/managers/subscription.manager.js +867 -1
  80. package/managers/subscription.manager.js.map +1 -1
  81. package/managers/subscription.performance.js +100 -1
  82. package/managers/subscription.performance.js.map +1 -1
  83. package/methods/accounts.js +224 -1
  84. package/methods/accounts.js.map +1 -1
  85. package/methods/aws.js +449 -1
  86. package/methods/aws.js.map +1 -1
  87. package/methods/cloudconvert.js +214 -1
  88. package/methods/cloudconvert.js.map +1 -1
  89. package/methods/collections.js +526 -1
  90. package/methods/collections.js.map +1 -1
  91. package/methods/counters.js +121 -1
  92. package/methods/counters.js.map +1 -1
  93. package/methods/cron-jobs.js +1284 -1
  94. package/methods/cron-jobs.js.map +1 -1
  95. package/methods/flags.js +8 -1
  96. package/methods/flags.js.map +1 -1
  97. package/methods/logs.js +370 -1
  98. package/methods/logs.js.map +1 -1
  99. package/methods/monitor.js +384 -1
  100. package/methods/monitor.js.map +1 -1
  101. package/methods/pdf.js +685 -1
  102. package/methods/pdf.js.map +1 -1
  103. package/methods/report-builder.js +826 -1
  104. package/methods/report-builder.js.map +1 -1
  105. package/methods/support.js +263 -1
  106. package/methods/support.js.map +1 -1
  107. package/models/app-status.model.js +3 -1
  108. package/models/app-status.model.js.map +1 -1
  109. package/models/app-version.model.js +3 -1
  110. package/models/app-version.model.js.map +1 -1
  111. package/models/billing-logged-in-users.model.js +3 -1
  112. package/models/billing-logged-in-users.model.js.map +1 -1
  113. package/models/collection-document.model.js +3 -1
  114. package/models/collection-document.model.js.map +1 -1
  115. package/models/counter.model.js +3 -1
  116. package/models/counter.model.js.map +1 -1
  117. package/models/cron-job-history.model.js +3 -1
  118. package/models/cron-job-history.model.js.map +1 -1
  119. package/models/cron-job.model.js +3 -1
  120. package/models/cron-job.model.js.map +1 -1
  121. package/models/dialog.model.js +3 -1
  122. package/models/dialog.model.js.map +1 -1
  123. package/models/email-history.model.js +3 -1
  124. package/models/email-history.model.js.map +1 -1
  125. package/models/email-verified.model.js +3 -1
  126. package/models/email-verified.model.js.map +1 -1
  127. package/models/file.model.js +3 -1
  128. package/models/file.model.js.map +1 -1
  129. package/models/flag.model.js +3 -1
  130. package/models/flag.model.js.map +1 -1
  131. package/models/log-method-latency.model.js +3 -1
  132. package/models/log-method-latency.model.js.map +1 -1
  133. package/models/log-subscription.model.js +3 -1
  134. package/models/log-subscription.model.js.map +1 -1
  135. package/models/log.model.js +3 -1
  136. package/models/log.model.js.map +1 -1
  137. package/models/logged-in-users.model.js +3 -1
  138. package/models/logged-in-users.model.js.map +1 -1
  139. package/models/method-call.model.js +3 -1
  140. package/models/method-call.model.js.map +1 -1
  141. package/models/method-response.model.js +3 -1
  142. package/models/method-response.model.js.map +1 -1
  143. package/models/method.model.js +3 -1
  144. package/models/method.model.js.map +1 -1
  145. package/models/monitor-cpu.model.js +3 -1
  146. package/models/monitor-cpu.model.js.map +1 -1
  147. package/models/monitor-https-inbound.model.js +3 -1
  148. package/models/monitor-https-inbound.model.js.map +1 -1
  149. package/models/monitor-https-outbound.model.js +3 -1
  150. package/models/monitor-https-outbound.model.js.map +1 -1
  151. package/models/monitor-memory.model.js +3 -1
  152. package/models/monitor-memory.model.js.map +1 -1
  153. package/models/monitor-mongo.model.js +3 -1
  154. package/models/monitor-mongo.model.js.map +1 -1
  155. package/models/notification.model.js +3 -1
  156. package/models/notification.model.js.map +1 -1
  157. package/models/pagination.model.js +23 -1
  158. package/models/pagination.model.js.map +1 -1
  159. package/models/permission.model.js +3 -1
  160. package/models/permission.model.js.map +1 -1
  161. package/models/report-builder-dashboard-builder.model.js +3 -1
  162. package/models/report-builder-dashboard-builder.model.js.map +1 -1
  163. package/models/report-builder-library.model.js +3 -1
  164. package/models/report-builder-library.model.js.map +1 -1
  165. package/models/report-builder-report.model.js +3 -1
  166. package/models/report-builder-report.model.js.map +1 -1
  167. package/models/report-builder.model.js +3 -1
  168. package/models/report-builder.model.js.map +1 -1
  169. package/models/select-data-label.model.js +3 -1
  170. package/models/select-data-label.model.js.map +1 -1
  171. package/models/server-response.model.js +3 -1
  172. package/models/server-response.model.js.map +1 -1
  173. package/models/subscription.model.js +3 -1
  174. package/models/subscription.model.js.map +1 -1
  175. package/models/support-ticket.model.js +3 -1
  176. package/models/support-ticket.model.js.map +1 -1
  177. package/models/user-group.model.js +3 -1
  178. package/models/user-group.model.js.map +1 -1
  179. package/models/user-guide.model.js +3 -1
  180. package/models/user-guide.model.js.map +1 -1
  181. package/models/user.model.js +3 -1
  182. package/models/user.model.js.map +1 -1
  183. package/package.json +1 -2
  184. package/public_api.js +78 -1
  185. package/public_api.js.map +1 -1
  186. package/publications/app-status.js +16 -1
  187. package/publications/app-status.js.map +1 -1
  188. package/publications/app-version.js +16 -1
  189. package/publications/app-version.js.map +1 -1
  190. package/publications/cron-jobs.js +32 -1
  191. package/publications/cron-jobs.js.map +1 -1
  192. package/publications/files.js +36 -1
  193. package/publications/files.js.map +1 -1
  194. package/publications/flags.js +22 -1
  195. package/publications/flags.js.map +1 -1
  196. package/publications/logs.js +90 -1
  197. package/publications/logs.js.map +1 -1
  198. package/publications/method-calls.js +16 -1
  199. package/publications/method-calls.js.map +1 -1
  200. package/publications/method-responses.js +16 -1
  201. package/publications/method-responses.js.map +1 -1
  202. package/publications/notifications.js +16 -1
  203. package/publications/notifications.js.map +1 -1
  204. package/publications/report-builder-dashboard-builders.js +42 -1
  205. package/publications/report-builder-dashboard-builders.js.map +1 -1
  206. package/publications/report-builder-libraries.js +89 -1
  207. package/publications/report-builder-libraries.js.map +1 -1
  208. package/publications/report-builder-reports.js +50 -1
  209. package/publications/report-builder-reports.js.map +1 -1
  210. package/publications/super-admin.js +16 -1
  211. package/publications/super-admin.js.map +1 -1
  212. package/publications/user-groups.js +16 -1
  213. package/publications/user-groups.js.map +1 -1
  214. package/publications/user-guides.js +16 -1
  215. package/publications/user-guides.js.map +1 -1
  216. package/server-app.js +682 -1
  217. package/server-app.js.map +1 -1
  218. package/util/common.js +498 -1
  219. package/util/common.js.map +1 -1
  220. package/util/schema-report-builder.js +452 -1
  221. package/util/schema-report-builder.js.map +1 -1
package/methods/pdf.js CHANGED
@@ -1,2 +1,686 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))((function(i,a){function fulfilled(t){try{step(r.next(t))}catch(t){a(t)}}function rejected(t){try{step(r.throw(t))}catch(t){a(t)}}function step(t){t.done?i(t.value):function adopt(t){return t instanceof n?t:new n((function(e){e(t)}))}(t.value).then(fulfilled,rejected)}step((r=r.apply(t,e||[])).next())}))},__generator=this&&this.__generator||function(t,e){var n,r,i,a,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:verb(0),throw:verb(1),return:verb(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function verb(a){return function(o){return function step(a){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,r=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=e.call(t,s)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,o])}}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.loadPDFMethods=void 0;var fs=require("fs"),bent=require("bent"),simpl_schema_1=require("simpl-schema"),file_collection_1=require("../collections/file.collection"),mongo_manager_1=require("../managers/mongo.manager"),pdf_lib_1=require("pdf-lib"),pdfCnt=0;function loadPDFMethods(t){t.methods({generatePDF:{check:new simpl_schema_1.default({html:{type:String},fileName:{type:String},orientation:{type:String,optional:!0},fontSize:{type:String,optional:!0},upload:{type:Boolean,optional:!0},uploadFileOrder:{type:Number,optional:!0},uploadFileType:{type:String,optional:!0}}),function:function(t,e,n,r,i,a,s){void 0===n&&(n="portrait"),void 0===r&&(r="8px"),void 0===i&&(i=!1),void 0===a&&(a=-1),void 0===s&&(s="");var o=this;return new Promise((function(l,c){var f=require("phantom"),u=null;f.create().then((function(t){return u=t,t.createPage()})).then((function(f){var p=[];f.on("onResourceRequested",(function(t,e){p.push(t.id)})),f.on("onResourceReceived",(function(t){var e=p.indexOf(t.id);p.splice(e,1)})),f.property("paperSize",{format:"letter",margin:"1cm",orientation:n,footer:{height:"1cm",contents:u.callback((function(t,e){return"<span style='float:right; font-size: 12px;'>Page "+t+" of "+e+"</span>"}))}}),f.property("content","\n\t\t\t\t\t\t<head>\n\t\t\t\t\t\t\t<style>\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\t\t\t\tfont-weight: bold;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-Italic.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\t\t\t\tfont-weight: bold;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-BoldItalic.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tbody {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-size: "+(r||"8px")+";\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.row {\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\tdisplay: flex;\n\t\t\t\t\t\t\t\t\tflex-wrap: wrap;\n\t\t\t\t\t\t\t\t\t-webkit-flex-wrap: wrap;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t.page-break {\n\t\t\t\t\t\t\t\t\tpage-break-before: always !important;\n\t\t\t\t\t\t\t\t\tmargin-bottom: 1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.cell {\n\t\t\t\t\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\t\t\t\t\tpadding : 2px;\n\t\t\t\t\t\t\t\t\t-webkit-flex: 1;\n\t\t\t\t\t\t\t\t\tword-wrap: break-word;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.cell-no-padding {\n\t\t\t\t\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\t\t\t\t\t-webkit-flex: 1;\n\t\t\t\t\t\t\t\t\tword-wrap: break-word;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered {\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: center;\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\ttext-align: center;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered-left {\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: flex-start;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered-right {\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: flex-end;\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t.box {\n\t\t\t\t\t\t\t\t\tborder: 1px solid black;\n\t\t\t\t\t\t\t\t\tmargin: -1px 0px 0px -1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.boxNoBorder {\n\t\t\t\t\t\t\t\t\tmargin: -1px 0px 0px -1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tpre {\n\t\t\t\t\t\t\t\t\twhite-space: pre-wrap; /* css-3 */\n\t\t\t\t\t\t\t\t\twhite-space: -moz-pre-wrap; /* Mozilla, since 1999 */\n\t\t\t\t\t\t\t\t\twhite-space: -pre-wrap; /* Opera 4-6 */\n\t\t\t\t\t\t\t\t\twhite-space: -o-pre-wrap; /* Opera 7 */\n\t\t\t\t\t\t\t\t\tword-wrap: break-word; /* Internet Explorer 5.5+ */\n\t\t\t\t\t\t\t\t\tfont-size: 12px;\n\t\t\t\t\t\t\t\t\tfont-family: Open Sans; \n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</style>\n\t\t\t\t\t\t</head>\n\t\t\t\t\t\t<body>\n\t\t\t\t\t"+t.replace(new RegExp("flex-basis:","g"),"-webkit-flex-basis:").replace(new RegExp("flex:","g"),"-webkit-flex:")+"</body>");var d=pdfCnt+++"-"+e.replace(new RegExp(/\W/g),""),h=setInterval((function(){0===p.length&&(clearInterval(h),f.render(d,{format:"pdf",quality:"100"}).then((function(){fs.existsSync(d)?fs.readFile(d,(function(t,n){if(t)console.log(t);else if(fs.unlinkSync(d),i){var r="data:application/pdf;base64,"+Buffer.from(n).toString("base64");o.callMethodInternal.call(o,"uploadFileAndSave",e,r,4*Math.ceil(r.length/3),null!==a?a:-1,s,!1,(function(t,e){t?c(t):l(e)}))}else l("data:application/pdf;base64,"+Buffer.from(n).toString("base64"));u.exit()})):(c("No tmp file"),console.log("No tmp file ",d))}),(function(t){console.log(t)})))}),1e3)})).catch((function(t){c(t),u.exit()}))}))}},compressBase64AndUploadPDF:{check:new simpl_schema_1.default({id_file:{type:String},fileName:{type:String},filePath:{type:String},fileContent:{type:String}}),function:function(t,e,n,r){var i=this;return new Promise((function(a,s){i.callMethodInternal("getCCProcessURL").then((function(o){return __awaiter(i,void 0,void 0,(function(){var i,l,c;return __generator(this,(function(f){switch(f.label){case 0:i=bent("https:"+o.url,"POST","json",200),f.label=1;case 1:return f.trys.push([1,3,,4]),[4,i("",{mode:"compress",input:"base64",file:r,filename:e,outputformat:"pdf",wait:!0,output:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET,path:n}}})];case 2:return(l=f.sent()).output.size?(file_collection_1.Files.updateOne({_id:t},{$set:{size:l.output.size}}),a(!0)):s("Compressed file has no size, compress failed"),[3,4];case 3:return c=f.sent(),s(c),[3,4];case 4:return[2]}}))}))}),(function(t){return s(t)}))}))}},compressPDFOnS3:{check:new simpl_schema_1.default({id_file:{type:String},fileName:{type:String},filePath:{type:String}}),function:function(t,e,n){var r=this;return new Promise((function(i,a){r.callMethodInternal("getCCProcessURL").then((function(s){return __awaiter(r,void 0,void 0,(function(){var r,o,l;return __generator(this,(function(c){switch(c.label){case 0:r=bent("https:"+s.url,"POST","json",200),c.label=1;case 1:return c.trys.push([1,3,,4]),[4,r("",{mode:"compress",input:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET}},file:n,filename:e,outputformat:"pdf",wait:!0,output:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET,path:n}}})];case 2:return(o=c.sent()).output.size?(file_collection_1.Files.updateOne({_id:t},{$set:{size:o.output.size}}),i(!0)):a("Compressed file has no size, compress failed"),[3,4];case 3:return l=c.sent(),a(l),[3,4];case 4:return[2]}}))}))}),(function(t){return a(t)}))}))}},mergePDFResolve:{check:new simpl_schema_1.default({fileKeys:{type:Array},"fileKeys.$":{type:String},fileName:{type:String},fileType:{type:String,optional:!0}}),function:function(t,e,n){var r=this;return new Promise((function(i,a){r.callMethodInternal("getSignedUrls",t,900,(function(s,o){if(o&&o.length){var l=[];o.forEach((function(t){l.push(r.callMethodInternal("getDataURIfromURL",t))})),Promise.all(l).then((function(t){return __awaiter(r,void 0,void 0,(function(){var r,a,s,o,l,c,f,u;return __generator(this,(function(p){switch(p.label){case 0:return[4,pdf_lib_1.PDFDocument.create()];case 1:r=p.sent(),a=0,p.label=2;case 2:return a<t.length?(s=t[a]).startsWith("data:application/pdf;base64,")?[4,pdf_lib_1.PDFDocument.load(s)]:[3,5]:[3,15];case 3:return o=p.sent(),[4,r.copyPages(o,o.getPageIndices())];case 4:return p.sent().forEach((function(t){r.addPage(t)})),[3,14];case 5:return s.startsWith("data:image/jpeg;base64,/9j/")||s.startsWith("data:image/png;base64,/9j/")?[4,pdf_lib_1.PDFDocument.create()]:[3,9];case 6:return[4,(o=p.sent()).embedJpg(s)];case 7:return l=p.sent(),c=o.addPage(),((f=l.size()).width>c.getWidth()||f.height>c.getWidth())&&(f=l.scaleToFit(c.getWidth(),c.getHeight())),l.scaleToFit(c.getWidth()/2,c.getHeight()/2),c.drawImage(l,{x:c.getWidth()/2-f.width/2,y:c.getHeight()/2-f.height/2,width:f.width,height:f.height}),[4,r.copyPages(o,o.getPageIndices())];case 8:return p.sent().forEach((function(t){r.addPage(t)})),[3,14];case 9:return s.startsWith("data:image/png;base64,iVBOR")?[4,pdf_lib_1.PDFDocument.create()]:[3,13];case 10:return[4,(o=p.sent()).embedPng(s)];case 11:return l=p.sent(),c=o.addPage(),((f=l.size()).width>c.getWidth()||f.height>c.getWidth())&&(f=l.scaleToFit(c.getWidth(),c.getHeight())),l.scaleToFit(c.getWidth()/2,c.getHeight()/2),c.drawImage(l,{x:c.getWidth()/2-f.width/2,y:c.getHeight()/2-f.height/2,width:f.width,height:f.height}),[4,r.copyPages(o,o.getPageIndices())];case 12:return p.sent().forEach((function(t){r.addPage(t)})),[3,14];case 13:console.log("Error - unsupported!!",s.slice(0,60)),p.label=14;case 14:return a++,[3,2];case 15:return[4,r.saveAsBase64({dataUri:!0})];case 16:return u=p.sent(),this.callMethodInternal("uploadFileAndSave",e,u,4*Math.ceil(u.length/3),-1,n,!1,(function(t,e){i(e)})),[2]}}))}))}))}else r.callMethodInternal("insertErrorLog","PDF Merge - getSignedUrls",[t,s]),a(s)}))}))}},mergePDF:{check:new simpl_schema_1.default({fileKeys:{type:Array},"fileKeys.$":{type:String},fileName:{type:String},fileType:{type:String,optional:!0}}),function:function(t,e,n){var r=this;return new Promise((function(i,a){var s="uploads/"+Date.now()+"-"+pdfCnt+++"-"+e;r.callMethodInternal("createCloudConvertJob",t,["merge","optimize"],s,e).then((function(t){var o={_id:mongo_manager_1.objectIdHexString(),__v:0,name:e,size:t.data.tasks.filter((function(t){return"task-2"===t.name}))[0].result.files[0].size,key:s,type:n||"",order:-1};o.size?r.callMethodInternal("insertDocument","files",o,(function(t,e){e?(o.status="Uploaded",i(o)):a(t)})):a("Merged file has no size, merge failed")}),(function(){}))}))}},mergePDFV1:{check:new simpl_schema_1.default({fileKeys:{type:Array},"fileKeys.$":{type:String},fileName:{type:String},fileType:{type:String,optional:!0}}),function:function(t,e,n){var r=this;return new Promise((function(i,a){r.callMethodInternal.call(r,"getCCProcessURL").then((function(s){return __awaiter(r,void 0,void 0,(function(){var r,o,l,c,f,u=this;return __generator(this,(function(p){switch(p.label){case 0:r="uploads/"+Date.now()+"-"+pdfCnt+++"-"+e,o=bent("https:"+s.url,"POST","json",200),p.label=1;case 1:return p.trys.push([1,3,,4]),[4,o("",{mode:"combine",input:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET}},files:t,outputformat:"pdf",wait:!0,output:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET,path:r}},converteroptions:{resize:"612x792",resizemode:"scale",resizeenlarge:null,grayscale:null,auto_orient:null,strip_metatags:!1,density:72,rotate:null,command:null}})];case 2:return l=p.sent(),c={_id:mongo_manager_1.objectIdHexString(),__v:0,name:e,size:l.output.size,key:r,type:n||"",order:-1},l.output.size?this.callMethodInternal("insertDocument","files",c,(function(t,n){n?(c.status="Uploaded",u.callMethodInternal("compressPDFOnS3",c._id,e,r,(function(t,e){e?i(c):a(t)}))):a(t)})):a("Merged file has no size, merge failed"),[3,4];case 3:return f=p.sent(),a(f),[3,4];case 4:return[2]}}))}))}),(function(t){return a(t)}))}))}},mergePDFNoUpload:{check:new simpl_schema_1.default({fileKeys:{type:Array},"fileKeys.$":{type:String},fileName:{type:String},fileType:{type:String,optional:!0}}),function:function(t,e,n){var r=this;return new Promise((function(i,a){r.callMethodInternal("getCCProcessURL").then((function(s){return __awaiter(r,void 0,void 0,(function(){var r,o,l,c,f,u=this;return __generator(this,(function(p){switch(p.label){case 0:r="uploads/"+Date.now()+"-"+pdfCnt+++"-"+e,o=bent("https:"+s.url,"POST","json",200),p.label=1;case 1:return p.trys.push([1,3,,4]),[4,o("",{mode:"combine",input:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET}},files:t,outputformat:"pdf",wait:!0,output:{s3:{accesskeyid:this.serverConfig.AWSACCESSKEYID,secretaccesskey:this.serverConfig.AWSSECRETACCESSKEY,region:this.serverConfig.AWSREGION,bucket:this.serverConfig.AWSBUCKET,path:r}},converteroptions:{resize:"612x792",resizemode:"scale",resizeenlarge:null,grayscale:null,auto_orient:null,strip_metatags:!1,density:72,rotate:null,command:null}})];case 2:return l=p.sent(),c={_id:mongo_manager_1.objectIdHexString(),__v:0,name:e,size:l.output.size,key:r,type:n||"",order:-1},this.callMethodInternal("insertDocument","files",c,(function(t,e){if(e){c.status="Uploaded",i(c);var n=u;setTimeout((function(){n.callMethodInternal.call(n,"deleteFile",r)}),6e4)}else a(t)})),[3,4];case 3:return f=p.sent(),a(f),[3,4];case 4:return[2]}}))}))}),(function(t){return a(t)}))}))}}})}exports.loadPDFMethods=loadPDFMethods;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.loadPDFMethods = void 0;
40
+ var fs = require('fs');
41
+ var bent = require("bent");
42
+ var simpl_schema_1 = require("simpl-schema");
43
+ var file_collection_1 = require("../collections/file.collection");
44
+ var mongo_manager_1 = require("../managers/mongo.manager");
45
+ var pdf_lib_1 = require("pdf-lib");
46
+ var pdfCnt = 0;
47
+ function loadPDFMethods(methodManager) {
48
+ methodManager.methods({
49
+ generatePDF: {
50
+ check: new simpl_schema_1.default({
51
+ html: {
52
+ type: String
53
+ },
54
+ fileName: {
55
+ type: String
56
+ },
57
+ orientation: {
58
+ type: String,
59
+ optional: true
60
+ },
61
+ fontSize: {
62
+ type: String,
63
+ optional: true
64
+ },
65
+ upload: {
66
+ type: Boolean,
67
+ optional: true
68
+ },
69
+ uploadFileOrder: {
70
+ type: Number,
71
+ optional: true
72
+ },
73
+ uploadFileType: {
74
+ type: String,
75
+ optional: true
76
+ }
77
+ }),
78
+ function: function (html, fileName, orientation, fontSize, upload, uploadFileOrder, uploadFileType) {
79
+ if (orientation === void 0) { orientation = 'portrait'; }
80
+ if (fontSize === void 0) { fontSize = '8px'; }
81
+ if (upload === void 0) { upload = false; }
82
+ if (uploadFileOrder === void 0) { uploadFileOrder = -1; }
83
+ if (uploadFileType === void 0) { uploadFileType = ''; }
84
+ var that = this;
85
+ return new Promise(function (resolve, reject) {
86
+ var phantom = require('phantom');
87
+ var phInstance = null;
88
+ phantom.create().then(function (instance) {
89
+ phInstance = instance;
90
+ return instance.createPage();
91
+ }).then(function (page) {
92
+ var requestsArray = [];
93
+ page.on('onResourceRequested', function (requestData, networkRequest) {
94
+ requestsArray.push(requestData.id);
95
+ });
96
+ page.on('onResourceReceived', function (response) {
97
+ var index = requestsArray.indexOf(response.id);
98
+ requestsArray.splice(index, 1);
99
+ });
100
+ page.property('paperSize', {
101
+ format: 'letter',
102
+ margin: '1cm',
103
+ orientation: orientation,
104
+ footer: {
105
+ height: '1cm',
106
+ contents: phInstance.callback(function (pageNum, numPages) {
107
+ return '<span style=\'float:right; font-size: 12px;\'>Page ' + pageNum + ' of ' + numPages + '</span>';
108
+ })
109
+ }
110
+ });
111
+ page.property('content', "\n\t\t\t\t\t\t<head>\n\t\t\t\t\t\t\t<style>\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: normal;\n\t\t\t\t\t\t\t\t\tfont-weight: bold;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\t\t\t\tfont-weight: normal;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-Italic.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t@font-face {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-style: italic;\n\t\t\t\t\t\t\t\t\tfont-weight: bold;\n\t\t\t\t\t\t\t\t\tsrc: url(\"/usr/share/fonts/dejavu/DejaVuSerif-BoldItalic.ttf\");\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tbody {\n\t\t\t\t\t\t\t\t\tfont-family: 'Dejavu Serif', serif;\n\t\t\t\t\t\t\t\t\tfont-size: " + (fontSize ? fontSize : '8px') + ";\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.row {\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\tdisplay: flex;\n\t\t\t\t\t\t\t\t\tflex-wrap: wrap;\n\t\t\t\t\t\t\t\t\t-webkit-flex-wrap: wrap;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t.page-break {\n\t\t\t\t\t\t\t\t\tpage-break-before: always !important;\n\t\t\t\t\t\t\t\t\tmargin-bottom: 1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.cell {\n\t\t\t\t\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\t\t\t\t\tpadding : 2px;\n\t\t\t\t\t\t\t\t\t-webkit-flex: 1;\n\t\t\t\t\t\t\t\t\tword-wrap: break-word;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.cell-no-padding {\n\t\t\t\t\t\t\t\t\tbox-sizing: border-box;\n\t\t\t\t\t\t\t\t\t-webkit-flex: 1;\n\t\t\t\t\t\t\t\t\tword-wrap: break-word;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered {\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: center;\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\ttext-align: center;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered-left {\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: flex-start;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.centered-right {\n\t\t\t\t\t\t\t\t\t-webkit-justify-content: flex-end;\n\t\t\t\t\t\t\t\t\t-webkit-align-items: center;\n\t\t\t\t\t\t\t\t\tdisplay: -webkit-flex;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t.box {\n\t\t\t\t\t\t\t\t\tborder: 1px solid black;\n\t\t\t\t\t\t\t\t\tmargin: -1px 0px 0px -1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t.boxNoBorder {\n\t\t\t\t\t\t\t\t\tmargin: -1px 0px 0px -1px;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tpre {\n\t\t\t\t\t\t\t\t\twhite-space: pre-wrap; /* css-3 */\n\t\t\t\t\t\t\t\t\twhite-space: -moz-pre-wrap; /* Mozilla, since 1999 */\n\t\t\t\t\t\t\t\t\twhite-space: -pre-wrap; /* Opera 4-6 */\n\t\t\t\t\t\t\t\t\twhite-space: -o-pre-wrap; /* Opera 7 */\n\t\t\t\t\t\t\t\t\tword-wrap: break-word; /* Internet Explorer 5.5+ */\n\t\t\t\t\t\t\t\t\tfont-size: 12px;\n\t\t\t\t\t\t\t\t\tfont-family: Open Sans; \n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t</style>\n\t\t\t\t\t\t</head>\n\t\t\t\t\t\t<body>\n\t\t\t\t\t" + html.replace(new RegExp('flex-basis:', 'g'), '-webkit-flex-basis:').replace(new RegExp('flex:', 'g'), '-webkit-flex:') + '</body>');
112
+ var tmpFileName = (pdfCnt++) + '-' + fileName.replace(new RegExp(/\W/g), '');
113
+ var interval = setInterval(function () {
114
+ if (requestsArray.length === 0) {
115
+ clearInterval(interval);
116
+ page.render(tmpFileName, {
117
+ format: 'pdf',
118
+ quality: '100'
119
+ }).then(function () {
120
+ if (fs.existsSync(tmpFileName)) {
121
+ fs.readFile(tmpFileName, function (errRead, data) {
122
+ if (!errRead) {
123
+ fs.unlinkSync(tmpFileName);
124
+ if (!upload) {
125
+ resolve('data:application/pdf;base64,' + Buffer.from(data).toString('base64'));
126
+ }
127
+ else {
128
+ var fileData = 'data:application/pdf;base64,' + Buffer.from(data).toString('base64');
129
+ that.callMethodInternal.call(that, 'uploadFileAndSave', fileName, fileData, (4 * Math.ceil(fileData.length / 3)), uploadFileOrder !== null ? uploadFileOrder : -1, uploadFileType, false, function (err, res) {
130
+ if (err) {
131
+ reject(err);
132
+ }
133
+ else {
134
+ resolve(res);
135
+ }
136
+ });
137
+ }
138
+ }
139
+ else {
140
+ console.log(errRead);
141
+ }
142
+ phInstance.exit();
143
+ });
144
+ }
145
+ else {
146
+ reject('No tmp file');
147
+ console.log('No tmp file ', tmpFileName);
148
+ }
149
+ }, function (err) {
150
+ console.log(err);
151
+ });
152
+ }
153
+ }, 1000);
154
+ })
155
+ .catch(function (error) {
156
+ reject(error);
157
+ phInstance.exit();
158
+ });
159
+ });
160
+ }
161
+ },
162
+ compressBase64AndUploadPDF: {
163
+ check: new simpl_schema_1.default({
164
+ id_file: {
165
+ type: String
166
+ },
167
+ fileName: {
168
+ type: String
169
+ },
170
+ filePath: {
171
+ type: String
172
+ },
173
+ fileContent: {
174
+ type: String
175
+ }
176
+ }),
177
+ function: function (id_file, fileName, filePath, fileContent) {
178
+ var _this = this;
179
+ return new Promise(function (resolve, reject) {
180
+ _this.callMethodInternal('getCCProcessURL').then(function (resCC) { return __awaiter(_this, void 0, void 0, function () {
181
+ var request, body, error_1;
182
+ return __generator(this, function (_a) {
183
+ switch (_a.label) {
184
+ case 0:
185
+ request = bent('https:' + resCC.url, 'POST', 'json', 200);
186
+ _a.label = 1;
187
+ case 1:
188
+ _a.trys.push([1, 3, , 4]);
189
+ return [4 /*yield*/, request('', {
190
+ mode: 'compress',
191
+ input: 'base64',
192
+ file: fileContent,
193
+ filename: fileName,
194
+ outputformat: 'pdf',
195
+ wait: true,
196
+ output: {
197
+ 's3': {
198
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
199
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
200
+ 'region': this.serverConfig['AWSREGION'],
201
+ 'bucket': this.serverConfig['AWSBUCKET'],
202
+ 'path': filePath
203
+ }
204
+ }
205
+ })];
206
+ case 2:
207
+ body = _a.sent();
208
+ if (body.output.size) {
209
+ file_collection_1.Files.updateOne({ _id: id_file }, { $set: { size: body.output.size } });
210
+ resolve(true);
211
+ }
212
+ else {
213
+ reject('Compressed file has no size, compress failed');
214
+ }
215
+ return [3 /*break*/, 4];
216
+ case 3:
217
+ error_1 = _a.sent();
218
+ reject(error_1);
219
+ return [3 /*break*/, 4];
220
+ case 4: return [2 /*return*/];
221
+ }
222
+ });
223
+ }); }, function (errCC) { return reject(errCC); });
224
+ });
225
+ }
226
+ },
227
+ compressPDFOnS3: {
228
+ check: new simpl_schema_1.default({
229
+ id_file: {
230
+ type: String
231
+ },
232
+ fileName: {
233
+ type: String
234
+ },
235
+ filePath: {
236
+ type: String
237
+ }
238
+ }),
239
+ function: function (id_file, fileName, filePath) {
240
+ var _this = this;
241
+ return new Promise(function (resolve, reject) {
242
+ _this.callMethodInternal('getCCProcessURL').then(function (resCC) { return __awaiter(_this, void 0, void 0, function () {
243
+ var request, body, error_2;
244
+ return __generator(this, function (_a) {
245
+ switch (_a.label) {
246
+ case 0:
247
+ request = bent('https:' + resCC.url, 'POST', 'json', 200);
248
+ _a.label = 1;
249
+ case 1:
250
+ _a.trys.push([1, 3, , 4]);
251
+ return [4 /*yield*/, request('', {
252
+ mode: 'compress',
253
+ input: {
254
+ 's3': {
255
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
256
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
257
+ 'region': this.serverConfig['AWSREGION'],
258
+ 'bucket': this.serverConfig['AWSBUCKET']
259
+ }
260
+ },
261
+ file: filePath,
262
+ filename: fileName,
263
+ outputformat: 'pdf',
264
+ wait: true,
265
+ output: {
266
+ 's3': {
267
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
268
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
269
+ 'region': this.serverConfig['AWSREGION'],
270
+ 'bucket': this.serverConfig['AWSBUCKET'],
271
+ 'path': filePath
272
+ }
273
+ }
274
+ })];
275
+ case 2:
276
+ body = _a.sent();
277
+ if (body.output.size) {
278
+ file_collection_1.Files.updateOne({ _id: id_file }, { $set: { size: body.output.size } });
279
+ resolve(true);
280
+ }
281
+ else {
282
+ reject('Compressed file has no size, compress failed');
283
+ }
284
+ return [3 /*break*/, 4];
285
+ case 3:
286
+ error_2 = _a.sent();
287
+ reject(error_2);
288
+ return [3 /*break*/, 4];
289
+ case 4: return [2 /*return*/];
290
+ }
291
+ });
292
+ }); }, function (errCC) { return reject(errCC); });
293
+ });
294
+ }
295
+ },
296
+ mergePDFResolve: {
297
+ check: new simpl_schema_1.default({
298
+ fileKeys: {
299
+ type: Array
300
+ },
301
+ 'fileKeys.$': {
302
+ type: String
303
+ },
304
+ fileName: {
305
+ type: String
306
+ },
307
+ fileType: {
308
+ type: String,
309
+ optional: true
310
+ }
311
+ }),
312
+ function: function (fileKeys, fileName, fileType) {
313
+ var _this = this;
314
+ return new Promise(function (resolve, reject) {
315
+ _this.callMethodInternal('getSignedUrls', fileKeys, 900, function (errSigneds, resSigneds) {
316
+ if (resSigneds && resSigneds.length) {
317
+ var promises_1 = [];
318
+ resSigneds.forEach(function (signedUrl) {
319
+ promises_1.push(_this.callMethodInternal('getDataURIfromURL', signedUrl));
320
+ });
321
+ Promise.all(promises_1).then(function (fileDatas) { return __awaiter(_this, void 0, void 0, function () {
322
+ var mergedPdf, i, fileData, pdf, copiedPages, pdf, img, page, imgDim, copiedPages, pdf, img, page, imgDim, copiedPages, b64PDF;
323
+ return __generator(this, function (_a) {
324
+ switch (_a.label) {
325
+ case 0: return [4 /*yield*/, pdf_lib_1.PDFDocument.create()];
326
+ case 1:
327
+ mergedPdf = _a.sent();
328
+ i = 0;
329
+ _a.label = 2;
330
+ case 2:
331
+ if (!(i < fileDatas.length)) return [3 /*break*/, 15];
332
+ fileData = fileDatas[i];
333
+ if (!fileData.startsWith('data:application/pdf;base64,')) return [3 /*break*/, 5];
334
+ return [4 /*yield*/, pdf_lib_1.PDFDocument.load(fileData)];
335
+ case 3:
336
+ pdf = _a.sent();
337
+ return [4 /*yield*/, mergedPdf.copyPages(pdf, pdf.getPageIndices())];
338
+ case 4:
339
+ copiedPages = _a.sent();
340
+ copiedPages.forEach(function (page) {
341
+ mergedPdf.addPage(page);
342
+ });
343
+ return [3 /*break*/, 14];
344
+ case 5:
345
+ if (!(fileData.startsWith('data:image/jpeg;base64,/9j/') || fileData.startsWith('data:image/png;base64,/9j/'))) return [3 /*break*/, 9];
346
+ return [4 /*yield*/, pdf_lib_1.PDFDocument.create()];
347
+ case 6:
348
+ pdf = _a.sent();
349
+ return [4 /*yield*/, pdf.embedJpg(fileData)];
350
+ case 7:
351
+ img = _a.sent();
352
+ page = pdf.addPage();
353
+ imgDim = img.size();
354
+ if (imgDim.width > page.getWidth() || imgDim.height > page.getWidth()) {
355
+ imgDim = img.scaleToFit(page.getWidth(), page.getHeight());
356
+ }
357
+ img.scaleToFit(page.getWidth() / 2, page.getHeight() / 2);
358
+ page.drawImage(img, {
359
+ x: page.getWidth() / 2 - imgDim.width / 2,
360
+ y: page.getHeight() / 2 - imgDim.height / 2,
361
+ width: imgDim.width,
362
+ height: imgDim.height,
363
+ });
364
+ return [4 /*yield*/, mergedPdf.copyPages(pdf, pdf.getPageIndices())];
365
+ case 8:
366
+ copiedPages = _a.sent();
367
+ copiedPages.forEach(function (page) {
368
+ mergedPdf.addPage(page);
369
+ });
370
+ return [3 /*break*/, 14];
371
+ case 9:
372
+ if (!fileData.startsWith('data:image/png;base64,iVBOR')) return [3 /*break*/, 13];
373
+ return [4 /*yield*/, pdf_lib_1.PDFDocument.create()];
374
+ case 10:
375
+ pdf = _a.sent();
376
+ return [4 /*yield*/, pdf.embedPng(fileData)];
377
+ case 11:
378
+ img = _a.sent();
379
+ page = pdf.addPage();
380
+ imgDim = img.size();
381
+ if (imgDim.width > page.getWidth() || imgDim.height > page.getWidth()) {
382
+ imgDim = img.scaleToFit(page.getWidth(), page.getHeight());
383
+ }
384
+ img.scaleToFit(page.getWidth() / 2, page.getHeight() / 2);
385
+ page.drawImage(img, {
386
+ x: page.getWidth() / 2 - imgDim.width / 2,
387
+ y: page.getHeight() / 2 - imgDim.height / 2,
388
+ width: imgDim.width,
389
+ height: imgDim.height,
390
+ });
391
+ return [4 /*yield*/, mergedPdf.copyPages(pdf, pdf.getPageIndices())];
392
+ case 12:
393
+ copiedPages = _a.sent();
394
+ copiedPages.forEach(function (page) {
395
+ mergedPdf.addPage(page);
396
+ });
397
+ return [3 /*break*/, 14];
398
+ case 13:
399
+ console.log('Error - unsupported!!', fileData.slice(0, 60));
400
+ _a.label = 14;
401
+ case 14:
402
+ i++;
403
+ return [3 /*break*/, 2];
404
+ case 15: return [4 /*yield*/, mergedPdf.saveAsBase64({ dataUri: true })];
405
+ case 16:
406
+ b64PDF = _a.sent();
407
+ this.callMethodInternal('uploadFileAndSave', fileName, b64PDF, (4 * Math.ceil(b64PDF.length / 3)), -1, fileType, false, function (errUp, resUp) {
408
+ resolve(resUp);
409
+ });
410
+ return [2 /*return*/];
411
+ }
412
+ });
413
+ }); });
414
+ }
415
+ else {
416
+ _this.callMethodInternal('insertErrorLog', 'PDF Merge - getSignedUrls', [
417
+ fileKeys,
418
+ errSigneds
419
+ ]);
420
+ reject(errSigneds);
421
+ }
422
+ });
423
+ });
424
+ }
425
+ },
426
+ mergePDF: {
427
+ check: new simpl_schema_1.default({
428
+ fileKeys: {
429
+ type: Array
430
+ },
431
+ 'fileKeys.$': {
432
+ type: String
433
+ },
434
+ fileName: {
435
+ type: String
436
+ },
437
+ fileType: {
438
+ type: String,
439
+ optional: true
440
+ }
441
+ }),
442
+ function: function (fileKeys, fileName, fileType) {
443
+ var _this = this;
444
+ return new Promise(function (resolve, reject) {
445
+ var mergeFileName = 'uploads/' + Date.now() + '-' + (pdfCnt++) + '-' + fileName;
446
+ _this.callMethodInternal('createCloudConvertJob', fileKeys, ['merge', 'optimize'], mergeFileName, fileName).then(function (resCCJob) {
447
+ var resFile = {
448
+ _id: mongo_manager_1.objectIdHexString(),
449
+ __v: 0,
450
+ name: fileName,
451
+ size: resCCJob.data.tasks.filter(function (a) { return a.name === 'task-2'; })[0].result.files[0].size,
452
+ key: mergeFileName,
453
+ type: fileType ? fileType : '',
454
+ order: -1
455
+ };
456
+ if (resFile.size) {
457
+ _this.callMethodInternal('insertDocument', 'files', resFile, function (errUpFile, resUpFile) {
458
+ if (resUpFile) {
459
+ resFile.status = 'Uploaded';
460
+ resolve(resFile);
461
+ }
462
+ else {
463
+ reject(errUpFile);
464
+ }
465
+ });
466
+ }
467
+ else {
468
+ reject('Merged file has no size, merge failed');
469
+ }
470
+ }, function () { });
471
+ });
472
+ }
473
+ },
474
+ mergePDFV1: {
475
+ check: new simpl_schema_1.default({
476
+ fileKeys: {
477
+ type: Array
478
+ },
479
+ 'fileKeys.$': {
480
+ type: String
481
+ },
482
+ fileName: {
483
+ type: String
484
+ },
485
+ fileType: {
486
+ type: String,
487
+ optional: true
488
+ }
489
+ }),
490
+ function: function (fileKeys, fileName, fileType) {
491
+ var _this = this;
492
+ return new Promise(function (resolve, reject) {
493
+ _this.callMethodInternal.call(_this, 'getCCProcessURL').then(function (resCC) { return __awaiter(_this, void 0, void 0, function () {
494
+ var mergeFileName, request, body, resFile_1, error_3;
495
+ var _this = this;
496
+ return __generator(this, function (_a) {
497
+ switch (_a.label) {
498
+ case 0:
499
+ mergeFileName = 'uploads/' + Date.now() + '-' + (pdfCnt++) + '-' + fileName;
500
+ request = bent('https:' + resCC.url, 'POST', 'json', 200);
501
+ _a.label = 1;
502
+ case 1:
503
+ _a.trys.push([1, 3, , 4]);
504
+ return [4 /*yield*/, request('', {
505
+ mode: 'combine',
506
+ input: {
507
+ s3: {
508
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
509
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
510
+ 'region': this.serverConfig['AWSREGION'],
511
+ 'bucket': this.serverConfig['AWSBUCKET']
512
+ }
513
+ },
514
+ files: fileKeys,
515
+ outputformat: 'pdf',
516
+ wait: true,
517
+ output: {
518
+ 's3': {
519
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
520
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
521
+ 'region': this.serverConfig['AWSREGION'],
522
+ 'bucket': this.serverConfig['AWSBUCKET'],
523
+ 'path': mergeFileName
524
+ }
525
+ },
526
+ converteroptions: {
527
+ 'resize': '612x792',
528
+ 'resizemode': 'scale',
529
+ 'resizeenlarge': null,
530
+ 'grayscale': null,
531
+ 'auto_orient': null,
532
+ 'strip_metatags': false,
533
+ 'density': 72,
534
+ 'rotate': null,
535
+ 'command': null
536
+ }
537
+ })];
538
+ case 2:
539
+ body = _a.sent();
540
+ resFile_1 = {
541
+ _id: mongo_manager_1.objectIdHexString(),
542
+ __v: 0,
543
+ name: fileName,
544
+ size: body.output.size,
545
+ key: mergeFileName,
546
+ type: fileType ? fileType : '',
547
+ order: -1
548
+ };
549
+ if (body.output.size) {
550
+ this.callMethodInternal('insertDocument', 'files', resFile_1, function (errUpFile, resUpFile) {
551
+ if (resUpFile) {
552
+ resFile_1.status = 'Uploaded';
553
+ _this.callMethodInternal('compressPDFOnS3', resFile_1._id, fileName, mergeFileName, function (errCompress, resCompress) {
554
+ if (resCompress) {
555
+ resolve(resFile_1);
556
+ }
557
+ else {
558
+ reject(errCompress);
559
+ }
560
+ });
561
+ }
562
+ else {
563
+ reject(errUpFile);
564
+ }
565
+ });
566
+ }
567
+ else {
568
+ reject('Merged file has no size, merge failed');
569
+ }
570
+ return [3 /*break*/, 4];
571
+ case 3:
572
+ error_3 = _a.sent();
573
+ reject(error_3);
574
+ return [3 /*break*/, 4];
575
+ case 4: return [2 /*return*/];
576
+ }
577
+ });
578
+ }); }, function (errCC) { return reject(errCC); });
579
+ });
580
+ }
581
+ },
582
+ mergePDFNoUpload: {
583
+ check: new simpl_schema_1.default({
584
+ fileKeys: {
585
+ type: Array
586
+ },
587
+ 'fileKeys.$': {
588
+ type: String
589
+ },
590
+ fileName: {
591
+ type: String
592
+ },
593
+ fileType: {
594
+ type: String,
595
+ optional: true
596
+ }
597
+ }),
598
+ function: function (fileKeys, fileName, fileType) {
599
+ var _this = this;
600
+ return new Promise(function (resolve, reject) {
601
+ _this.callMethodInternal('getCCProcessURL').then(function (resCC) { return __awaiter(_this, void 0, void 0, function () {
602
+ var mergeFileName, request, body, resFile_2, error_4;
603
+ var _this = this;
604
+ return __generator(this, function (_a) {
605
+ switch (_a.label) {
606
+ case 0:
607
+ mergeFileName = 'uploads/' + Date.now() + '-' + (pdfCnt++) + '-' + fileName;
608
+ request = bent('https:' + resCC.url, 'POST', 'json', 200);
609
+ _a.label = 1;
610
+ case 1:
611
+ _a.trys.push([1, 3, , 4]);
612
+ return [4 /*yield*/, request('', {
613
+ mode: 'combine',
614
+ input: {
615
+ s3: {
616
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
617
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
618
+ 'region': this.serverConfig['AWSREGION'],
619
+ 'bucket': this.serverConfig['AWSBUCKET']
620
+ }
621
+ },
622
+ files: fileKeys,
623
+ outputformat: 'pdf',
624
+ wait: true,
625
+ output: {
626
+ 's3': {
627
+ 'accesskeyid': this.serverConfig['AWSACCESSKEYID'],
628
+ 'secretaccesskey': this.serverConfig['AWSSECRETACCESSKEY'],
629
+ 'region': this.serverConfig['AWSREGION'],
630
+ 'bucket': this.serverConfig['AWSBUCKET'],
631
+ 'path': mergeFileName
632
+ }
633
+ },
634
+ converteroptions: {
635
+ 'resize': '612x792',
636
+ 'resizemode': 'scale',
637
+ 'resizeenlarge': null,
638
+ 'grayscale': null,
639
+ 'auto_orient': null,
640
+ 'strip_metatags': false,
641
+ 'density': 72,
642
+ 'rotate': null,
643
+ 'command': null
644
+ },
645
+ })];
646
+ case 2:
647
+ body = _a.sent();
648
+ resFile_2 = {
649
+ _id: mongo_manager_1.objectIdHexString(),
650
+ __v: 0,
651
+ name: fileName,
652
+ size: body.output.size,
653
+ key: mergeFileName,
654
+ type: fileType ? fileType : '',
655
+ order: -1
656
+ };
657
+ this.callMethodInternal('insertDocument', 'files', resFile_2, function (errUpFile, resUpFile) {
658
+ if (resUpFile) {
659
+ resFile_2.status = 'Uploaded';
660
+ resolve(resFile_2);
661
+ var that_1 = _this;
662
+ setTimeout(function () {
663
+ that_1.callMethodInternal.call(that_1, 'deleteFile', mergeFileName);
664
+ }, 60000);
665
+ }
666
+ else {
667
+ reject(errUpFile);
668
+ }
669
+ });
670
+ return [3 /*break*/, 4];
671
+ case 3:
672
+ error_4 = _a.sent();
673
+ reject(error_4);
674
+ return [3 /*break*/, 4];
675
+ case 4: return [2 /*return*/];
676
+ }
677
+ });
678
+ }); }, function (errCC) { return reject(errCC); });
679
+ });
680
+ }
681
+ },
682
+ });
683
+ }
684
+ exports.loadPDFMethods = loadPDFMethods;
685
+
2
686
  //# sourceMappingURL=pdf.js.map