@opengis/fastify-table 1.4.54 → 1.4.56
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/package.json
CHANGED
|
@@ -46,7 +46,9 @@ async function writeFileToDisk(file, buffer) {
|
|
|
46
46
|
return null;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export default async function uploadMultiPart(req, {
|
|
49
|
+
export default async function uploadMultiPart(req, {
|
|
50
|
+
extensions, raw, subdir, originalFilename = false,
|
|
51
|
+
} = {}) {
|
|
50
52
|
const allowedExtensions = {
|
|
51
53
|
'/file/upload-image/*': images,
|
|
52
54
|
}[req.routeOptions?.url || ''] || all;
|
|
@@ -67,22 +69,28 @@ export default async function uploadMultiPart(req, { subdir, originalFilename =
|
|
|
67
69
|
|
|
68
70
|
const ext = path.extname(value.filename).toLowerCase();
|
|
69
71
|
|
|
72
|
+
if (Array.isArray(extensions) && !extensions.includes(ext)) {
|
|
73
|
+
throw new Error('file extension is not allowed');
|
|
74
|
+
}
|
|
75
|
+
|
|
70
76
|
// check extension
|
|
71
77
|
if (!allowedExtensions.includes(ext.substring(1))) {
|
|
72
78
|
throw new Error('file extension is not allowed');
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
const
|
|
81
|
+
const fileBuffer = await new Promise((res, rej) => {
|
|
76
82
|
const chunks = [];
|
|
77
83
|
value.file.on('data', chunk => chunks.push(chunk));
|
|
78
84
|
value.file.on('end', () => res(Buffer.concat(chunks)));
|
|
79
85
|
value.file.on('error', rej);
|
|
80
86
|
});
|
|
81
87
|
|
|
82
|
-
if (!
|
|
88
|
+
if (!fileBuffer?.length) {
|
|
83
89
|
throw new Error('file buffer is empty');
|
|
84
90
|
}
|
|
85
91
|
|
|
92
|
+
if (raw) return { buffer: fileBuffer, filename: value.filename, ext }; // return buffer for custom upload
|
|
93
|
+
|
|
86
94
|
const dir = subdir != null ? subdir : (req.params?.['*'] || 'uploads');
|
|
87
95
|
const yearMonthDay = subdir != null ? '' : (new Date()).toISOString().split('T')[0];
|
|
88
96
|
|
|
@@ -104,18 +112,18 @@ export default async function uploadMultiPart(req, { subdir, originalFilename =
|
|
|
104
112
|
filepath: path.join(folder, newFilename).replace(/\\/g, '/'),
|
|
105
113
|
filetype: getFileType(newFilename),
|
|
106
114
|
relativeFilepath: path.join(reldirpath, newFilename).replace(/\\/g, '/'),
|
|
107
|
-
size: Buffer.byteLength(
|
|
115
|
+
size: Buffer.byteLength(fileBuffer),
|
|
108
116
|
mimetype: value.mimetype,
|
|
109
117
|
extension: ext.substring(1),
|
|
110
118
|
};
|
|
111
119
|
|
|
112
120
|
await mkdir(folder, { recursive: true });
|
|
113
|
-
await writeFileToDisk(file,
|
|
121
|
+
await writeFileToDisk(file, fileBuffer);
|
|
114
122
|
|
|
115
123
|
// move file to s3
|
|
116
124
|
if (config.s3?.endpoint) {
|
|
117
125
|
const s3 = providers();
|
|
118
|
-
await s3.uploadFile(file.relativeFilepath,
|
|
126
|
+
await s3.uploadFile(file.relativeFilepath, fileBuffer);
|
|
119
127
|
if (config.trace) console.log('upload to s3', file.relativeFilepath);
|
|
120
128
|
}
|
|
121
129
|
|