files.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const moment = require('moment');
  4. const { sep, extname } = require('path');
  5. const fs = require('fs');
  6. const awaitWriteStream = require('await-stream-ready').write;
  7. const sendToWormhole = require('stream-wormhole');
  8. const assert = require('assert');
  9. const wxstream = require('stream');
  10. const ffmpeg = require('fluent-ffmpeg');
  11. const { BusinessError, ErrorCode } = require('naf-core').Error;
  12. class FilesController extends Controller {
  13. async upload() {
  14. const { ctx, app } = this;
  15. const { appid, catalog, item } = ctx.params;
  16. assert(appid);
  17. const stream = await ctx.getFileStream();
  18. ctx.logger.debug(stream);
  19. const rootPath = `${app.config.cdn.repos_root_path}`;
  20. const rootUrl = `${app.config.cdn.repos_root_url}`;
  21. const dirs = [ appid ];
  22. if (catalog && catalog !== '_') {
  23. const subs = catalog.split('_');
  24. dirs.push(...subs);
  25. }
  26. const saved = await this.saveFile(rootPath, dirs, stream, item);
  27. const uri = `${rootUrl}/${dirs.join('/')}/${saved.fileName}`;
  28. ctx.body = { errcode: 0, errmsg: 'ok', id: saved.id, name: saved.fileName, uri };
  29. }
  30. async saveFile(rootPath, dirs, stream, name) {
  31. const ctx = this.ctx;
  32. const ext = extname(stream.filename).toLowerCase();
  33. // TODO: 指定文件名或者按时间生成文件名
  34. // const name = moment().format('YYYYMMDDHHmmss');
  35. if (!name) name = moment().format('YYYYMMDDHHmmss');
  36. // TODO: 检查根路径是否存在,不存在则创建
  37. ctx.logger.debug('rootPath: ', rootPath);
  38. if (!fs.existsSync(rootPath)) {
  39. ctx.logger.debug('create dir: ', rootPath);
  40. fs.mkdirSync(rootPath);
  41. }
  42. // TODO: 检查分级目录是否存在,不存在则创建
  43. for (let i = 0; i < dirs.length; i++) {
  44. const p = `${rootPath}${sep}${dirs.slice(0, i + 1).join(sep)}`;
  45. if (!fs.existsSync(p)) {
  46. fs.mkdirSync(p);
  47. }
  48. }
  49. const filePath = `${rootPath}${sep}${dirs.join(sep)}${sep}`;
  50. const fileName = `${name}${ext}`;
  51. const writeStream = fs.createWriteStream(filePath + fileName);
  52. try {
  53. await awaitWriteStream(stream.pipe(writeStream));
  54. } catch (err) {
  55. await sendToWormhole(stream);
  56. throw err;
  57. }
  58. return { filePath, fileName, id: name };
  59. }
  60. async wxupload() {
  61. const { serverid, type, appid } = this.ctx.request.body;
  62. assert(serverid, 'serverid不允许为空');
  63. assert(type, 'type不允许为空');
  64. const { ctx, app } = this;
  65. const url = app.config.wx.wxdown(appid, serverid);
  66. const file = await this.ctx.curl(url);
  67. console.log(url);
  68. const stream = new wxstream.PassThrough();
  69. stream.end(file.data);
  70. const rootPath = `${app.config.cdn.repos_root_path}`;
  71. const rootUrl = `${app.config.cdn.repos_root_url}`;
  72. let uri;
  73. // 判断类型 0、图片 1、音频 2、视频
  74. if (type === '0') {
  75. const dirs = [ 'wximage' ];
  76. const saved = await this.wxsaveFile(rootPath, dirs, stream, '.jpg');
  77. uri = `${rootUrl}/${dirs.join('/')}/${saved.fileName}`;
  78. } else if (type === '1') {
  79. const dirs = [ 'wxadio' ];
  80. const saved = await this.wxsaveFile(rootPath, dirs, stream, '.amr');
  81. // 音频时如果需要使用转码,以下调用ffmpeg进行转码处理
  82. // const hz = '.mp3';
  83. const resultname = `${saved.id}.amr`;
  84. // const ffurl = `${saved.filePath}${resultname}`;
  85. // if (!ffmpeg) throw new BusinessError(ErrorCode.SERVICE_FAULT, '转换失败');
  86. // new ffmpeg({ source: `${saved.filePath}${saved.fileName}` })
  87. // .saveToFile(ffurl, function(retcode, error) {
  88. // })
  89. // .on('end', function() {
  90. // // 在这里处理完成后的结果
  91. // console.log('Finished processing');
  92. // });
  93. uri = `${rootUrl}/${dirs.join('/')}/${resultname}`;
  94. } else if (type === '2') {
  95. const dirs = [ 'wxvideo' ];
  96. const saved = await this.wxsaveFile(rootPath, dirs, stream, '');
  97. uri = `${rootUrl}/${dirs.join('/')}/${saved.fileName}`;
  98. }
  99. ctx.body = { errcode: 0, errmsg: 'ok', uri };
  100. }
  101. async wxsaveFile(rootPath, dirs, stream, ext) {
  102. const ctx = this.ctx;
  103. // TODO: 指定文件名或者按时间生成文件名
  104. const name = moment().format('YYYYMMDDHHmmss');
  105. // TODO: 检查根路径是否存在,不存在则创建
  106. ctx.logger.debug('rootPath: ', rootPath);
  107. if (!fs.existsSync(rootPath)) {
  108. ctx.logger.debug('create dir: ', rootPath);
  109. fs.mkdirSync(rootPath);
  110. }
  111. // TODO: 检查分级目录是否存在,不存在则创建
  112. for (let i = 0; i < dirs.length; i++) {
  113. const p = `${rootPath}${sep}${dirs.slice(0, i + 1).join(sep)}`;
  114. if (!fs.existsSync(p)) {
  115. fs.mkdirSync(p);
  116. }
  117. }
  118. const filePath = `${rootPath}${sep}${dirs.join(sep)}${sep}`;
  119. const fileName = `${name}${ext}`;
  120. console.log(filePath + fileName);
  121. const writeStream = fs.createWriteStream(filePath + fileName);
  122. try {
  123. await awaitWriteStream(stream.pipe(writeStream));
  124. } catch (err) {
  125. await sendToWormhole(stream);
  126. throw err;
  127. }
  128. return { filePath, fileName, id: name };
  129. }
  130. }
  131. module.exports = FilesController;