@opengis/fastify-table 1.4.37 → 1.4.39

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.4.37",
3
+ "version": "1.4.39",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -45,17 +45,22 @@ async function writeFileToDisk(file, buffer) {
45
45
  return null;
46
46
  }
47
47
 
48
- export default async function uploadMultiPart(req, { subdir = 'uploads', originalFilename = false } = {}) {
48
+ export default async function uploadMultiPart(req, { subdir, originalFilename = false } = {}) {
49
49
  const allowedExtensions = {
50
50
  '/file/upload-image/*': images,
51
51
  }[req.routeOptions?.url || ''] || all;
52
52
 
53
53
  const parts = req.parts();
54
- const part = await parts.next();
54
+ // const part = await parts.next();
55
+ const { value: part, done } = await parts.next();
55
56
 
56
- const value = part?.value?.fields?.file || part?.value;
57
+ if (done) {
58
+ return null;
59
+ }
60
+
61
+ const value = part?.fields?.file || part;
57
62
 
58
- if (!value?.filename) {
63
+ if (!value?.file || !value?.filename) {
59
64
  throw new Error('upload error');
60
65
  }
61
66
 
@@ -66,13 +71,18 @@ export default async function uploadMultiPart(req, { subdir = 'uploads', origina
66
71
  throw new Error('file extension is not allowed');
67
72
  }
68
73
 
69
- const buffer = await value.toBuffer();
74
+ const buffer = await new Promise((res, rej) => {
75
+ const chunks = [];
76
+ part.file.on('data', chunk => chunks.push(chunk));
77
+ part.file.on('end', () => res(Buffer.concat(chunks)));
78
+ part.file.on('error', rej);
79
+ });
70
80
 
71
81
  if (!buffer?.length) {
72
82
  throw new Error('file buffer is empty');
73
83
  }
74
84
 
75
- const dir = req.params?.['*'] || subdir;
85
+ const dir = subdir != null ? subdir : (req.params?.['*'] || 'uploads');
76
86
  const yearMonthDay = subdir != null ? '' : (new Date()).toISOString().split('T')[0];
77
87
 
78
88
  const dbname = req.pg?.options?.database || req.pg?.database || config.pg?.database; // request / config params / default config params
@@ -14,23 +14,26 @@ export default async function upload(req) {
14
14
  });
15
15
  if (hookData) return hookData;
16
16
 
17
- const extName = path.extname(file.filepath).slice(1).toLowerCase();
18
-
19
- const resultInsert = await dataInsert({
17
+ const resultInsert = pg ? await dataInsert({
20
18
  pg,
21
19
  table: 'crm.files',
22
20
  data: {
23
- uploaded_name: file?.originalFilename?.toLocaleLowerCase(),
24
- file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
25
- ext: extName,
26
- size: file?.size,
21
+ uploaded_name: file.originalFilename?.toLocaleLowerCase?.(),
22
+ file_path: file.relativeFilepath?.replace?.(/\\/g, '/'),
23
+ ext: file.extension,
24
+ size: file.size,
27
25
  file_status: 1,
28
26
  entity_id: objectId,
29
27
  },
30
28
  uid: req.user?.uid,
31
- }).then(el => el?.rows?.[0] || {});
29
+ }).then(el => el?.rows?.[0] || {}) : {};
32
30
 
33
- const { dir: relativeDirpath, base: fileName } = resultInsert?.file_path ? path.parse(resultInsert.file_path) : '';
31
+ const { dir: relativeDirpath, base: fileName } = resultInsert?.file_path
32
+ ? path.parse(resultInsert.file_path)
33
+ : {
34
+ relativeDirpath: path.dirname(file.relativeFilepath),
35
+ fileName: file.newFilename,
36
+ };
34
37
 
35
38
  // req.log.info('upload', { filepath: file?.filepath, uid: session.passport?.user?.uid });
36
39
  return {
@@ -38,13 +41,13 @@ export default async function upload(req) {
38
41
  res: 'ok',
39
42
  name: file?.originalFilename,
40
43
  result: {
41
- file_id: resultInsert.file_id,
42
- format: resultInsert.ext || extName,
43
- size: resultInsert.size || file?.size,
44
- entity_id: resultInsert.entity_id,
45
- file_path: resultInsert.file_path || file?.relativeFilepath?.replace(/\\/g, '/'),
46
- file_name: file?.originalFilename.toLocaleLowerCase(),
47
- dir: relativeDirpath?.replace(/\\/g, '/'),
44
+ file_id: resultInsert?.file_id,
45
+ format: resultInsert?.ext || file.extension,
46
+ size: resultInsert?.size || file?.size,
47
+ entity_id: resultInsert?.entity_id,
48
+ file_path: resultInsert?.file_path || file?.relativeFilepath?.replace(/\\/g, '/'),
49
+ file_name: file?.originalFilename?.toLocaleLowerCase?.(),
50
+ dir: relativeDirpath?.replace?.(/\\/g, '/'),
48
51
  native_file_name: fileName,
49
52
  },
50
53
  },