welcome.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {DOMImplementation, XMLSerializer} from 'xmldom';
  2. import JsBarcode from 'jsbarcode';
  3. import {JSDOM} from 'jsdom';
  4. import {readFileSync} from 'fs';
  5. import {join} from 'path';
  6. import {spawn} from 'child_process';
  7. const src = join(__dirname, '..', 'src');
  8. const _svg = readFileSync(join(src, 'welcome.svg'), 'utf-8');
  9. const barcode = (data) => {
  10. const xmlSerializer = new XMLSerializer();
  11. const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
  12. const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  13. JsBarcode(svgNode, data, {
  14. xmlDocument: document,
  15. });
  16. return xmlSerializer.serializeToString(svgNode);
  17. };
  18. const imageSetter = (img, value) => img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', value);
  19. const textSetter = (el, value) => el.textContent = value;
  20. const {document: svg} = new JSDOM(_svg).window;
  21. /**
  22. * Generate SVG Welcome
  23. * @param {object} param0
  24. * @param {string} param0.wid
  25. * @param {string} param0.pp
  26. * @param {string} param0.name
  27. * @param {string} param0.text
  28. * @param {string} param0.background
  29. * @return {string}
  30. */
  31. const genSVG = async ({
  32. wid = '',
  33. pp = join(src, 'avatar_contact.png'),
  34. title = '',
  35. name = '',
  36. text = '',
  37. background = '',
  38. } = {}) => {
  39. const el = {
  40. code: ['#_1661899539392 > g:nth-child(6) > image', imageSetter, toBase64(await toImg(barcode(wid.replace(/[^0-9]/g, '')), 'png'), 'image/png')],
  41. pp: ['#_1661899539392 > g:nth-child(3) > image', imageSetter, pp],
  42. text: ['#_1661899539392 > text.fil1.fnt0', textSetter, text],
  43. title: ['#_1661899539392 > text.fil2.fnt1', textSetter, title],
  44. name: ['#_1661899539392 > text.fil2.fnt2', textSetter, name],
  45. bg: ['#_1661899539392 > g:nth-child(2) > image', imageSetter, background],
  46. };
  47. for (const [selector, set, value] of Object.values(el)) {
  48. set(svg.querySelector(selector), value);
  49. }
  50. return svg.body.innerHTML;
  51. };
  52. const toImg = (svg, format = 'png') => new Promise((resolve, reject) => {
  53. if (!svg) return resolve(Buffer.alloc(0));
  54. const bufs = [];
  55. const im = spawn('magick', ['convert', 'svg:-', format + ':-']);
  56. im.on('error', (e) => reject(e));
  57. im.stdout.on('data', (chunk) => bufs.push(chunk));
  58. im.stdin.write(Buffer.from(svg));
  59. im.stdin.end();
  60. im.on('close', (code) => {
  61. if (code !== 0) reject(code);
  62. resolve(Buffer.concat(bufs));
  63. });
  64. });
  65. const toBase64 = (buffer, mime) => `data:${mime};base64,${buffer.toString('base64')}`;
  66. /**
  67. * Render SVG Welcome
  68. * @param {object} param0
  69. * @param {string} param0.wid
  70. * @param {string} param0.pp
  71. * @param {string} param0.name
  72. * @param {string} param0.text
  73. * @param {string} param0.background
  74. * @return {Promise<Buffer>}
  75. */
  76. const render = async ({
  77. wid = '',
  78. pp = toBase64(readFileSync(join(src, 'avatar_contact.png')), 'image/png'),
  79. name = '',
  80. title = '',
  81. text = '',
  82. background = toBase64(readFileSync(join(src, 'Aesthetic', 'Aesthetic_000.jpeg')), 'image/jpeg'),
  83. } = {}, format = 'png') => {
  84. const svg = await genSVG({
  85. wid, pp, name, text, background, title,
  86. });
  87. return await toImg(svg, format);
  88. };
  89. if (require.main === module) {
  90. render({
  91. wid: '1234567890',
  92. // pp: '',
  93. name: 'John Doe',
  94. text: 'Lorem ipsum\ndot sit color',
  95. title: 'grup testing',
  96. // background: ''
  97. }, 'jpg').then((result) => {
  98. // console.log(result)
  99. process.stdout.write(result);
  100. });
  101. // toImg(barcode('test')).then(result => {
  102. // // console.log(result)
  103. // process.stdout.write(result)
  104. // })
  105. } else module.exports = render;