1234567891011121314151617181920212223242526272829303132333435363738 |
- 'use strict';
- const fs = require('fs');
- // node.js 路径操作对象
- const path = require('path');
- const Service = require('../service/baseService');
- class OperationManualService extends Service {
- tag() {
- return this.ctx.model.OperationManualModel;
- }
- // 上传前先删除原有文件
- async upload(query) {
- const { service } = this;
- const { id } = query;
- if (id) {
- const oneFile = await this.one(id);
- if (oneFile.filePath && oneFile.filePath !== '') {
- // 同步删除 unlinkSync (异步 : unlink)
- const dicPath = path.join(this.app.config.defaultUploadPath, oneFile.fileName);
- if (fs.existsSync(dicPath)) {
- fs.unlinkSync(dicPath);
- }
- }
- const url = await service.imageHandleService.upload(oneFile.fileName);
- if (url) {
- const data = {};
- data.filePath = url;
- data.time = Date.now();
- return await this.update(id, data);
- }
- }
- return null;
- }
- }
- module.exports = OperationManualService;
|