api.ts 4.3 KB

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