operationManualService.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const fs = require('fs');
  3. // node.js 路径操作对象
  4. const path = require('path');
  5. const Service = require('../service/baseService');
  6. class OperationManualService extends Service {
  7. tag() {
  8. return this.ctx.model.OperationManualModel;
  9. }
  10. // 上传前先删除原有文件
  11. async upload(query) {
  12. const { service } = this;
  13. const { id } = query;
  14. if (id) {
  15. const oneFile = await this.one(id);
  16. if (oneFile.filePath && oneFile.filePath !== '') {
  17. // 同步删除 unlinkSync (异步 : unlink)
  18. const dicPath = path.join(this.app.config.defaultUploadPath, oneFile.fileName);
  19. if (fs.existsSync(dicPath)) {
  20. fs.unlinkSync(dicPath);
  21. }
  22. }
  23. const url = await service.imageHandleService.upload(oneFile.fileName);
  24. if (url) {
  25. const data = {};
  26. data.filePath = url;
  27. data.time = Date.now();
  28. return await this.update(id, data);
  29. }
  30. }
  31. return null;
  32. }
  33. }
  34. module.exports = OperationManualService;