example.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const BaseService = require('./base');
  3. class ExampleService extends BaseService {
  4. async openLocalDir(dir) {
  5. const self = this;
  6. await self.ipcCall('example.openDir', dir);
  7. return true;
  8. }
  9. async executeJS(str) {
  10. const self = this;
  11. let result = await self.ipcCall('example.executeJS', str);
  12. return result;
  13. }
  14. async uploadFileToSMMS(tmpFile) {
  15. const res = {
  16. code: 1000,
  17. message: 'unknown error',
  18. };
  19. try {
  20. //throw new Error('Sync Error');
  21. const headersObj = {
  22. 'Content-Type': 'multipart/form-data',
  23. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  24. };
  25. const url = 'https://sm.ms/api/v2/upload';
  26. const response = await this.app.curl(url, {
  27. method: 'POST',
  28. headers: headersObj,
  29. files: {
  30. smfile: tmpFile,
  31. },
  32. dataType: 'json',
  33. timeout: 15000,
  34. });
  35. const result = response.data;
  36. if (this.app.config.env === 'local') {
  37. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  38. }
  39. if (result.code !== 'success') {
  40. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  41. }
  42. return result;
  43. } catch (e) {
  44. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  45. }
  46. return res;
  47. }
  48. }
  49. module.exports = ExampleService;