@kne/fastify-file-manager 1.1.3 → 1.2.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 +6 -3
- package/libs/controllers/index.js +15 -9
- package/libs/models/file-record.js +6 -1
- package/libs/services/file-record.js +38 -5
- package/package.json +5 -2
package/index.js
CHANGED
|
@@ -13,9 +13,12 @@ module.exports = fp(
|
|
|
13
13
|
dbTableNamePrefix: 't_file_manager_',
|
|
14
14
|
multipart: {},
|
|
15
15
|
static: {},
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
ossAdapter: () => {
|
|
17
|
+
return {};
|
|
18
|
+
},
|
|
19
|
+
createAuthenticate: () => {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
19
22
|
},
|
|
20
23
|
options
|
|
21
24
|
);
|
|
@@ -5,7 +5,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
5
5
|
fastify.post(
|
|
6
6
|
`${options.prefix}/upload`,
|
|
7
7
|
{
|
|
8
|
-
onRequest:
|
|
8
|
+
onRequest: options.createAuthenticate('file:write'),
|
|
9
9
|
schema: {
|
|
10
10
|
query: {
|
|
11
11
|
type: 'object',
|
|
@@ -31,7 +31,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
31
31
|
fastify.get(
|
|
32
32
|
`${options.prefix}/file-url/:id`,
|
|
33
33
|
{
|
|
34
|
-
onRequest:
|
|
34
|
+
onRequest: options.createAuthenticate('file:read'),
|
|
35
35
|
schema: {
|
|
36
36
|
params: {
|
|
37
37
|
type: 'object',
|
|
@@ -51,7 +51,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
51
51
|
fastify.get(
|
|
52
52
|
`${options.prefix}/file-id/:id`,
|
|
53
53
|
{
|
|
54
|
-
onRequest:
|
|
54
|
+
onRequest: options.createAuthenticate('file:read'),
|
|
55
55
|
schema: {
|
|
56
56
|
query: {
|
|
57
57
|
type: 'object',
|
|
@@ -72,17 +72,23 @@ module.exports = fp(async (fastify, options) => {
|
|
|
72
72
|
async (request, reply) => {
|
|
73
73
|
const { id } = request.params;
|
|
74
74
|
const { attachment, filename: targetFilename } = request.query;
|
|
75
|
-
const {
|
|
75
|
+
const { filePath, targetFile, filename, mimetype, ...props } = await services.fileRecord.getFileInfo({
|
|
76
76
|
id
|
|
77
77
|
});
|
|
78
|
-
|
|
78
|
+
if (targetFile) {
|
|
79
|
+
const outputFilename = encodeURIComponent(targetFilename || filename);
|
|
80
|
+
reply.header('Content-Type', mimetype);
|
|
81
|
+
reply.header('Content-Disposition', attachment ? `attachment; filename="${outputFilename}"` : `filename="${outputFilename}"`);
|
|
82
|
+
return reply.send(targetFile);
|
|
83
|
+
}
|
|
84
|
+
return attachment ? reply.download(filePath, targetFilename || filename) : reply.sendFile(filePath);
|
|
79
85
|
}
|
|
80
86
|
);
|
|
81
87
|
|
|
82
88
|
fastify.post(
|
|
83
89
|
`${options.prefix}/file-list`,
|
|
84
90
|
{
|
|
85
|
-
onRequest:
|
|
91
|
+
onRequest: options.createAuthenticate('file:mange'),
|
|
86
92
|
schema: {
|
|
87
93
|
body: {
|
|
88
94
|
type: 'object',
|
|
@@ -123,7 +129,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
123
129
|
fastify.post(
|
|
124
130
|
`${options.prefix}/replace-file`,
|
|
125
131
|
{
|
|
126
|
-
onRequest:
|
|
132
|
+
onRequest: options.createAuthenticate('file:mange'),
|
|
127
133
|
schema: {
|
|
128
134
|
type: 'object',
|
|
129
135
|
properties: {
|
|
@@ -143,7 +149,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
143
149
|
fastify.post(
|
|
144
150
|
`${options.prefix}/rename-file`,
|
|
145
151
|
{
|
|
146
|
-
onRequest:
|
|
152
|
+
onRequest: options.createAuthenticate('file:mange'),
|
|
147
153
|
schema: {
|
|
148
154
|
type: 'object',
|
|
149
155
|
properties: {
|
|
@@ -161,7 +167,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
161
167
|
fastify.post(
|
|
162
168
|
`${options.prefix}/delete-files`,
|
|
163
169
|
{
|
|
164
|
-
onRequest:
|
|
170
|
+
onRequest: options.createAuthenticate('file:mange'),
|
|
165
171
|
schema: {
|
|
166
172
|
body: {
|
|
167
173
|
type: 'object',
|
|
@@ -22,7 +22,12 @@ module.exports = ({ DataTypes }) => {
|
|
|
22
22
|
allowNull: false
|
|
23
23
|
},
|
|
24
24
|
encoding: DataTypes.STRING,
|
|
25
|
-
mimetype: DataTypes.STRING
|
|
25
|
+
mimetype: DataTypes.STRING,
|
|
26
|
+
storageType: {
|
|
27
|
+
type: DataTypes.STRING,
|
|
28
|
+
allowNull: false,
|
|
29
|
+
comment: '存储类型:local本地文件系统,oss远程oss存储'
|
|
30
|
+
}
|
|
26
31
|
},
|
|
27
32
|
options: {
|
|
28
33
|
indexes: [
|
|
@@ -2,6 +2,7 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
const fs = require('fs-extra');
|
|
3
3
|
const crypto = require('crypto');
|
|
4
4
|
const path = require('path');
|
|
5
|
+
const { NotFound } = require('http-errors');
|
|
5
6
|
|
|
6
7
|
module.exports = fp(async (fastify, options) => {
|
|
7
8
|
const { models, services } = fastify.fileManager;
|
|
@@ -13,8 +14,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
13
14
|
hash.update(buffer);
|
|
14
15
|
const digest = hash.digest('hex');
|
|
15
16
|
const extension = path.extname(filename);
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
let storageType;
|
|
19
|
+
const ossServices = options.ossAdapter();
|
|
20
|
+
if (typeof ossServices.uploadFile === 'function') {
|
|
21
|
+
await ossServices.uploadFile({ file: buffer, filename: `${digest}${extension}` });
|
|
22
|
+
storageType = 'oss';
|
|
23
|
+
} else {
|
|
24
|
+
const filepath = path.resolve(options.root, `${digest}${extension}`);
|
|
25
|
+
await fs.writeFile(filepath, buffer);
|
|
26
|
+
storageType = 'local';
|
|
27
|
+
}
|
|
18
28
|
|
|
19
29
|
const outputFile = await (async create => {
|
|
20
30
|
if (!id) {
|
|
@@ -29,6 +39,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
29
39
|
file.mimetype = mimetype;
|
|
30
40
|
file.hash = digest;
|
|
31
41
|
file.size = buffer.byteLength;
|
|
42
|
+
file.storageType = storageType;
|
|
32
43
|
await file.save();
|
|
33
44
|
return file;
|
|
34
45
|
})(() =>
|
|
@@ -38,7 +49,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
38
49
|
encoding,
|
|
39
50
|
mimetype,
|
|
40
51
|
hash: digest,
|
|
41
|
-
size: buffer.byteLength
|
|
52
|
+
size: buffer.byteLength,
|
|
53
|
+
storageType
|
|
42
54
|
})
|
|
43
55
|
);
|
|
44
56
|
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
@@ -52,6 +64,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
52
64
|
throw new Error('文件不存在');
|
|
53
65
|
}
|
|
54
66
|
const extension = path.extname(file.filename);
|
|
67
|
+
const ossServices = options.ossAdapter();
|
|
68
|
+
if (file.storageType === 'oss' && typeof ossServices.getFileLink !== 'function') {
|
|
69
|
+
throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
|
|
70
|
+
}
|
|
71
|
+
if (file.storageType === 'oss') {
|
|
72
|
+
return await ossServices.getFileLink({ filename: `${file.hash}${extension}` });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!(await fs.exists(`${options.root}/${file.hash}${extension}`))) {
|
|
76
|
+
throw new NotFound();
|
|
77
|
+
}
|
|
55
78
|
return `${options.prefix}/file/${file.hash}${extension}?filename=${file.filename}`;
|
|
56
79
|
};
|
|
57
80
|
|
|
@@ -63,9 +86,19 @@ module.exports = fp(async (fastify, options) => {
|
|
|
63
86
|
throw new Error('文件不存在');
|
|
64
87
|
}
|
|
65
88
|
const extension = path.extname(file.filename);
|
|
66
|
-
|
|
89
|
+
const targetFileName = `${file.hash}${extension}`;
|
|
90
|
+
const ossServices = options.ossAdapter();
|
|
91
|
+
if (file.storageType === 'oss' && typeof ossServices.downloadFile !== 'function') {
|
|
92
|
+
throw new Error('ossAdapter未正确配置无法读取oss类型存储文件');
|
|
93
|
+
}
|
|
94
|
+
let targetFile;
|
|
95
|
+
if (file.storageType === 'oss') {
|
|
96
|
+
targetFile = await ossServices.downloadFile({ filename: targetFileName });
|
|
97
|
+
}
|
|
98
|
+
return Object.assign({}, file.get({ pain: true }), {
|
|
67
99
|
id: file.uuid,
|
|
68
|
-
|
|
100
|
+
filePath: targetFileName,
|
|
101
|
+
targetFile
|
|
69
102
|
});
|
|
70
103
|
};
|
|
71
104
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-file-manager",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "用于管理静态文件上传查看等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/kne-union/fastify-file-manager#readme",
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@fastify/env": "^4.4.0",
|
|
36
|
+
"@kne/fastify-aliyun": "^1.1.1",
|
|
35
37
|
"@kne/fastify-sequelize": "^2.0.1",
|
|
36
38
|
"fastify": "^4.27.0",
|
|
37
39
|
"husky": "^9.0.11",
|
|
@@ -44,6 +46,7 @@
|
|
|
44
46
|
"@fastify/static": "^7.0.4",
|
|
45
47
|
"@kne/fastify-namespace": "^0.1.0",
|
|
46
48
|
"fastify-plugin": "^4.5.1",
|
|
47
|
-
"fs-extra": "^11.2.0"
|
|
49
|
+
"fs-extra": "^11.2.0",
|
|
50
|
+
"http-errors": "^2.0.0"
|
|
48
51
|
}
|
|
49
52
|
}
|