example.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. const BaseController = require('../base');
  3. const os = require('os');
  4. const fs = require('fs');
  5. const path = require('path');
  6. class ExampleController extends BaseController {
  7. async openLocalDir() {
  8. const self = this;
  9. const { ctx, service } = this;
  10. const body = ctx.request.body;
  11. const id = body.id;
  12. const data = {};
  13. let dir = '';
  14. switch (id) {
  15. case 'download' :
  16. dir = os.userInfo().homedir + '/Downloads';
  17. break;
  18. case 'picture' :
  19. dir = os.userInfo().homedir + '/Pictures';
  20. break;
  21. case 'doc' :
  22. dir = os.userInfo().homedir + '/Documents';
  23. break;
  24. case 'music' :
  25. dir = os.userInfo().homedir + '/Music';
  26. break;
  27. }
  28. await service.example.openLocalDir(dir);
  29. self.sendSuccess(data);
  30. }
  31. async uploadFile() {
  32. const self = this;
  33. const { ctx, service } = this;
  34. let tmpDir = service.storage.getStorageDir();
  35. // for (const file of ctx.request.files) {
  36. // this.app.logger.info('file:', file);
  37. // try {
  38. // let tmpFile = fs.readFileSync(file.filepath)
  39. // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  40. // } finally {
  41. // await fs.unlink(file.filepath, function(){});
  42. // }
  43. // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  44. // const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  45. // }
  46. const file = ctx.request.files[0];
  47. //this.app.logger.info('file:', file);
  48. try {
  49. let tmpFile = fs.readFileSync(file.filepath)
  50. fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
  51. } finally {
  52. await fs.unlink(file.filepath, function(){});
  53. }
  54. const fileStream = fs.createReadStream(path.join(tmpDir, file.filename))
  55. const uploadRes = await service.example.uploadFileToSMMS(fileStream);
  56. self.sendData(uploadRes);
  57. }
  58. async getWsUrl() {
  59. const self = this;
  60. const { service } = this;
  61. const data = {};
  62. const addr = await service.socket.getWsUrl();
  63. data.url = addr;
  64. self.sendSuccess(data);
  65. }
  66. async addTestData() {
  67. const self = this;
  68. const { service } = this;
  69. const data = {};
  70. const userInfo = {
  71. name: 'jame',
  72. age: 18,
  73. gender: 'man'
  74. }
  75. await service.storage.addTestData(userInfo);
  76. self.sendSuccess(data);
  77. }
  78. async delTestData() {
  79. const self = this;
  80. const { service } = this;
  81. const data = {};
  82. const name = 'jame';
  83. await service.storage.delTestData(name);
  84. self.sendSuccess(data);
  85. }
  86. async updateTestData() {
  87. const self = this;
  88. const { service } = this;
  89. const data = {};
  90. const name = 'jame';
  91. const age = 20;
  92. await service.storage.updateTestData(name, age);
  93. self.sendSuccess(data);
  94. }
  95. async getTestData() {
  96. const self = this;
  97. const { service } = this;
  98. const data = {};
  99. const name = 'jame';
  100. const user = await service.storage.getTestData(name);
  101. data.user = user;
  102. self.sendSuccess(data);
  103. }
  104. }
  105. module.exports = ExampleController;