base.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const path = require('path');
  4. const awaitWriteStream = require('await-stream-ready').write;
  5. const sendToWormhole = require('stream-wormhole');//管道读入一个虫洞
  6. const fs = require('fs');
  7. /**
  8. * 基类控制器
  9. */
  10. class BaseController extends Controller {
  11. /**
  12. * [uploadOne 单文件上传功能]
  13. * @author szjcomo
  14. * @createTime 2020-08-14
  15. * @param {[type]} savePath [description]
  16. * @param {[type]} filename [description]
  17. * @return {[type]} [description]
  18. */
  19. async _uploadOne(savePath,filename) {
  20. let that = this;
  21. filename = filename || (that.app.szjcomo.date('YmdHis') + '' + that.app.szjcomo.mt_rand(100000,999999));
  22. return await that._uploadHandler(await that.ctx.getFileStream(),savePath,filename);
  23. }
  24. /**
  25. * [uploadAll 批量上传文件功能]
  26. * @author szjcomo
  27. * @createTime 2020-08-14
  28. * @param {[type]} savePath [description]
  29. * @return {[type]} [description]
  30. */
  31. async _uploadAll(savePath) {
  32. let that = this;
  33. let part;
  34. let result = [];
  35. let fields = {};
  36. let parts = await that.ctx.multipart();
  37. while ((part = await parts()) != null) {
  38. if (part.length) {
  39. fields[part[0]] = part[1];
  40. } else {
  41. // 定义文件名
  42. let filename = (that.app.szjcomo.date('YmdHis') + '' + that.app.szjcomo.mt_rand(100000,999999));
  43. // part 是上传的文件流
  44. result.push(await that._uploadHandler(part,savePath,filename));
  45. }
  46. }
  47. return result;
  48. }
  49. /**
  50. * [uploadHandler 上传的文件流]
  51. * @Author szjcomo
  52. * @DateTime 2019-10-05
  53. * @param {[type]} stream [description]
  54. * @return {[type]} [description]
  55. */
  56. async _uploadHandler(stream,savePath,filename){
  57. let that = this;
  58. let ext = path.extname(filename);
  59. if(that.app.szjcomo.empty(ext)) filename += path.extname(stream.filename);
  60. // 生成文件路径
  61. that.app.szjcomo.mkdir(savePath);
  62. // 目标文件
  63. let target = path.join(savePath, filename);
  64. // 创建文件流
  65. let writeStream = fs.createWriteStream(target);
  66. try{
  67. //异步把文件流 写入
  68. await awaitWriteStream(stream.pipe(writeStream));
  69. } catch(err){
  70. //如果出现错误,关闭管道
  71. await sendToWormhole(stream);
  72. throw err;
  73. }
  74. await sendToWormhole(stream);
  75. return {save_path:savePath,save_name:filename,file_sha1:that.app.szjcomo.FileSHA1(target),
  76. fields:stream.fields,file_size:writeStream.bytesWritten,mime:stream.mime,encoding:stream.encoding,
  77. origin_name:stream.filename,file_md5:that.app.szjcomo.FileMD5(target),ext:path.extname(stream.filename)}
  78. }
  79. /**
  80. * [appFileLogs 文件记录位置]
  81. * @param {[type]} fileName [description]
  82. * @param {[type]} err [description]
  83. * @return {[type]} [description]
  84. */
  85. async logs(fileName,err) {
  86. let that = this;
  87. let filePath = `${that.app.baseDir}/logs/${that.app.szjcomo.date('Y-m-d')}`;
  88. fileName = fileName + '.txt';
  89. await that.app.szjcomo.mkdir(filePath);
  90. let isFile = await that.service.base.fileExists(path.join(filePath,fileName));
  91. let filedata = `=========${that.app.szjcomo.date('Y-m-d H:i:s')}==========\r\n${err.stack}\r\n=========${that.app.szjcomo.date('Y-m-d H:i:s')}==========\r\n\r\n`;
  92. if(isFile) {
  93. fs.appendFileSync(path.join(filePath,fileName),filedata);
  94. } else {
  95. fs.writeFileSync(path.join(filePath,fileName), filedata);
  96. }
  97. }
  98. }
  99. module.exports = BaseController;