api.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. const fsize = fontSize || defaultFontSize;
  58. const ny = y + fsize;
  59. gmObj
  60. .stroke(defaultColor, 1)
  61. .fill(color || defaultColor)
  62. .fontSize(fsize)
  63. .drawText(x, ny, text);
  64. return;
  65. }
  66. // circle
  67. if (track.type === 'circle') {
  68. const { x0, y0, x1, y1 } = track.option as DrawTrackCircleOption;
  69. const rx = (x1 - x0) / 2;
  70. const ry = (y1 - y0) / 2;
  71. const cx = x0 + rx;
  72. const cy = y0 + ry;
  73. gmObj
  74. .stroke(defaultColor, 2)
  75. .fill('none')
  76. .drawEllipse(cx, cy, rx, ry, 0, 360);
  77. return;
  78. }
  79. // line
  80. if (track.type === 'line') {
  81. const { x0, y0, x1, y1 } = track.option as DrawTrackLineOption;
  82. gmObj.stroke(defaultColor, 2).fill('none').drawLine(x0, y0, x1, y1);
  83. }
  84. });
  85. gmObj.write(outpath, (err) => {
  86. if (!err) {
  87. return resolve(outpath);
  88. }
  89. return reject(err);
  90. });
  91. });
  92. }
  93. async function downloadFile(url: string, outputPath: string): Promise<string> {
  94. return new Promise((resolve, reject) => {
  95. axios({
  96. url,
  97. method: 'GET',
  98. responseType: 'arraybuffer',
  99. })
  100. .then((response) => {
  101. fs.writeFileSync(outputPath, Buffer.from(response.data, 'binary'));
  102. resolve(outputPath);
  103. })
  104. .catch(() => {
  105. reject();
  106. });
  107. });
  108. }
  109. async function downloadImage(url: string, outputPath: string) {
  110. makeDirSync(path.dirname(outputPath));
  111. await downloadFile(url, outputPath);
  112. const size = sizeOf(outputPath);
  113. return {
  114. url: outputPath,
  115. width: size.width || 100,
  116. height: size.height || 100,
  117. };
  118. }
  119. function joinPath(paths: string[]) {
  120. return path.join(...paths);
  121. }
  122. interface ImageItem {
  123. url: string;
  124. width: number;
  125. height: number;
  126. }
  127. async function imagesToPdf(
  128. images: ImageItem[],
  129. outpath: string
  130. ): Promise<string> {
  131. return new Promise((resolve, reject) => {
  132. const doc = new PDFDocument({ autoFirstPage: false });
  133. makeDirSync(path.dirname(outpath));
  134. const steam = fs.createWriteStream(outpath);
  135. doc.pipe(steam);
  136. images.forEach((image) => {
  137. const { url, width, height } = image;
  138. doc.addPage({ size: [width, height] });
  139. doc.image(url, 0, 0, { width, height });
  140. });
  141. doc.end();
  142. steam.on('finish', () => {
  143. resolve(outpath);
  144. });
  145. steam.on('error', (err) => {
  146. reject(err);
  147. });
  148. });
  149. }
  150. function logger(content: string, type?: 'info' | 'error') {
  151. if (type === 'error') {
  152. log.error(content);
  153. } else {
  154. log.info(content);
  155. }
  156. }
  157. const commonApi = {
  158. cropImage,
  159. drawTrack,
  160. joinPath,
  161. downloadImage,
  162. imagesToPdf,
  163. logger,
  164. };
  165. export type CommonApi = typeof commonApi;
  166. export default commonApi;