@opengis/fastify-table 1.4.34 → 1.4.36
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
|
@@ -3,6 +3,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
|
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
4
|
import { imageSize } from 'image-size';
|
|
5
5
|
|
|
6
|
+
import { existsSync } from 'node:fs';
|
|
6
7
|
import config from '../../../config.js';
|
|
7
8
|
|
|
8
9
|
import providers from './providers/index.js';
|
|
@@ -10,6 +11,7 @@ import providers from './providers/index.js';
|
|
|
10
11
|
import { all, images } from './utils/allowedExtensions.js';
|
|
11
12
|
|
|
12
13
|
import grpc from '../grpc/grpc.js';
|
|
14
|
+
import getFileType from './utils/getFileType.js';
|
|
13
15
|
|
|
14
16
|
const { resizeImage } = grpc();
|
|
15
17
|
|
|
@@ -43,7 +45,7 @@ async function writeFileToDisk(file, buffer) {
|
|
|
43
45
|
return null;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
export default async function uploadMultiPart(req) {
|
|
48
|
+
export default async function uploadMultiPart(req, { subdir = 'uploads', originalFilename = false } = {}) {
|
|
47
49
|
const allowedExtensions = {
|
|
48
50
|
'/file/upload-image/*': images,
|
|
49
51
|
}[req.routeOptions?.url || ''] || all;
|
|
@@ -70,8 +72,8 @@ export default async function uploadMultiPart(req) {
|
|
|
70
72
|
throw new Error('file buffer is empty');
|
|
71
73
|
}
|
|
72
74
|
|
|
73
|
-
const dir = req.params?.['*'] ||
|
|
74
|
-
const yearMonthDay = (new Date()).toISOString().split('T')[0];
|
|
75
|
+
const dir = req.params?.['*'] || subdir;
|
|
76
|
+
const yearMonthDay = subdir != null ? '' : (new Date()).toISOString().split('T')[0];
|
|
75
77
|
|
|
76
78
|
const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
|
|
77
79
|
|
|
@@ -79,11 +81,17 @@ export default async function uploadMultiPart(req) {
|
|
|
79
81
|
const reldirpath = path.join('/files', dir, yearMonthDay);
|
|
80
82
|
const folder = path.join(rootDir, config.folder || '', reldirpath);
|
|
81
83
|
|
|
82
|
-
const newFilename = `${randomUUID()}${ext}`;
|
|
84
|
+
const newFilename = originalFilename ? value.filename : `${randomUUID()}${ext}`;
|
|
85
|
+
|
|
86
|
+
if (existsSync(path.join(folder, newFilename).replace(/\\/g, '/'))) {
|
|
87
|
+
throw new Error('file with specified name already exists in directory');
|
|
88
|
+
}
|
|
83
89
|
|
|
84
90
|
const file = {
|
|
85
91
|
originalFilename: value.filename,
|
|
92
|
+
newFilename,
|
|
86
93
|
filepath: path.join(folder, newFilename).replace(/\\/g, '/'),
|
|
94
|
+
filetype: getFileType(newFilename),
|
|
87
95
|
relativeFilepath: path.join(reldirpath, newFilename).replace(/\\/g, '/'),
|
|
88
96
|
size: Buffer.byteLength(buffer),
|
|
89
97
|
mimetype: value.mimetype,
|
|
@@ -15,7 +15,7 @@ import getTemplate from '../../../plugins/table/funcs/getTemplate.js';
|
|
|
15
15
|
import getMeta from '../../../plugins/pg/funcs/getMeta.js';
|
|
16
16
|
import pgClients from '../../../plugins/pg/pgClients.js';
|
|
17
17
|
import eventStream from '../../../plugins/util/funcs/eventStream.js';
|
|
18
|
-
import getData from '
|
|
18
|
+
import getData from '../../../plugins/table/funcs/getData.js';
|
|
19
19
|
import getFolder from '../../../plugins/crud/funcs/utils/getFolder.js';
|
|
20
20
|
import metaFormat from '../../../plugins/table/funcs/metaFormat/index.js';
|
|
21
21
|
|
|
@@ -46,7 +46,6 @@ export default async function upload(req) {
|
|
|
46
46
|
file_name: file?.originalFilename.toLocaleLowerCase(),
|
|
47
47
|
dir: relativeDirpath?.replace(/\\/g, '/'),
|
|
48
48
|
native_file_name: fileName,
|
|
49
|
-
original: file?.relativeOriginalFilepath?.replace?.(/\\/g, '/'),
|
|
50
49
|
},
|
|
51
50
|
},
|
|
52
51
|
};
|