api.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import gm from 'gm';
  2. import axios from 'axios';
  3. import sizeOf from 'image-size';
  4. import path from 'node:path';
  5. import fs from 'node:fs';
  6. import PDFDocument from 'pdfkit';
  7. import log from 'electron-log/renderer';
  8. import {
  9. getImagicPath,
  10. getTempPath,
  11. makeDirSync,
  12. getGmFontPath,
  13. } from './utils';
  14. import {
  15. DrawTrackItem,
  16. DrawTrackCircleOption,
  17. DrawTrackLineOption,
  18. DrawTrackTextOption,
  19. } from './types';
  20. // macos install gm imagemagick https://github.com/aheckmann/gm/blob/master/README.md
  21. const gmInst =
  22. process.platform === 'win32'
  23. ? gm.subClass({
  24. imageMagick: '7+',
  25. appPath: getImagicPath(),
  26. })
  27. : gm.subClass({ imageMagick: '7+' });
  28. function cropImage(imgPath: string): Promise<string> {
  29. return new Promise((resolve, reject) => {
  30. const outpath = path.join(getTempPath(), '001.png');
  31. gmInst(imgPath)
  32. .crop(500, 200, 0, 0)
  33. .write(outpath, (err) => {
  34. if (!err) {
  35. return resolve(outpath);
  36. }
  37. return reject(err);
  38. });
  39. });
  40. }
  41. function drawTrack(
  42. imgPath: string,
  43. drawTrackList: DrawTrackItem[],
  44. outpath: string
  45. ): Promise<string> {
  46. return new Promise((resolve, reject) => {
  47. const gmObj = gmInst(imgPath);
  48. makeDirSync(path.dirname(outpath));
  49. const defaultColor = '#f53f3f';
  50. const defaultFontSize = 22;
  51. gmObj.font(getGmFontPath());
  52. drawTrackList.forEach((track) => {
  53. // text
  54. if (track.type === 'text') {
  55. const { x, y, text, color, fontSize } =
  56. track.option as DrawTrackTextOption;
  57. gmObj
  58. .fill(color || defaultColor)
  59. .fontSize(fontSize || defaultFontSize)
  60. .drawText(x, y, text);
  61. return;
  62. }
  63. // circle
  64. if (track.type === 'circle') {
  65. const { x0, y0, x1, y1 } = track.option as DrawTrackCircleOption;
  66. gmObj.drawCircle(x0, y0, x1, y1).stroke(defaultColor, 2);
  67. return;
  68. }
  69. // line
  70. if (track.type === 'line') {
  71. const { x0, y0, x1, y1 } = track.option as DrawTrackLineOption;
  72. gmObj.drawLine(x0, y0, x1, y1).stroke(defaultColor, 2);
  73. }
  74. });
  75. gmObj.write(outpath, (err) => {
  76. if (!err) {
  77. return resolve(outpath);
  78. }
  79. return reject(err);
  80. });
  81. });
  82. }
  83. async function downloadFile(url: string, outputPath: string): Promise<string> {
  84. return new Promise((resolve, reject) => {
  85. axios({
  86. url,
  87. method: 'GET',
  88. responseType: 'arraybuffer',
  89. })
  90. .then((response) => {
  91. fs.writeFileSync(outputPath, Buffer.from(response.data, 'binary'));
  92. resolve(outputPath);
  93. })
  94. .catch(() => {
  95. reject();
  96. });
  97. });
  98. }
  99. async function downloadImage(url: string, outputPath: string) {
  100. makeDirSync(path.dirname(outputPath));
  101. await downloadFile(url, outputPath);
  102. const size = sizeOf(outputPath);
  103. return {
  104. url: outputPath,
  105. width: size.width || 100,
  106. height: size.height || 100,
  107. };
  108. }
  109. function joinPath(paths: string[]) {
  110. return path.join(...paths);
  111. }
  112. interface ImageItem {
  113. url: string;
  114. width: number;
  115. height: number;
  116. }
  117. async function imagesToPdf(
  118. images: ImageItem[],
  119. outpath: string
  120. ): Promise<string> {
  121. return new Promise((resolve, reject) => {
  122. const doc = new PDFDocument({ autoFirstPage: false });
  123. makeDirSync(path.dirname(outpath));
  124. const steam = fs.createWriteStream(outpath);
  125. doc.pipe(steam);
  126. images.forEach((image) => {
  127. const { url, width, height } = image;
  128. doc.addPage({ size: [width, height] });
  129. doc.image(url, 0, 0, { width, height });
  130. });
  131. doc.end();
  132. steam.on('finish', () => {
  133. resolve(outpath);
  134. });
  135. steam.on('error', (err) => {
  136. reject(err);
  137. });
  138. });
  139. }
  140. function logger(content: string, type?: 'info' | 'error') {
  141. if (type === 'error') {
  142. log.error(content);
  143. } else {
  144. log.info(content);
  145. }
  146. }
  147. const commonApi = {
  148. cropImage,
  149. drawTrack,
  150. joinPath,
  151. downloadImage,
  152. imagesToPdf,
  153. logger,
  154. };
  155. export type CommonApi = typeof commonApi;
  156. export default commonApi;