imageHandleService.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. const fs = require('fs');
  3. // node.js 路径操作对象
  4. const path = require('path');
  5. // 异步二进制 写入流
  6. const awaitWriteStream = require('await-stream-ready').write;
  7. // 管道读入一个虫洞。
  8. const sendToWormhole = require('stream-wormhole');
  9. const axios = require('axios');
  10. const FormData = require('form-data');
  11. const Service = require('egg').Service;
  12. class ImageHandleService extends Service {
  13. async upload(filename) {
  14. const ctx = this.ctx;
  15. const stream = await ctx.getFileStream();// 获取文件
  16. const fileNameT = stream.filename;
  17. const fileType = fileNameT.split('.')[fileNameT.split('.').length - 1].toLowerCase();
  18. if (fileType === 'png' || fileType === 'jpg' || fileType === 'jpeg') {
  19. if (ctx.request.headers['content-length'] / 1024 < 1024) { // 上传文件大小限制
  20. // let stream;
  21. try {
  22. if (!filename) {
  23. // 新建一个文件名 = 文件名 + 时间 + 文件后缀
  24. filename = path.parse(stream.filename).name
  25. + Date.now() + path.extname(stream.filename)
  26. .toLocaleLowerCase();
  27. }
  28. // TODO 修改成OSS -CH (已完成)
  29. // OSS (STS) Bucket
  30. await this.ctx.oss.get('bucket1').putStream(this.app.config.defaultWriteAliOSSPath + filename, stream);
  31. return this.app.config.defaultWritePathPre + filename;
  32. } catch (err) {
  33. ctx.logger.debug(err);
  34. // 如果出现错误,关闭管道
  35. // await sendToWormhole(stream);
  36. return null;
  37. }
  38. } else {
  39. return null;
  40. }
  41. } else {
  42. return null;
  43. }
  44. }
  45. // 上传操作手册,限制只能为docx的格式
  46. async uploadOperationManual(filename) {
  47. const ctx = this.ctx;
  48. const stream = await ctx.getFileStream();// 获取文件
  49. const fileNameT = stream.filename;
  50. const fileType = fileNameT.split('.')[fileNameT.split('.').length - 1];
  51. if (fileType === 'docx') {
  52. try {
  53. if (!filename) {
  54. // 新建一个文件名 = 文件名 + 时间 + 文件后缀
  55. filename = path.parse(stream.filename).name
  56. + Date.now() + path.extname(stream.filename)
  57. .toLocaleLowerCase();
  58. }
  59. // TODO 修改成OSS -CH (已完成)
  60. // OSS Bucket
  61. await this.ctx.oss.get('bucket1').putStream(this.app.config.operationManualPath + filename, stream);
  62. return this.app.config.defaultWriteFilePathPre + filename;
  63. } catch (err) {
  64. ctx.logger.debug(err);
  65. return null;
  66. }
  67. } else {
  68. return null;
  69. }
  70. }
  71. async uploadImageXyService(filename, filepath, clientType) {
  72. const { ctx } = this;
  73. const filenameObs = filename.substring(1); // 上传至obs的 需要去掉起始的/
  74. const mydata = new FormData();
  75. mydata.append('objkey', 'sckjpicup$' + filenameObs);
  76. mydata.append('objdata', fs.createReadStream(path.resolve(__dirname, filepath)));
  77. const requestResult = await axios({
  78. method: 'post',
  79. url: 'http://10.201.147.2:8080/obs-demo/objUpload',
  80. data: mydata,
  81. headers: mydata.getHeaders(),
  82. });
  83. if (requestResult.status === 200) {
  84. if (clientType === 'PC') {
  85. const arr = [];
  86. arr.push({ url: filename });
  87. return arr;
  88. } else if (clientType === 'wxInfo') {
  89. return { url: filename };
  90. } else if (clientType === 'SCAN') {
  91. return filename;
  92. }
  93. ctx.logger('回显信息错误,请联系管理员!');
  94. } else {
  95. ctx.logger('上传文件错误,请联系管理员!');
  96. }
  97. }
  98. }
  99. module.exports = ImageHandleService;