@kne/fastify-file-manager 0.1.1-alpha.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const fp = require('fastify-plugin');
2
- const autoload = require('@fastify/autoload');
3
2
  const path = require('path');
4
3
  const fs = require('fs-extra');
5
4
 
@@ -19,12 +18,15 @@ module.exports = fp(
19
18
  options
20
19
  );
21
20
  await fs.ensureDir(options.root);
22
- await fastify.sequelize.addModels(path.resolve(__dirname, './models'));
23
-
24
21
  fastify.register(require('@fastify/multipart'), options.multipart);
25
- fastify.register(autoload, {
26
- dir: path.resolve(__dirname, './libs'),
27
- options
22
+ fastify.register(require('@kne/fastify-namespace'), {
23
+ name: 'fileManager',
24
+ options,
25
+ modules: [
26
+ ['models', await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'))],
27
+ ['services', path.resolve(__dirname, './libs/services')],
28
+ ['controllers', path.resolve(__dirname, './libs/controllers')]
29
+ ]
28
30
  });
29
31
  fastify.register(
30
32
  require('@fastify/static'),
@@ -1,6 +1,7 @@
1
1
  const fp = require('fastify-plugin');
2
2
 
3
3
  module.exports = fp(async (fastify, options) => {
4
+ const { services } = fastify.fileManager;
4
5
  fastify.post(
5
6
  `${options.prefix}/upload`,
6
7
  {
@@ -12,7 +13,7 @@ module.exports = fp(async (fastify, options) => {
12
13
  throw new Error('不能获取到上传文件');
13
14
  }
14
15
  //1. 保存到服务器目录 2.对接oss
15
- return await fastify.fileManagerServices.uploadToFileSystem({ file, namespace: options.namespace });
16
+ return await services.fileRecord.uploadToFileSystem({ file, namespace: options.namespace });
16
17
  }
17
18
  );
18
19
 
@@ -32,7 +33,7 @@ module.exports = fp(async (fastify, options) => {
32
33
  },
33
34
  async request => {
34
35
  const { id } = request.params;
35
- return await fastify.fileManagerServices.getFileUrl({ id, namespace: options.namespace });
36
+ return await services.fileRecord.getFileUrl({ id, namespace: options.namespace });
36
37
  }
37
38
  );
38
39
 
@@ -60,7 +61,7 @@ module.exports = fp(async (fastify, options) => {
60
61
  async (request, reply) => {
61
62
  const { id } = request.params;
62
63
  const { attachment, filename: targetFilename } = request.query;
63
- const { targetFileName, filename } = await fastify.fileManagerServices.getFileInfo({
64
+ const { targetFileName, filename } = await services.fileRecord.getFileInfo({
64
65
  id,
65
66
  namespace: options.namespace
66
67
  });
@@ -81,7 +82,7 @@ module.exports = fp(async (fastify, options) => {
81
82
  perPage: 20,
82
83
  currentPage: 1
83
84
  });
84
- return await fastify.fileManagerServices.getFileList({
85
+ return await services.fileRecord.getFileList({
85
86
  filter,
86
87
  namespace: options.namespace,
87
88
  perPage,
@@ -106,7 +107,7 @@ module.exports = fp(async (fastify, options) => {
106
107
  },
107
108
  async request => {
108
109
  const { id } = request.body;
109
- await fastify.fileManagerServices.deleteFile({ id, namespace: options.namespace });
110
+ await services.fileRecord.deleteFile({ id, namespace: options.namespace });
110
111
  return {};
111
112
  }
112
113
  );
@@ -0,0 +1,43 @@
1
+ module.exports = (sequelize, DataTypes) => {
2
+ return sequelize.define(
3
+ 'fileRecord',
4
+ {
5
+ id: {
6
+ type: DataTypes.UUID,
7
+ defaultValue: DataTypes.UUIDV4,
8
+ primaryKey: true
9
+ },
10
+ filename: {
11
+ type: DataTypes.STRING,
12
+ allowNull: false
13
+ },
14
+ hash: {
15
+ type: DataTypes.STRING,
16
+ allowNull: false
17
+ },
18
+ namespace: {
19
+ type: DataTypes.STRING,
20
+ defaultValue: 'default'
21
+ },
22
+ size: {
23
+ type: DataTypes.INTEGER,
24
+ allowNull: false
25
+ },
26
+ encoding: DataTypes.STRING,
27
+ mimetype: DataTypes.STRING
28
+ },
29
+ {
30
+ indexes: [
31
+ {
32
+ fields: ['namespace']
33
+ },
34
+ {
35
+ fields: ['filename']
36
+ },
37
+ {
38
+ fields: ['hash']
39
+ }
40
+ ]
41
+ }
42
+ );
43
+ };
@@ -4,6 +4,7 @@ const crypto = require('crypto');
4
4
  const path = require('path');
5
5
 
6
6
  module.exports = fp(async (fastify, options) => {
7
+ const { models, services } = fastify.fileManager;
7
8
  const uploadToFileSystem = async ({ file, namespace }) => {
8
9
  const { filename, encoding, mimetype } = file;
9
10
  const buffer = await file.toBuffer();
@@ -13,9 +14,9 @@ module.exports = fp(async (fastify, options) => {
13
14
  const extension = path.extname(filename);
14
15
  const filepath = path.resolve(options.root, `${digest}${extension}`);
15
16
  await fs.writeFile(filepath, buffer);
16
- return await fastify.models.fileManager.create({
17
+ return await models.fileRecord.create({
17
18
  filename,
18
- namespace,
19
+ namespace: namespace || options.namespace,
19
20
  encoding,
20
21
  mimetype,
21
22
  hash: digest,
@@ -24,8 +25,8 @@ module.exports = fp(async (fastify, options) => {
24
25
  };
25
26
 
26
27
  const getFileUrl = async ({ id, namespace }) => {
27
- const file = await fastify.models.fileManager.findByPk(id, {
28
- where: { namespace }
28
+ const file = await models.fileRecord.findByPk(id, {
29
+ where: { namespace: namespace || options.namespace }
29
30
  });
30
31
  if (!file) {
31
32
  throw new Error('文件不存在');
@@ -35,8 +36,8 @@ module.exports = fp(async (fastify, options) => {
35
36
  };
36
37
 
37
38
  const getFileInfo = async ({ id, namespace }) => {
38
- const file = await fastify.models.fileManager.findByPk(id, {
39
- where: { namespace }
39
+ const file = await models.fileRecord.findByPk(id, {
40
+ where: { namespace: namespace || options.namespace }
40
41
  });
41
42
  if (!file) {
42
43
  throw new Error('文件不存在');
@@ -48,8 +49,8 @@ module.exports = fp(async (fastify, options) => {
48
49
  };
49
50
 
50
51
  const getFileList = async ({ filter, namespace, currentPage, perPage }) => {
51
- const queryFilter = { namespace };
52
- const { count, rows } = await fastify.models.fileManager.findAndCountAll({
52
+ const queryFilter = { namespace: namespace || options.namespace };
53
+ const { count, rows } = await models.fileRecord.findAndCountAll({
53
54
  where: queryFilter,
54
55
  offset: currentPage * (currentPage - 1),
55
56
  limit: perPage
@@ -61,8 +62,8 @@ module.exports = fp(async (fastify, options) => {
61
62
  };
62
63
 
63
64
  const deleteFile = async ({ id, namespace }) => {
64
- const file = await fastify.models.fileManager.findByPk(id, {
65
- where: { namespace }
65
+ const file = await models.fileRecord.findByPk(id, {
66
+ where: { namespace: namespace || options.namespace }
66
67
  });
67
68
  if (!file) {
68
69
  throw new Error('文件不存在');
@@ -70,6 +71,5 @@ module.exports = fp(async (fastify, options) => {
70
71
 
71
72
  await file.destroy();
72
73
  };
73
-
74
- fastify.decorate('fileManagerServices', { uploadToFileSystem, getFileUrl, getFileInfo, getFileList, deleteFile });
74
+ services.fileRecord = { uploadToFileSystem, getFileUrl, getFileInfo, getFileList, deleteFile };
75
75
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-file-manager",
3
- "version": "0.1.1-alpha.1",
3
+ "version": "1.0.1",
4
4
  "description": "用于管理静态文件上传查看等",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,8 +18,7 @@
18
18
  },
19
19
  "files": [
20
20
  "index.js",
21
- "libs",
22
- "models"
21
+ "libs"
23
22
  ],
24
23
  "repository": {
25
24
  "type": "git",
@@ -33,16 +32,16 @@
33
32
  },
34
33
  "homepage": "https://github.com/kne-union/fastify-file-manager#readme",
35
34
  "devDependencies": {
36
- "@kne/fastify-sequelize": "^0.1.3",
35
+ "@kne/fastify-sequelize": "^1.0.0",
37
36
  "fastify": "^4.27.0",
38
37
  "husky": "^9.0.11",
39
38
  "prettier": "^3.2.5",
40
39
  "sqlite3": "^5.1.7"
41
40
  },
42
41
  "dependencies": {
43
- "@fastify/autoload": "^5.8.2",
44
42
  "@fastify/multipart": "^8.2.0",
45
43
  "@fastify/static": "^7.0.4",
44
+ "@kne/fastify-namespace": "^0.1.0",
46
45
  "fastify-plugin": "^4.5.1",
47
46
  "fs-extra": "^11.2.0"
48
47
  }
@@ -1,23 +0,0 @@
1
- module.exports = (sequelize, DataTypes) => {
2
- return sequelize.define('fileManager', {
3
- id: {
4
- type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true
5
- }, filename: {
6
- type: DataTypes.STRING, allowNull: false
7
- }, hash: {
8
- type: DataTypes.STRING, allowNull: false
9
- }, namespace: {
10
- type: DataTypes.STRING, defaultValue: 'default'
11
- }, size: {
12
- type: DataTypes.INTEGER, allowNull: false
13
- }, encoding: DataTypes.STRING, mimetype: DataTypes.STRING
14
- }, {
15
- indexes: [{
16
- fields: ['namespace']
17
- }, {
18
- fields: ['filename']
19
- }, {
20
- fields: ['hash']
21
- }]
22
- });
23
- };