example.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 uploadFileToSMMS(tmpFile) {
  10. const res = {
  11. code: 1000,
  12. message: 'unknown error',
  13. };
  14. try {
  15. //throw new Error('Sync Error');
  16. const headersObj = {
  17. 'Content-Type': 'multipart/form-data',
  18. 'Authorization': 'pHVaIfVX8kgxsEL2THTYMVzJDYY3MMZU'
  19. };
  20. const url = 'https://sm.ms/api/v2/upload';
  21. const response = await this.app.curl(url, {
  22. method: 'POST',
  23. headers: headersObj,
  24. files: {
  25. smfile: tmpFile,
  26. },
  27. dataType: 'json',
  28. timeout: 15000,
  29. });
  30. const result = response.data;
  31. if (this.app.config.env === 'local') {
  32. this.app.logger.info('[ExampleService] [uploadFileToSMMS]: info result:%j', result);
  33. }
  34. if (result.code !== 'success') {
  35. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: res error result:%j', result);
  36. }
  37. return result;
  38. } catch (e) {
  39. this.app.logger.error('[ExampleService] [uploadFileToSMMS]: ERROR ', e);
  40. }
  41. return res;
  42. }
  43. }
  44. module.exports = ExampleService;