files.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 amrToMp3 = require('amrToMp3');
  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 } = this.ctx.request.body;
  62. assert(serverid, 'serverid不允许为空');
  63. assert(type, 'type不允许为空');
  64. const { ctx, app } = this;
  65. const url = app.config.wx.wxdown + serverid;
  66. const file = await this.ctx.curl(url);
  67. const stream = new wxstream.PassThrough();
  68. stream.end(file.data);
  69. const rootPath = `${app.config.cdn.repos_root_path}`;
  70. const rootUrl = `${app.config.cdn.repos_root_url}`;
  71. let uri;
  72. // 判断类型 0、图片 1、音频 2、视频
  73. if (type === '0') {
  74. const dirs = [ 'wximage' ];
  75. const saved = await this.wxsaveFile(rootPath, dirs, stream, '.jpg');
  76. uri = `${rootUrl}/${dirs.join('/')}/${saved.fileName}`;
  77. } else if (type === '1') {
  78. const dirs = [ 'wxadio' ];
  79. const saved = await this.wxsaveFile(rootPath, dirs, stream, '.amr');
  80. // 音频时如果需要使用转码,以下调用ffmpeg进行转码处理
  81. const hz = '.mp3';
  82. const dir_path = `${app.config.cdn.wxadio_path}`;
  83. const resultname = `${saved.id}`;
  84. console.log(`dir_path=>${dir_path}`);
  85. console.log(`resultname=>${resultname}`);
  86. console.log(`p1=>${dir_path}${resultname}.amr`);
  87. // const data = await amrToMp3(`${dir_path}${resultname}.amr`, `${dir_path}`, `${resultname}`);
  88. // console.log(data);
  89. amrToMp3(`${dir_path}${resultname}.amr`, `${dir_path}`, `${resultname}`)
  90. .then(data => {
  91. console.log(data);
  92. }).catch(e => {
  93. console.log(e);
  94. });
  95. // const ffurl = `${saved.filePath}${resultname}`;
  96. // new ffmpeg({ source: `${saved.filePath}${saved.fileName}` })
  97. // .saveToFile(ffurl, function(retcode, error) {
  98. // })
  99. // .on('end', function() {
  100. // // 在这里处理完成后的结果
  101. // console.log('Finished processing');
  102. // });
  103. uri = `${rootUrl}/${dirs.join('/')}/${resultname}${hz}`;
  104. } else if (type === '2') {
  105. const dirs = [ 'wxvideo' ];
  106. const saved = await this.wxsaveFile(rootPath, dirs, stream, '');
  107. uri = `${rootUrl}/${dirs.join('/')}/${saved.fileName}`;
  108. }
  109. ctx.body = { errcode: 0, errmsg: 'ok', uri };
  110. }
  111. async wxsaveFile(rootPath, dirs, stream, ext) {
  112. const ctx = this.ctx;
  113. // TODO: 指定文件名或者按时间生成文件名
  114. const name = moment().format('YYYYMMDDHHmmss');
  115. // TODO: 检查根路径是否存在,不存在则创建
  116. ctx.logger.debug('rootPath: ', rootPath);
  117. if (!fs.existsSync(rootPath)) {
  118. ctx.logger.debug('create dir: ', rootPath);
  119. fs.mkdirSync(rootPath);
  120. }
  121. // TODO: 检查分级目录是否存在,不存在则创建
  122. for (let i = 0; i < dirs.length; i++) {
  123. const p = `${rootPath}${sep}${dirs.slice(0, i + 1).join(sep)}`;
  124. if (!fs.existsSync(p)) {
  125. fs.mkdirSync(p);
  126. }
  127. }
  128. const filePath = `${rootPath}${sep}${dirs.join(sep)}${sep}`;
  129. const fileName = `${name}${ext}`;
  130. console.log(filePath + fileName);
  131. const writeStream = fs.createWriteStream(filePath + fileName);
  132. try {
  133. await awaitWriteStream(stream.pipe(writeStream));
  134. } catch (err) {
  135. await sendToWormhole(stream);
  136. throw err;
  137. }
  138. return { filePath, fileName, id: name };
  139. }
  140. }
  141. module.exports = FilesController;