@kne/fastify-file-manager 0.1.0 → 1.0.0
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
|
|
|
@@ -9,7 +8,7 @@ module.exports = fp(
|
|
|
9
8
|
{
|
|
10
9
|
root: path.join(process.cwd(), 'static'),
|
|
11
10
|
namespace: 'default',
|
|
12
|
-
prefix: '/static',
|
|
11
|
+
prefix: '/api/static',
|
|
13
12
|
multipart: {},
|
|
14
13
|
static: {},
|
|
15
14
|
authenticateFileRead: async () => {},
|
|
@@ -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(
|
|
26
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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,15 +14,7 @@ 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
|
-
|
|
17
|
-
filename,
|
|
18
|
-
namespace,
|
|
19
|
-
encoding,
|
|
20
|
-
mimetype,
|
|
21
|
-
hash: digest,
|
|
22
|
-
size: buffer.byteLength
|
|
23
|
-
});
|
|
24
|
-
return await fastify.models.fileManager.create({
|
|
17
|
+
return await models.fileRecord.create({
|
|
25
18
|
filename,
|
|
26
19
|
namespace,
|
|
27
20
|
encoding,
|
|
@@ -32,7 +25,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
32
25
|
};
|
|
33
26
|
|
|
34
27
|
const getFileUrl = async ({ id, namespace }) => {
|
|
35
|
-
const file = await
|
|
28
|
+
const file = await models.fileRecord.findByPk(id, {
|
|
36
29
|
where: { namespace }
|
|
37
30
|
});
|
|
38
31
|
if (!file) {
|
|
@@ -43,7 +36,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
43
36
|
};
|
|
44
37
|
|
|
45
38
|
const getFileInfo = async ({ id, namespace }) => {
|
|
46
|
-
const file = await
|
|
39
|
+
const file = await models.fileRecord.findByPk(id, {
|
|
47
40
|
where: { namespace }
|
|
48
41
|
});
|
|
49
42
|
if (!file) {
|
|
@@ -57,7 +50,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
57
50
|
|
|
58
51
|
const getFileList = async ({ filter, namespace, currentPage, perPage }) => {
|
|
59
52
|
const queryFilter = { namespace };
|
|
60
|
-
const { count, rows } = await
|
|
53
|
+
const { count, rows } = await models.fileRecord.findAndCountAll({
|
|
61
54
|
where: queryFilter,
|
|
62
55
|
offset: currentPage * (currentPage - 1),
|
|
63
56
|
limit: perPage
|
|
@@ -69,7 +62,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
69
62
|
};
|
|
70
63
|
|
|
71
64
|
const deleteFile = async ({ id, namespace }) => {
|
|
72
|
-
const file = await
|
|
65
|
+
const file = await models.fileRecord.findByPk(id, {
|
|
73
66
|
where: { namespace }
|
|
74
67
|
});
|
|
75
68
|
if (!file) {
|
|
@@ -78,6 +71,5 @@ module.exports = fp(async (fastify, options) => {
|
|
|
78
71
|
|
|
79
72
|
await file.destroy();
|
|
80
73
|
};
|
|
81
|
-
|
|
82
|
-
fastify.decorate('fileManagerServices', { uploadToFileSystem, getFileUrl, getFileInfo, getFileList, deleteFile });
|
|
74
|
+
services.fileRecord = { uploadToFileSystem, getFileUrl, getFileInfo, getFileList, deleteFile };
|
|
83
75
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-file-manager",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
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.
|
|
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
|
}
|
package/models/file-manager.js
DELETED
|
@@ -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
|
-
};
|