Ver Fonte

增加文件管理下载接口,增加文件管理网关配置

asd123a20 há 3 anos atrás
pai
commit
2f316c5e8a

+ 4 - 0
service-files/app/controller/filestore.js

@@ -14,6 +14,10 @@ class FilestoreController extends Controller {
     const res = await this.ctx.service.filestore.query(this.ctx.query);
     this.ctx.body = res;
   }
+  async filesDownload() {
+    const res = await this.ctx.service.filestore.filesDownload(this.ctx.query);
+    this.ctx.body = res;
+  }
 }
 
 module.exports = FilestoreController;

+ 1 - 0
service-files/app/router.js

@@ -8,4 +8,5 @@ module.exports = app => {
   router.post('/api/files/:app/upload', controller.filestore.upload);
   router.post('/api/files/filestore/delete', controller.filestore.delete);
   router.get('/api/files/filestore/query', controller.filestore.query);
+  router.get('/api/files/filestore/filesDownload', controller.filestore.filesDownload);
 };

+ 16 - 2
service-files/app/service/filestore.js

@@ -64,12 +64,16 @@ class FilestoreService extends Service {
       throw error;
     }
   }
-  async query({ skip, limit, filter }) {
+  async query({ skip, limit, fileName, name }) {
     try {
+      const filter = {};
+      if (name || fileName) filter.$or = [];
+      if (fileName) filter.$or.push({ code: { $regex: fileName } });
+      if (name) filter.$or.push({ name: { $regex: name } });
       let res;
       const total = await this.model.find({ ...filter });
       if (skip && limit) {
-        res = await this.model.find({ ...filter }).skip(skip * limit);
+        res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
       } else {
         res = await this.model.find({ ...filter });
       }
@@ -78,6 +82,16 @@ class FilestoreService extends Service {
       throw error;
     }
   }
+  // 文件下载
+  async filesDownload({ filePath }) {
+    const rootPath = `${this.app.config.root_path}`;
+    const res = await this.model.findOne({ filePath });
+    const url = `${rootPath}${filePath}`;
+    const postfix = url.split('.')[1];
+    this.ctx.attachment(`${res.name}.${postfix}`);
+    // this.ctx.set('Content-Type', 'application/octet-stream');
+    return fs.createReadStream(url);
+  }
 }
 
 module.exports = FilestoreService;

+ 1 - 0
service-files/config/config.default.js

@@ -48,6 +48,7 @@ module.exports = appInfo => {
   };
   // 文件存储路径
   config.repos_root_path = `${appInfo.baseDir}${sep}upload`;
+  config.root_path = `${appInfo.baseDir}`;
   // 是否启用持久化数据
   config.data_save = true;
   // 限制文件大小

+ 6 - 0
service-gateway/config/files.js

@@ -19,4 +19,10 @@ module.exports = [
     issuer: [ 'naf' ],
     log: false,
   },
+  {
+    url: '/api/files/filestore/filesDownload',
+    jwt: false,
+    issuer: [ 'naf' ],
+    log: false,
+  },
 ];

+ 9 - 0
service-naf/config/menu.js

@@ -220,5 +220,14 @@ const data = [
     parentCode: 'paperBank',
     icon: 'el-icon-star-on',
   },
+  // 文件管理
+  {
+    module: 'files',
+    path: '/files/home',
+    title: '文件管理',
+    code: 'filesHome',
+    parentCode: null,
+    icon: 'el-icon-star-on',
+  },
 ];
 module.exports.data = data;