api.ts 4.0 KB

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