浏览代码

修改文件删除路径

asd123a20 3 年之前
父节点
当前提交
02c94932f9
共有 2 个文件被更改,包括 6 次插入6 次删除
  1. 5 5
      app/service/files.js
  2. 1 1
      config/config.default.js

+ 5 - 5
app/service/files.js

@@ -25,7 +25,7 @@ class FilesService extends Service {
     assert(id, 'id不存在');
     const { Files: model } = this.ctx.model;
     try {
-      await model.findByIdAndUpdate(id, { name, path });
+      await model.findById(id).update({ name, path });
       return { errmsg: '', errcode: 0 };
     } catch (error) {
       throw new Error({ errcode: -2001, errmsg: '修改失败' });
@@ -37,9 +37,9 @@ class FilesService extends Service {
     try {
       // 获取文件路径
       const files = await model.findById(id);
-      const filepath = `${files.path}`;
+      const filepath = `${this.app.config.filespath}${files.path}`;
       // 开始删除文件
-      fs.unlinkSync(path.join(this.app.baseDir, `/app${filepath}`));
+      fs.unlinkSync(path.join(filepath));
       await model.findById(id).remove();
       return { errmsg: '', errcode: 0 };
     } catch (error) {
@@ -74,8 +74,8 @@ class FilesService extends Service {
       const index = filename.indexOf('.');
       const fileType = filename.slice(index, filename.length);
       const uuid = uuidv1();
-      const filepath = `${this.app.config.filespath}${type === 'files' ? 'files' : 'resource'}/${uuid}${fileType}`;
-      const paths = path.join(filepath);
+      const filepath = `/filespath/${type === 'files' ? 'files' : 'resource'}/${uuid}${fileType}`;
+      const paths = path.join(`${this.app.config.filespath}${filepath}`);
       // 存储
       const result = await new Promise((resolve, reject) => {
         const remoteFileStream = fs.createWriteStream(paths);

+ 1 - 1
config/config.default.js

@@ -47,7 +47,7 @@ module.exports = appInfo => {
     secret: '123456',
   };
   config.userSecret = '123456';
-  config.filespath = '/home/cms/filespath/';
+  config.filespath = '/home/cms';
   // 数据库配置
   config.mongoose = {
     url: 'mongodb://172.17.116.7:27018/cms',