@opengis/fastify-table 1.4.8 → 1.4.10

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.
Files changed (71) hide show
  1. package/config.js +1 -0
  2. package/index.js +5 -0
  3. package/package.json +8 -3
  4. package/server/plugins/file/downloadFile.js +18 -0
  5. package/server/plugins/file/getExport.js +38 -0
  6. package/server/plugins/file/isFileExists.js +17 -0
  7. package/server/plugins/file/providers/fs.js +100 -0
  8. package/server/plugins/file/providers/index.d.ts +49 -0
  9. package/server/plugins/file/providers/index.js +36 -0
  10. package/server/plugins/file/providers/mime/index.js +12 -0
  11. package/server/plugins/file/providers/mime/mimes.js +1180 -0
  12. package/server/plugins/file/providers/s3/client.js +41 -0
  13. package/server/plugins/file/providers/s3/funcs/downloadFile.js +50 -0
  14. package/server/plugins/file/providers/s3/funcs/fileExists.js +32 -0
  15. package/server/plugins/file/providers/s3/funcs/uploadFile.js +38 -0
  16. package/server/plugins/file/providers/s3/funcs/utils/getS3FilePath.js +23 -0
  17. package/server/plugins/file/providers/s3/index.js +12 -0
  18. package/server/plugins/file/providers/utils/getDataSize.js +20 -0
  19. package/server/plugins/file/providers/utils/getValidData.js +32 -0
  20. package/server/plugins/file/providers/utils/handlers/dataTypes.js +8 -0
  21. package/server/plugins/file/providers/utils/handlers/index.js +53 -0
  22. package/server/plugins/file/providers/utils/handlers/sizeHandlers.js +11 -0
  23. package/server/plugins/file/providers/utils/streamToBuffer.js +8 -0
  24. package/server/plugins/file/providers/utils/typeguards/isArray.js +3 -0
  25. package/server/plugins/file/providers/utils/typeguards/isBuffer.js +3 -0
  26. package/server/plugins/file/providers/utils/typeguards/isPath.js +5 -0
  27. package/server/plugins/file/providers/utils/typeguards/isReadableStream.js +8 -0
  28. package/server/plugins/file/providers/utils/typeguards/isText.js +3 -0
  29. package/server/plugins/file/uploadFile.js +19 -0
  30. package/server/plugins/file/uploadMultiPart.js +63 -0
  31. package/server/plugins/file/utils/allowedExtensions.js +25 -0
  32. package/server/plugins/file/utils/getFileType.js +10 -0
  33. package/server/plugins/file/utils/getPath.js +25 -0
  34. package/server/plugins/file/utils/isFileExists.js +15 -0
  35. package/server/plugins/file/utils/uploadFileDisk.js +87 -0
  36. package/server/plugins/grpc/file2json.js +54 -0
  37. package/server/plugins/grpc/grpc.js +295 -0
  38. package/server/plugins/grpc/office2pdf.js +68 -0
  39. package/server/plugins/grpc/utils/convertp.proto +137 -0
  40. package/server/plugins/grpc/utils/csv2xls.js +8 -0
  41. package/server/plugins/grpc/utils/excel2Json.js +8 -0
  42. package/server/plugins/grpc/utils/html2doc.js +19 -0
  43. package/server/plugins/grpc/utils/html2img.js +18 -0
  44. package/server/plugins/grpc/utils/html2pdf.js +23 -0
  45. package/server/plugins/grpc/utils/htmlTemplate.js +14 -0
  46. package/server/plugins/grpc/utils/json2xls.js +13 -0
  47. package/server/plugins/grpc/utils/mergePdf.js +20 -0
  48. package/server/plugins/grpc/utils/office2pdf.proto +14 -0
  49. package/server/routes/file/controllers/delete.js +108 -0
  50. package/server/routes/file/controllers/download.js +66 -0
  51. package/server/routes/file/controllers/export.js +283 -0
  52. package/server/routes/file/controllers/files.js +72 -0
  53. package/server/routes/file/controllers/resize.js +90 -0
  54. package/server/routes/file/controllers/resizeAll.js +164 -0
  55. package/server/routes/file/controllers/upload.js +53 -0
  56. package/server/routes/file/controllers/uploadImage.js +47 -0
  57. package/server/routes/file/controllers/utils/convertJSONToCSV.js +36 -0
  58. package/server/routes/file/controllers/utils/convertJSONToXls.js +44 -0
  59. package/server/routes/file/controllers/utils/formatResult.js +17 -0
  60. package/server/routes/file/index.mjs +26 -0
  61. package/server/routes/file/schema.js +16 -0
  62. package/server/routes/grpc/controllers/file2geojson.js +60 -0
  63. package/server/routes/grpc/controllers/filePreview.js +89 -0
  64. package/server/routes/grpc/index.mjs +12 -0
  65. package/server/routes/table/controllers/filter.js +19 -18
  66. package/server/routes/table/controllers/tableData.js +2 -2
  67. package/server/routes/table/functions/getData.js +26 -11
  68. package/server/routes/table/index.js +2 -3
  69. package/server/routes/table/schema.js +30 -39
  70. package/utils.js +30 -2
  71. package/server/routes/table/controllers/tableFilter.js +0 -16
package/config.js CHANGED
@@ -10,6 +10,7 @@ const config = fileName ? JSON.parse(readFileSync(fileName)) : {};
10
10
  // npm run dev === cross-env NODE_ENV=development
11
11
  // alt: node --env=development
12
12
  Object.assign(config, {
13
+ storageList: {},
13
14
  allTemplates: config?.allTemplates || {},
14
15
  skipCheckPolicyRoutes: [],
15
16
  env: process.env?.NODE_ENV || process.argv[2]?.split?.('=')?.pop?.(),
package/index.js CHANGED
@@ -112,6 +112,11 @@ async function plugin(fastify, opt) {
112
112
  menuRoutes(fastify, opt);
113
113
  templatesRoutes(fastify, opt);
114
114
 
115
+ // from fastify-file
116
+ fastify.register(import('./server/routes/file/index.mjs'), opt);
117
+ fastify.register(import('./server/routes/grpc/index.mjs'), opt);
118
+ await fastify.register(import('@fastify/multipart')); // file upload
119
+
115
120
  fastify.get('/api/test-proxy', {}, (req) => ({ ...req.headers || {}, sessionId: req.session?.sessionId }));
116
121
  fastify.get('/api/config', { config: { policy: ['admin', 'site'] } }, () => config);
117
122
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.4.8",
3
+ "version": "1.4.10",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -26,14 +26,20 @@
26
26
  "test:routes": "node --test .\\test\\routes",
27
27
  "test:functions": "node --test .\\test\\functions",
28
28
  "compress": "node compress.js",
29
- "dev": "cross-env NODE_ENV=development node server.js"
29
+ "dev": "set NODE_ENV=development&& node server.js"
30
30
  },
31
31
  "dependencies": {
32
+ "@aws-sdk/client-s3": "3.554.0",
32
33
  "@fastify/http-proxy": "11.1.2",
34
+ "@fastify/multipart": "9.0.3",
35
+ "@grpc/grpc-js": "1.10.6",
36
+ "@grpc/proto-loader": "0.7.12",
33
37
  "dotenv": "16.5.0",
34
38
  "fastify": "5.3.3",
35
39
  "fastify-plugin": "5.0.1",
40
+ "formidable": "3.5.1",
36
41
  "handlebars": "4.7.8",
42
+ "image-size": "1.2.0",
37
43
  "ioredis": "5.3.2",
38
44
  "js-yaml": "4.1.0",
39
45
  "markdown-it": "14.1.0",
@@ -45,7 +51,6 @@
45
51
  "uglify-js": "3.19.3"
46
52
  },
47
53
  "devDependencies": {
48
- "cross-env": "7.0.3",
49
54
  "eslint": "8.49.0",
50
55
  "eslint-config-airbnb": "19.0.4"
51
56
  },
@@ -0,0 +1,18 @@
1
+ import path from 'path';
2
+ import providers from './providers/index.js';
3
+
4
+ async function downloadFile(filePath, options = {}) {
5
+ if (!filePath) return null;
6
+ const filename = path.basename(filePath);
7
+
8
+ // prefix
9
+ const prefix = (options.prefix === 'date' ? new Date().toISOString().split('T')[0] : null)
10
+ || (options.prefix === '3s' ? filename.substring(0, 3) : '');
11
+
12
+ const relativePath = path.join(path.dirname(filePath), prefix, filename);
13
+
14
+ const fp = providers(options);
15
+ return fp.downloadFile(relativePath, options);
16
+ }
17
+
18
+ export default downloadFile;
@@ -0,0 +1,38 @@
1
+ import routeExport from '../../routes/file/controllers/export.js';
2
+
3
+ export default async function getExport({
4
+ pg,
5
+ user,
6
+ host,
7
+ unittest,
8
+ columns,
9
+ cls,
10
+ id,
11
+ cols,
12
+ search,
13
+ format,
14
+ table,
15
+ tableSql,
16
+ sourceName,
17
+ filter,
18
+ nocache,
19
+ formatAnswer,
20
+ sql,
21
+ }, reply) {
22
+ const query = {
23
+ id,
24
+ cols,
25
+ search,
26
+ format,
27
+ table,
28
+ filter,
29
+ nocache,
30
+ formatAnswer,
31
+ sql,
32
+ };
33
+ const result = await routeExport({
34
+ pg, user, host, unittest, columns, cls, query, tableSql, sourceName,
35
+ }, reply);
36
+
37
+ return result;
38
+ }
@@ -0,0 +1,17 @@
1
+ import path from 'path';
2
+ import providers from './providers/index.js';
3
+
4
+ async function isFileExists(filePath, options = { }) {
5
+ const filename = path.basename(filePath);
6
+
7
+ // prefix
8
+ const prefix = (options.prefix === 'date' ? new Date().toISOString().split('T')[0] : null)
9
+ || (options.prefix === '3s' ? filename.substring(0, 3) : '');
10
+
11
+ const relativePath = path.join(path.dirname(filePath), prefix, filename);
12
+
13
+ const fp = providers(options);
14
+ return fp.fileExists(relativePath, options);
15
+ }
16
+
17
+ export default isFileExists;
@@ -0,0 +1,100 @@
1
+ import fs from 'node:fs';
2
+
3
+ import fsp from 'node:fs/promises';
4
+
5
+ import path from 'node:path';
6
+ import isFileExists from '../utils/isFileExists.js';
7
+
8
+ import isBuffer from './utils/typeguards/isBuffer.js';
9
+ import isReadableStream from './utils/typeguards/isReadableStream.js';
10
+
11
+ import getValidData from './utils/getValidData.js';
12
+ import dataTypes from './utils/handlers/dataTypes.js';
13
+
14
+ import getPath from '../utils/getPath.js';
15
+
16
+ const deleteFile = () => async (fp) => {
17
+ const filepath = getPath(fp);
18
+ if (!await isFileExists(filepath)) {
19
+ throw new Error('Файл не знайдено');
20
+ }
21
+ await fsp.rm(filepath);
22
+ };
23
+
24
+ const moveFile = () => async (fp, destFilepath) => {
25
+ const filepath = getPath(fp);
26
+ if (!await isFileExists(filepath)) {
27
+ throw new Error('Файл не знайдено');
28
+ }
29
+ await fsp.mkdir(path.dirname(filepath), { recursive: true });
30
+ await fsp.mkdir(path.dirname(destFilepath), { recursive: true });
31
+
32
+ await fsp.copyFile(filepath, destFilepath, fs.constants.COPYFILE_FICLONE);
33
+ await fsp.deleteFile(filepath);
34
+ };
35
+
36
+ const downloadFile = () => async (fp, options = {}) => {
37
+ const filepath = getPath(fp, options);
38
+ if (options.debug) {
39
+ return { original: fp, full: filepath };
40
+ }
41
+ if (!await isFileExists(filepath, options)) {
42
+ return null;
43
+ }
44
+ if (options.buffer) {
45
+ // const test = await readFile(filepath);
46
+ return fsp.readFile(filepath);
47
+ }
48
+ const fileStream = fs.createReadStream(filepath);
49
+ return fileStream;
50
+ };
51
+
52
+ const fileExists = () => async (filepath, opt = {}) => isFileExists(getPath(filepath, opt), opt);
53
+
54
+ const uploadFile = () => async (fp, data, opt = {}) => {
55
+ const filepath = getPath(fp, opt);
56
+ const validData = await getValidData({ data, types: [dataTypes.buffer, dataTypes.path, dataTypes.stream] });
57
+ // const validData = data;
58
+ await fsp.mkdir(path.dirname(filepath), { recursive: true });
59
+ try {
60
+ const exists = await isFileExists(validData);
61
+
62
+ if (!exists) {
63
+ await fsp.writeFile(filepath, validData, opt);
64
+ }
65
+ else if (isBuffer(validData) || isReadableStream(validData)) {
66
+ await fsp.writeFile(filepath, validData, opt);
67
+ }
68
+ else {
69
+ await fsp.copyFile(validData, filepath);
70
+ }
71
+ return filepath;
72
+ }
73
+ catch (err) {
74
+ // console.log(err.toString());
75
+
76
+ throw new Error(`Can't upload a file ${err.toString()}`);
77
+ }
78
+ };
79
+
80
+ const stat = () => async (filepath) => fsp.stat(getPath(filepath));
81
+
82
+ const getFileSize = () => async (filepath) => (await fsp.stat(getPath(filepath))).size;
83
+
84
+ const getMDate = () => async (filepath) => new Date((await fsp.stat(getPath(filepath))).mtime);
85
+
86
+ const readdir = () => async (filepath) => fsp.readdir(getPath(getPath(filepath)));
87
+
88
+ export default function fsStorage(opt) {
89
+ return {
90
+ deleteFile: deleteFile(opt),
91
+ downloadFile: downloadFile(opt),
92
+ uploadFile: uploadFile(opt),
93
+ fileExists: fileExists(opt),
94
+ stat: stat(opt),
95
+ moveFile: moveFile(opt),
96
+ getFileSize: getFileSize(opt),
97
+ getMDate: getMDate(opt),
98
+ readdir: readdir(opt),
99
+ };
100
+ }
@@ -0,0 +1,49 @@
1
+ import { Readable } from 'stream';
2
+
3
+ interface IListElement {
4
+ login?: string;
5
+ password?: string;
6
+ endpoint?: string;
7
+ containerName?: string;
8
+ accessKeyId?: string;
9
+ secretAccessKey?: string;
10
+ }
11
+
12
+ interface IList {
13
+ [key: string]: IListElement;
14
+ }
15
+
16
+ interface IArgs {
17
+ name: string;
18
+ list: IList;
19
+ }
20
+
21
+ interface IUploadArgs {
22
+ filepath: string;
23
+ data: string | Buffer | ReadableStream;
24
+ }
25
+
26
+ interface IInputArgs {
27
+ filepath: string;
28
+ }
29
+
30
+ interface IObjects {
31
+ Key: string
32
+ }
33
+
34
+ interface IDownloadOptions {
35
+ buffer: boolean
36
+ }
37
+
38
+ interface Ifuncs {
39
+ deleteFile: (filepath: string) => Promise<any>;
40
+ downloadFile: (filepath: string, options: IDownloadOptions) => Promise<Readable | Buffer>;
41
+ uploadFile: (opt: IUploadArgs) => Promise<any>;
42
+ moveFile: (filepath: string, destFilepath: string) => Promise<any>;
43
+ fileExists: (filepath: string) => Promise<boolean>;
44
+ readdir: (filepath: string) => Promise<[IObjects]>
45
+ getFileSize: (filepath) => Promise<number>;
46
+ getMDate: (filepath) => Promise<Date>;
47
+ }
48
+
49
+ export = function (opt?: IArgs): Ifuncs { }
@@ -0,0 +1,36 @@
1
+ import s3 from './s3/index.js';
2
+ import fs from './fs.js';
3
+
4
+ const providers = (opt) => ({
5
+ fs: fs(opt),
6
+ s3: s3(opt),
7
+ });
8
+
9
+ import config from '../../../../config.js';
10
+
11
+ // storage s3 or file
12
+ function dataStorage(opt) {
13
+ const { storageList } = config;
14
+ const providerName = opt?.provider || (config.s3 ? 's3' : 'fs');
15
+
16
+ if (storageList[providerName]) {
17
+ return storageList[providerName];
18
+ }
19
+
20
+ if (storageList[providerName] && !['fs', 's3'].includes(providerName)) {
21
+ return storageList[providerName];
22
+ }
23
+
24
+ const confS3 = {
25
+ containerName: 'work',
26
+ accessKeyId: config.s3?.user,
27
+ secretAccessKey: config.s3?.password,
28
+ ...config.s3 || {},
29
+ };
30
+ const provider = providers(confS3)[providerName];
31
+
32
+ storageList[providerName] = provider;
33
+ return provider;
34
+ }
35
+
36
+ export default dataStorage;
@@ -0,0 +1,12 @@
1
+ import path from 'node:path';
2
+
3
+ import types from './mimes.js';
4
+
5
+ function getMimeType(filename) {
6
+ const ext = path.extname(filename)
7
+ .toLocaleLowerCase()
8
+ .slice(1);
9
+ return types[ext] || null;
10
+ }
11
+
12
+ export default getMimeType;