@kne/fastify-file-manager 1.0.1 → 1.1.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 -1
- package/libs/models/file-record.js +11 -9
- package/libs/services/file-record.js +10 -8
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -23,7 +23,12 @@ module.exports = fp(
|
|
|
23
23
|
name: 'fileManager',
|
|
24
24
|
options,
|
|
25
25
|
modules: [
|
|
26
|
-
[
|
|
26
|
+
[
|
|
27
|
+
'models',
|
|
28
|
+
await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
|
|
29
|
+
prefix: 't_file_manager_'
|
|
30
|
+
})
|
|
31
|
+
],
|
|
27
32
|
['services', path.resolve(__dirname, './libs/services')],
|
|
28
33
|
['controllers', path.resolve(__dirname, './libs/controllers')]
|
|
29
34
|
]
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
module.exports = (
|
|
2
|
-
return
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
id: {
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
uuid: {
|
|
6
5
|
type: DataTypes.UUID,
|
|
7
|
-
defaultValue: DataTypes.UUIDV4
|
|
8
|
-
primaryKey: true
|
|
6
|
+
defaultValue: DataTypes.UUIDV4
|
|
9
7
|
},
|
|
10
8
|
filename: {
|
|
11
9
|
type: DataTypes.STRING,
|
|
@@ -26,8 +24,12 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
26
24
|
encoding: DataTypes.STRING,
|
|
27
25
|
mimetype: DataTypes.STRING
|
|
28
26
|
},
|
|
29
|
-
{
|
|
27
|
+
options: {
|
|
30
28
|
indexes: [
|
|
29
|
+
{
|
|
30
|
+
unique: true,
|
|
31
|
+
fields: ['uuid', 'deleted_at']
|
|
32
|
+
},
|
|
31
33
|
{
|
|
32
34
|
fields: ['namespace']
|
|
33
35
|
},
|
|
@@ -39,5 +41,5 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
39
41
|
}
|
|
40
42
|
]
|
|
41
43
|
}
|
|
42
|
-
|
|
44
|
+
};
|
|
43
45
|
};
|
|
@@ -14,7 +14,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
14
14
|
const extension = path.extname(filename);
|
|
15
15
|
const filepath = path.resolve(options.root, `${digest}${extension}`);
|
|
16
16
|
await fs.writeFile(filepath, buffer);
|
|
17
|
-
|
|
17
|
+
const outputFile = await models.fileRecord.create({
|
|
18
18
|
filename,
|
|
19
19
|
namespace: namespace || options.namespace,
|
|
20
20
|
encoding,
|
|
@@ -22,11 +22,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
22
22
|
hash: digest,
|
|
23
23
|
size: buffer.byteLength
|
|
24
24
|
});
|
|
25
|
+
return Object.assign({}, outputFile.get({ plain: true }), { id: outputFile.uuid });
|
|
25
26
|
};
|
|
26
27
|
|
|
27
28
|
const getFileUrl = async ({ id, namespace }) => {
|
|
28
|
-
const file = await models.fileRecord.
|
|
29
|
-
where: { namespace: namespace || options.namespace }
|
|
29
|
+
const file = await models.fileRecord.findOne({
|
|
30
|
+
where: { uuid: id, namespace: namespace || options.namespace }
|
|
30
31
|
});
|
|
31
32
|
if (!file) {
|
|
32
33
|
throw new Error('文件不存在');
|
|
@@ -36,14 +37,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
36
37
|
};
|
|
37
38
|
|
|
38
39
|
const getFileInfo = async ({ id, namespace }) => {
|
|
39
|
-
const file = await models.fileRecord.
|
|
40
|
-
where: { namespace: namespace || options.namespace }
|
|
40
|
+
const file = await models.fileRecord.findOne({
|
|
41
|
+
where: { uuid: id, namespace: namespace || options.namespace }
|
|
41
42
|
});
|
|
42
43
|
if (!file) {
|
|
43
44
|
throw new Error('文件不存在');
|
|
44
45
|
}
|
|
45
46
|
const extension = path.extname(file.filename);
|
|
46
47
|
return Object.assign({}, file, {
|
|
48
|
+
id: file.uuid,
|
|
47
49
|
targetFileName: `${file.hash}${extension}`
|
|
48
50
|
});
|
|
49
51
|
};
|
|
@@ -56,14 +58,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
56
58
|
limit: perPage
|
|
57
59
|
});
|
|
58
60
|
return {
|
|
59
|
-
pageData: rows,
|
|
61
|
+
pageData: rows.map(item => Object.assign({}, item.get({ plain: true }), { id: item.uuid })),
|
|
60
62
|
totalCount: count
|
|
61
63
|
};
|
|
62
64
|
};
|
|
63
65
|
|
|
64
66
|
const deleteFile = async ({ id, namespace }) => {
|
|
65
|
-
const file = await models.fileRecord.
|
|
66
|
-
where: { namespace: namespace || options.namespace }
|
|
67
|
+
const file = await models.fileRecord.findOne({
|
|
68
|
+
where: { uuid: id, namespace: namespace || options.namespace }
|
|
67
69
|
});
|
|
68
70
|
if (!file) {
|
|
69
71
|
throw new Error('文件不存在');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-file-manager",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "用于管理静态文件上传查看等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/kne-union/fastify-file-manager#readme",
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@kne/fastify-sequelize": "^
|
|
35
|
+
"@kne/fastify-sequelize": "^2.0.1",
|
|
36
36
|
"fastify": "^4.27.0",
|
|
37
37
|
"husky": "^9.0.11",
|
|
38
38
|
"prettier": "^3.2.5",
|