@opengis/cms 0.0.44 → 0.0.45
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/README.md +98 -98
- package/dist/{en-OTy187va.js → en-sWmaB9uu.js} +3 -1
- package/dist/index.html +29 -29
- package/dist/index.js +4208 -4125
- package/dist/index.umd.cjs +70 -65
- package/dist/{uk-Dy2psXBp.js → uk-rH4IPxG5.js} +4 -2
- package/input-types.json +9 -9
- package/module/cms/cls/content.status.json +17 -17
- package/module/cms/cls/user_type.json +9 -9
- package/module/cms/form/admin.users.form.json +77 -77
- package/module/cms/select/cms.page_type.sql +1 -1
- package/module/cms/select/news_tag_id.sql +11 -11
- package/module/cms/table/admin.users.table.json +53 -53
- package/module/cms/table/collection.default.table.json +96 -96
- package/module/cms/table/single.default.table.json +116 -116
- package/package.json +3 -3
- package/plugin.js +43 -43
- package/server/app.js +35 -35
- package/server/config.js +4 -4
- package/server/functions/getDraftKey.js +22 -22
- package/server/index.js +22 -22
- package/server/migrations/fixes.sql +124 -124
- package/server/migrations/site.sql +595 -595
- package/server/plugins/adminHook.js +78 -78
- package/server/plugins/hook.js +59 -59
- package/server/plugins/vite.js +75 -75
- package/server/routes/category/controllers/cms.category.delete.js +21 -21
- package/server/routes/category/controllers/cms.category.get.js +17 -17
- package/server/routes/category/controllers/cms.category.list.js +16 -16
- package/server/routes/category/controllers/cms.category.post.js +21 -21
- package/server/routes/category/controllers/cms.category.put.js +23 -23
- package/server/routes/category/index.mjs +22 -22
- package/server/routes/cms/controllers/cmsStat.js +55 -55
- package/server/routes/cms/controllers/cmsSuggest.js +57 -57
- package/server/routes/cms/controllers/deleteContent.js +113 -113
- package/server/routes/cms/controllers/deleteMedia.js +76 -76
- package/server/routes/cms/controllers/downloadMedia.js +84 -49
- package/server/routes/cms/controllers/getContent.js +110 -110
- package/server/routes/cms/controllers/getContentBySlug.js +93 -93
- package/server/routes/cms/controllers/getPermissions.js +15 -15
- package/server/routes/cms/controllers/insertContent.js +217 -217
- package/server/routes/cms/controllers/listMedia.js +108 -94
- package/server/routes/cms/controllers/metadataMedia.js +39 -39
- package/server/routes/cms/controllers/properties.get.js +53 -53
- package/server/routes/cms/controllers/properties.post.js +99 -99
- package/server/routes/cms/controllers/searchContent.js +205 -205
- package/server/routes/cms/controllers/setPermissions.js +49 -49
- package/server/routes/cms/controllers/translate.js +89 -89
- package/server/routes/cms/controllers/updateContent.js +238 -238
- package/server/routes/cms/controllers/uploadMedia.js +79 -79
- package/server/routes/cms/index.mjs +112 -112
- package/server/routes/cms/utils/additionalData.js +35 -35
- package/server/routes/cms/utils/getCollection.js +82 -82
- package/server/routes/cms/utils/getSingle.js +187 -187
- package/server/routes/cms/utils/inputTypes.js +5 -5
- package/server/routes/cms/utils/insertContentLocalization.js +86 -86
- package/server/routes/cms/utils/requestTranslation.js +85 -85
- package/server/routes/cms/utils/updateLocalization.js +47 -47
- package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
- package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
- package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
- package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
- package/server/routes/cmsSpace/index.mjs +20 -20
- package/server/routes/contentType/controllers/addContentType.js +162 -162
- package/server/routes/contentType/controllers/contentTypeList.js +54 -54
- package/server/routes/contentType/controllers/delContentType.js +75 -75
- package/server/routes/contentType/controllers/editContentType.js +61 -61
- package/server/routes/contentType/controllers/getContentType.js +37 -37
- package/server/routes/contentType/index.mjs +35 -35
- package/server/routes/contentType/utils/updateContents.js +28 -28
- package/server/routes/contentType/utils/updateCustomContentTable.js +53 -53
- package/server/routes/feedback/controllers/email.list.js +24 -24
- package/server/routes/feedback/controllers/feedback.js +48 -48
- package/server/routes/feedback/controllers/feedback.list.js +37 -37
- package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
- package/server/routes/feedback/index.mjs +71 -71
- package/server/routes/logs/controllers/export.user.logs.js +77 -77
- package/server/routes/logs/controllers/user.logs.js +44 -44
- package/server/routes/logs/index.mjs +9 -9
- package/server/routes/menu/controllers/addMenu.js +37 -37
- package/server/routes/menu/controllers/delMenu.js +31 -31
- package/server/routes/menu/controllers/editMenu.js +41 -41
- package/server/routes/menu/controllers/getMenu.js +42 -42
- package/server/routes/menu/index.mjs +13 -13
- package/server/routes/migration/controllers/collectionToCustom.js +137 -137
- package/server/routes/migration/index.mjs +8 -8
- package/server/routes/root.mjs +8 -8
- package/server/routes/tags/controllers/add.tags.js +24 -24
- package/server/routes/tags/controllers/del.tags.js +19 -19
- package/server/routes/tags/controllers/edit.tags.js +25 -25
- package/server/routes/tags/controllers/get.tags.js +15 -15
- package/server/routes/tags/index.mjs +14 -14
- package/server/templates/cls/cms.category_type.json +9 -9
- package/server/templates/cls/cms.content_review_status.json +9 -9
- package/server/templates/cls/cms.content_status.json +9 -9
- package/server/templates/cls/cms.content_type.json +9 -9
- package/server/templates/cls/cms.lang.json +9 -9
- package/server/templates/page/login.html +126 -126
- package/server/templates/select/core.user_mentioned.sql +1 -1
|
@@ -1,218 +1,218 @@
|
|
|
1
|
-
import { config, checkSQL, getTemplate, pgClients, dataInsert, logger } from '@opengis/fastify-table/utils.js';
|
|
2
|
-
|
|
3
|
-
import inputTypes from '../utils/inputTypes.js';
|
|
4
|
-
|
|
5
|
-
import updateLocalization from '../utils/updateLocalization.js';
|
|
6
|
-
|
|
7
|
-
const defaultColumns = [
|
|
8
|
-
'content_id',
|
|
9
|
-
'space_id',
|
|
10
|
-
'content_type_id',
|
|
11
|
-
'created_at',
|
|
12
|
-
'updated_at',
|
|
13
|
-
'published_at',
|
|
14
|
-
'revision',
|
|
15
|
-
'locale',
|
|
16
|
-
'status',
|
|
17
|
-
'slug',
|
|
18
|
-
'title',
|
|
19
|
-
'created_by',
|
|
20
|
-
'published_by',
|
|
21
|
-
'updated_by',
|
|
22
|
-
'meta',
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
export default async function insertContent(req, reply) {
|
|
26
|
-
const {
|
|
27
|
-
pg = pgClients.client,
|
|
28
|
-
params = {},
|
|
29
|
-
user = {},
|
|
30
|
-
body = {},
|
|
31
|
-
headers = {},
|
|
32
|
-
} = req;
|
|
33
|
-
|
|
34
|
-
const { type, id = body?.id } = params;
|
|
35
|
-
|
|
36
|
-
if (!type) {
|
|
37
|
-
return reply.status(400).send('not enough params: type');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
|
|
41
|
-
left join pg_namespace b on a.relnamespace=b.oid
|
|
42
|
-
where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
|
|
43
|
-
|
|
44
|
-
const { content_id: contentId, type: contentType } = await pg.query(
|
|
45
|
-
'select content_id, (select type from site.content_types where content_type_id=t.content_type_id) from site.contents t where slug=$1',
|
|
46
|
-
[['single', 'pages'].includes(type) ? id : type],
|
|
47
|
-
).then(el => el.rows?.[0] || {});
|
|
48
|
-
|
|
49
|
-
const ctypeId = contentId && contentId !== 'pages' && contentType === 'single' ? await pg.query(
|
|
50
|
-
'select content_type_id from site.contents where content_id=$1',
|
|
51
|
-
[contentId],
|
|
52
|
-
).then(el => el.rows?.[0]?.content_type_id) : null;
|
|
53
|
-
|
|
54
|
-
if (!arr.length && (ctypeId || type) !== 'pages') {
|
|
55
|
-
return reply.status(400).send('empty schema: data');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const table = arr.find(el => el === params.type);
|
|
59
|
-
|
|
60
|
-
const { ctid, dbtable, columns: contentColumns } = await pg.query(
|
|
61
|
-
'select content_type_id as ctid, table_name as dbtable, columns from site.content_types where $1 in (content_type_id, name)',
|
|
62
|
-
[type === 'single' && id ? id : (ctypeId || type)],
|
|
63
|
-
).then(el => el.rows?.[0] || {});
|
|
64
|
-
|
|
65
|
-
const loadTable = (ctypeId || type) === 'pages' ? await getTemplate('table', 'single.default.table') : {};
|
|
66
|
-
|
|
67
|
-
const columns = (ctypeId || type) === 'pages'
|
|
68
|
-
? (loadTable?.columns || []).concat(contentColumns.filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
|
|
69
|
-
: contentColumns;
|
|
70
|
-
|
|
71
|
-
// site.content_data, includes singletone
|
|
72
|
-
if (((!table && !dbtable) || (ctypeId || type) === 'pages')) {
|
|
73
|
-
const cid = await pg.query(
|
|
74
|
-
'select content_id from site.contents where content_type_id=$1 limit 1',
|
|
75
|
-
[ctid || 'pages'],
|
|
76
|
-
).then(el => el.rows?.[0]?.content_id);
|
|
77
|
-
|
|
78
|
-
const ctid1 = body.content_type_id || ctid || 'pages';
|
|
79
|
-
|
|
80
|
-
if (!cid) {
|
|
81
|
-
return reply.status(404).send('contents not found');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const columnList = columns?.map?.(el => el.name) || [];
|
|
85
|
-
const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
|
|
86
|
-
const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
|
|
87
|
-
|
|
88
|
-
if (!Object.keys(body || {}).length) {
|
|
89
|
-
return reply.status(400).send('invalid payload');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// const xss = checkXSS({ body });
|
|
93
|
-
const sql = checkSQL({ body });
|
|
94
|
-
|
|
95
|
-
if (/*xss.error ||*/ sql.error && false) {
|
|
96
|
-
logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
|
|
97
|
-
table,
|
|
98
|
-
...params,
|
|
99
|
-
uid: user?.uid,
|
|
100
|
-
...(/*xss.error ? xss : */sql),
|
|
101
|
-
});
|
|
102
|
-
return reply
|
|
103
|
-
.status(409)
|
|
104
|
-
.send(
|
|
105
|
-
`Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const client = await pg.connect();
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
await client.query('begin');
|
|
113
|
-
const res = await dataInsert({
|
|
114
|
-
pg: client,
|
|
115
|
-
table: 'site.contents',
|
|
116
|
-
id,
|
|
117
|
-
data: { ...body, content_type_id: ctid1 },
|
|
118
|
-
uid: user?.uid,
|
|
119
|
-
}).then(el => el.rows?.[0] || {});
|
|
120
|
-
|
|
121
|
-
if (!res?.content_id) throw new Error('insert contents error');
|
|
122
|
-
|
|
123
|
-
await Promise.all(keys.map(async key => dataInsert({
|
|
124
|
-
pg: client,
|
|
125
|
-
table: 'site.content_data',
|
|
126
|
-
data: {
|
|
127
|
-
field_key: key,
|
|
128
|
-
content_id: res.content_id,
|
|
129
|
-
object_id: res.content_id,
|
|
130
|
-
field_type: types[key] || 'text',
|
|
131
|
-
field_value: inputTypes[types[key] || ''] === 'json' ? undefined : body[key],
|
|
132
|
-
field_value_object: inputTypes[types[key] || ''] === 'json' ? body[key] : undefined,
|
|
133
|
-
},
|
|
134
|
-
uid: user?.uid,
|
|
135
|
-
})));
|
|
136
|
-
|
|
137
|
-
await updateLocalization(client, res.content_id, body, ctid1, user?.uid);
|
|
138
|
-
|
|
139
|
-
if (body?.tag_list?.length) {
|
|
140
|
-
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
141
|
-
pg: client,
|
|
142
|
-
table: 'site.tag_data',
|
|
143
|
-
data: {
|
|
144
|
-
tag_id: tag,
|
|
145
|
-
data_id: id,
|
|
146
|
-
},
|
|
147
|
-
uid: user?.uid,
|
|
148
|
-
})));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
await client.query('commit');
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
id: res.content_id, rows: [res].filter(Boolean)
|
|
155
|
-
};
|
|
156
|
-
} catch (err) {
|
|
157
|
-
await client.query('rollback');
|
|
158
|
-
return reply.status(500).send(err.toString());
|
|
159
|
-
} finally {
|
|
160
|
-
client.release();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// custom table
|
|
165
|
-
if (!table && !dbtable) {
|
|
166
|
-
return reply.status(400).send('invalid params: type');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const client = await pg.connect();
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
await client.query('begin');
|
|
173
|
-
|
|
174
|
-
// const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: inputTypes[curr.type] || 'text' }), {}) || {};
|
|
175
|
-
const { rows = [] } = await dataInsert({
|
|
176
|
-
pg: client,
|
|
177
|
-
id,
|
|
178
|
-
table: 'data.' + `"${(table || dbtable)}"`,
|
|
179
|
-
data: body,
|
|
180
|
-
referer: headers?.referer,
|
|
181
|
-
uid: user?.uid,
|
|
182
|
-
}).catch(err => {
|
|
183
|
-
if (err.message?.includes?.('unique constraint')) {
|
|
184
|
-
throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^']+)/g)?.[1]);
|
|
185
|
-
}
|
|
186
|
-
throw err;
|
|
187
|
-
}) || {};
|
|
188
|
-
|
|
189
|
-
const idRes = rows?.[0]?.id;
|
|
190
|
-
|
|
191
|
-
if (!idRes) {
|
|
192
|
-
throw new Error('content insert error');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
await updateLocalization(client, idRes, body, ctid, user?.uid);
|
|
196
|
-
|
|
197
|
-
if (body?.tag_list?.length) {
|
|
198
|
-
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
199
|
-
pg: client,
|
|
200
|
-
table: 'site.tag_data',
|
|
201
|
-
data: {
|
|
202
|
-
tag_id: tag,
|
|
203
|
-
data_id: id || idRes,
|
|
204
|
-
},
|
|
205
|
-
uid: user?.uid,
|
|
206
|
-
})));
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
await client.query('commit');
|
|
210
|
-
|
|
211
|
-
return reply.status(200).send({ id: idRes, rows });
|
|
212
|
-
} catch (err) {
|
|
213
|
-
await client.query('rollback');
|
|
214
|
-
return reply.status(500).send(err.toString());
|
|
215
|
-
} finally {
|
|
216
|
-
client.release();
|
|
217
|
-
}
|
|
1
|
+
import { config, checkSQL, getTemplate, pgClients, dataInsert, logger } from '@opengis/fastify-table/utils.js';
|
|
2
|
+
|
|
3
|
+
import inputTypes from '../utils/inputTypes.js';
|
|
4
|
+
|
|
5
|
+
import updateLocalization from '../utils/updateLocalization.js';
|
|
6
|
+
|
|
7
|
+
const defaultColumns = [
|
|
8
|
+
'content_id',
|
|
9
|
+
'space_id',
|
|
10
|
+
'content_type_id',
|
|
11
|
+
'created_at',
|
|
12
|
+
'updated_at',
|
|
13
|
+
'published_at',
|
|
14
|
+
'revision',
|
|
15
|
+
'locale',
|
|
16
|
+
'status',
|
|
17
|
+
'slug',
|
|
18
|
+
'title',
|
|
19
|
+
'created_by',
|
|
20
|
+
'published_by',
|
|
21
|
+
'updated_by',
|
|
22
|
+
'meta',
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export default async function insertContent(req, reply) {
|
|
26
|
+
const {
|
|
27
|
+
pg = pgClients.client,
|
|
28
|
+
params = {},
|
|
29
|
+
user = {},
|
|
30
|
+
body = {},
|
|
31
|
+
headers = {},
|
|
32
|
+
} = req;
|
|
33
|
+
|
|
34
|
+
const { type, id = body?.id } = params;
|
|
35
|
+
|
|
36
|
+
if (!type) {
|
|
37
|
+
return reply.status(400).send('not enough params: type');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
|
|
41
|
+
left join pg_namespace b on a.relnamespace=b.oid
|
|
42
|
+
where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
|
|
43
|
+
|
|
44
|
+
const { content_id: contentId, type: contentType } = await pg.query(
|
|
45
|
+
'select content_id, (select type from site.content_types where content_type_id=t.content_type_id) from site.contents t where slug=$1',
|
|
46
|
+
[['single', 'pages'].includes(type) ? id : type],
|
|
47
|
+
).then(el => el.rows?.[0] || {});
|
|
48
|
+
|
|
49
|
+
const ctypeId = contentId && contentId !== 'pages' && contentType === 'single' ? await pg.query(
|
|
50
|
+
'select content_type_id from site.contents where content_id=$1',
|
|
51
|
+
[contentId],
|
|
52
|
+
).then(el => el.rows?.[0]?.content_type_id) : null;
|
|
53
|
+
|
|
54
|
+
if (!arr.length && (ctypeId || type) !== 'pages') {
|
|
55
|
+
return reply.status(400).send('empty schema: data');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const table = arr.find(el => el === params.type);
|
|
59
|
+
|
|
60
|
+
const { ctid, dbtable, columns: contentColumns } = await pg.query(
|
|
61
|
+
'select content_type_id as ctid, table_name as dbtable, columns from site.content_types where $1 in (content_type_id, name)',
|
|
62
|
+
[type === 'single' && id ? id : (ctypeId || type)],
|
|
63
|
+
).then(el => el.rows?.[0] || {});
|
|
64
|
+
|
|
65
|
+
const loadTable = (ctypeId || type) === 'pages' ? await getTemplate('table', 'single.default.table') : {};
|
|
66
|
+
|
|
67
|
+
const columns = (ctypeId || type) === 'pages'
|
|
68
|
+
? (loadTable?.columns || []).concat(contentColumns.filter(col => loadTable?.columns.findIndex(el => el.name === col.name) === -1))
|
|
69
|
+
: contentColumns;
|
|
70
|
+
|
|
71
|
+
// site.content_data, includes singletone
|
|
72
|
+
if (((!table && !dbtable) || (ctypeId || type) === 'pages')) {
|
|
73
|
+
const cid = await pg.query(
|
|
74
|
+
'select content_id from site.contents where content_type_id=$1 limit 1',
|
|
75
|
+
[ctid || 'pages'],
|
|
76
|
+
).then(el => el.rows?.[0]?.content_id);
|
|
77
|
+
|
|
78
|
+
const ctid1 = body.content_type_id || ctid || 'pages';
|
|
79
|
+
|
|
80
|
+
if (!cid) {
|
|
81
|
+
return reply.status(404).send('contents not found');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const columnList = columns?.map?.(el => el.name) || [];
|
|
85
|
+
const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: curr.type || 'text' }), {}) || {};
|
|
86
|
+
const keys = Object.keys(body || {}).filter(key => columnList.includes(key) && !defaultColumns.includes(key));
|
|
87
|
+
|
|
88
|
+
if (!Object.keys(body || {}).length) {
|
|
89
|
+
return reply.status(400).send('invalid payload');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// const xss = checkXSS({ body });
|
|
93
|
+
const sql = checkSQL({ body });
|
|
94
|
+
|
|
95
|
+
if (/*xss.error ||*/ sql.error && false) {
|
|
96
|
+
logger.file(`injection/${/*xss.error ? 'xss' : */'sql'}/cms`, {
|
|
97
|
+
table,
|
|
98
|
+
...params,
|
|
99
|
+
uid: user?.uid,
|
|
100
|
+
...(/*xss.error ? xss : */sql),
|
|
101
|
+
});
|
|
102
|
+
return reply
|
|
103
|
+
.status(409)
|
|
104
|
+
.send(
|
|
105
|
+
`Дані містять заборонені ${/*xss.error ? 'xss' : */'sql'} символи. Приберіть їх та спробуйте ще раз`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const client = await pg.connect();
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
await client.query('begin');
|
|
113
|
+
const res = await dataInsert({
|
|
114
|
+
pg: client,
|
|
115
|
+
table: 'site.contents',
|
|
116
|
+
id,
|
|
117
|
+
data: { ...body, content_type_id: ctid1 },
|
|
118
|
+
uid: user?.uid,
|
|
119
|
+
}).then(el => el.rows?.[0] || {});
|
|
120
|
+
|
|
121
|
+
if (!res?.content_id) throw new Error('insert contents error');
|
|
122
|
+
|
|
123
|
+
await Promise.all(keys.map(async key => dataInsert({
|
|
124
|
+
pg: client,
|
|
125
|
+
table: 'site.content_data',
|
|
126
|
+
data: {
|
|
127
|
+
field_key: key,
|
|
128
|
+
content_id: res.content_id,
|
|
129
|
+
object_id: res.content_id,
|
|
130
|
+
field_type: types[key] || 'text',
|
|
131
|
+
field_value: inputTypes[types[key] || ''] === 'json' ? undefined : body[key],
|
|
132
|
+
field_value_object: inputTypes[types[key] || ''] === 'json' ? body[key] : undefined,
|
|
133
|
+
},
|
|
134
|
+
uid: user?.uid,
|
|
135
|
+
})));
|
|
136
|
+
|
|
137
|
+
await updateLocalization(client, res.content_id, body, ctid1, user?.uid);
|
|
138
|
+
|
|
139
|
+
if (body?.tag_list?.length) {
|
|
140
|
+
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
141
|
+
pg: client,
|
|
142
|
+
table: 'site.tag_data',
|
|
143
|
+
data: {
|
|
144
|
+
tag_id: tag,
|
|
145
|
+
data_id: id,
|
|
146
|
+
},
|
|
147
|
+
uid: user?.uid,
|
|
148
|
+
})));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
await client.query('commit');
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
id: res.content_id, rows: [res].filter(Boolean)
|
|
155
|
+
};
|
|
156
|
+
} catch (err) {
|
|
157
|
+
await client.query('rollback');
|
|
158
|
+
return reply.status(500).send(err.toString());
|
|
159
|
+
} finally {
|
|
160
|
+
client.release();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// custom table
|
|
165
|
+
if (!table && !dbtable) {
|
|
166
|
+
return reply.status(400).send('invalid params: type');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const client = await pg.connect();
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
await client.query('begin');
|
|
173
|
+
|
|
174
|
+
// const types = columns?.reduce?.((acc, curr) => ({ ...acc, [curr.name]: inputTypes[curr.type] || 'text' }), {}) || {};
|
|
175
|
+
const { rows = [] } = await dataInsert({
|
|
176
|
+
pg: client,
|
|
177
|
+
id,
|
|
178
|
+
table: 'data.' + `"${(table || dbtable)}"`,
|
|
179
|
+
data: body,
|
|
180
|
+
referer: headers?.referer,
|
|
181
|
+
uid: user?.uid,
|
|
182
|
+
}).catch(err => {
|
|
183
|
+
if (err.message?.includes?.('unique constraint')) {
|
|
184
|
+
throw new Error('Порушенні унікальності: ' + err.message?.match?.(/([^']+)/g)?.[1]);
|
|
185
|
+
}
|
|
186
|
+
throw err;
|
|
187
|
+
}) || {};
|
|
188
|
+
|
|
189
|
+
const idRes = rows?.[0]?.id;
|
|
190
|
+
|
|
191
|
+
if (!idRes) {
|
|
192
|
+
throw new Error('content insert error');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
await updateLocalization(client, idRes, body, ctid, user?.uid);
|
|
196
|
+
|
|
197
|
+
if (body?.tag_list?.length) {
|
|
198
|
+
await Promise.all(body.tag_list.map(async tag => dataInsert({
|
|
199
|
+
pg: client,
|
|
200
|
+
table: 'site.tag_data',
|
|
201
|
+
data: {
|
|
202
|
+
tag_id: tag,
|
|
203
|
+
data_id: id || idRes,
|
|
204
|
+
},
|
|
205
|
+
uid: user?.uid,
|
|
206
|
+
})));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
await client.query('commit');
|
|
210
|
+
|
|
211
|
+
return reply.status(200).send({ id: idRes, rows });
|
|
212
|
+
} catch (err) {
|
|
213
|
+
await client.query('rollback');
|
|
214
|
+
return reply.status(500).send(err.toString());
|
|
215
|
+
} finally {
|
|
216
|
+
client.release();
|
|
217
|
+
}
|
|
218
218
|
}
|
|
@@ -1,95 +1,109 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { existsSync, mkdirSync } from 'node:fs';
|
|
3
|
-
import { readdir, stat } from 'node:fs/promises';
|
|
4
|
-
|
|
5
|
-
import { config, getFolder, pgClients, getMimeType } from '@opengis/fastify-table/utils.js';
|
|
6
|
-
import { createHash } from 'node:crypto';
|
|
7
|
-
|
|
8
|
-
// path.resolve() converts POSIX paths from getFolder to valid Windows paths (Bun/Node fs require this on Windows)
|
|
9
|
-
const rootDir = path.resolve(getFolder(config, 'local'));
|
|
10
|
-
const dir = '/files';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
4
|
+
|
|
5
|
+
import { config, getFolder, pgClients, getMimeType } from '@opengis/fastify-table/utils.js';
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
|
|
8
|
+
// path.resolve() converts POSIX paths from getFolder to valid Windows paths (Bun/Node fs require this on Windows)
|
|
9
|
+
const rootDir = path.resolve(getFolder(config, 'local'));
|
|
10
|
+
const dir = '/files';
|
|
11
|
+
|
|
12
|
+
const filesizeCache = {};
|
|
13
|
+
const previewWidth = 300;
|
|
14
|
+
|
|
15
|
+
mkdirSync(path.join(rootDir, dir), { recursive: true });
|
|
16
|
+
|
|
17
|
+
export default async function listMedia(req, reply) {
|
|
18
|
+
const { pg = pgClients.client, query = {} } = req;
|
|
19
|
+
const { subdir: subdir1 = '', search } = query;
|
|
20
|
+
|
|
21
|
+
if (!pg.pk?.['site.media']) {
|
|
22
|
+
return reply.status(404).send('table not found');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (typeof subdir1 !== 'string' || subdir1.includes('..')) {
|
|
26
|
+
return reply.status(403).send('invalid params: subdir');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const subdir = subdir1.replace(/\/{2,}/g, '/');
|
|
30
|
+
const relpath = path.join(dir, subdir1).replace(/\\/g, '/');
|
|
31
|
+
const dirpath = path.join(rootDir, relpath).replace(/\\/g, '/');
|
|
32
|
+
|
|
33
|
+
if (!existsSync(dirpath)) {
|
|
34
|
+
return { data: [], relpath, msg: 'directory not exists' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const isDirectory = (await stat(dirpath)).isDirectory();
|
|
38
|
+
|
|
39
|
+
const allItems = isDirectory ? await readdir(dirpath, { withFileTypes: true, recursive: true }) : [];
|
|
40
|
+
const items = isDirectory ? await readdir(dirpath, { withFileTypes: true }) : [];
|
|
41
|
+
|
|
42
|
+
const rows = await pg.query(
|
|
43
|
+
`select
|
|
44
|
+
media_id as id, filename, filetype, filesize, url, description, alt,
|
|
45
|
+
mime, preview_url, created_at, updated_at, created_by, updated_by
|
|
46
|
+
from site.media
|
|
47
|
+
where ${subdir ? 'subdir = $1' : (search ? '1=1' : 'subdir is null')} ${search ? `and filename ilike '%${search.replace(/'/g, "''")}%'` : ''}`,
|
|
48
|
+
[subdir].filter(Boolean),
|
|
49
|
+
).then(el => el.rows || []); // ?.filter(row => items.map(el => el.name).includes(row.filename))
|
|
50
|
+
|
|
51
|
+
const subdirs = items
|
|
52
|
+
.filter(el => el.isDirectory())
|
|
53
|
+
.map(el => ({ type: 'dir', name: el.name }))
|
|
54
|
+
.filter(el => search ? el.name.includes(search) : true);
|
|
55
|
+
|
|
56
|
+
if (subdirs.length) {
|
|
57
|
+
await Promise.all(subdirs.map(async (item) => {
|
|
58
|
+
const items = isDirectory ? await readdir(path.join(dirpath, item.name)) : [];
|
|
59
|
+
Object.assign(item, { count: items.length });
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const result = { relpath };
|
|
64
|
+
|
|
65
|
+
if (config.debug) {
|
|
66
|
+
Object.assign(result, { rootDir });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const filteredFiles = (search ? allItems.filter(el => el.name.includes(search)) : items).filter(el => el.isFile());
|
|
70
|
+
|
|
71
|
+
const files = await Promise.all(filteredFiles.map(async el => {
|
|
72
|
+
const media = rows.find(row => row.filename === el.name);
|
|
73
|
+
const filepath = media ? media.url : ('/files/' + (el.path.split('/files/')[1] ? el.path.split('/files/')[1] + '/' : '') + el.name);
|
|
74
|
+
|
|
75
|
+
if (!filesizeCache[filepath]) {
|
|
76
|
+
const { size } = await stat(path.join(rootDir, filepath));
|
|
77
|
+
Object.assign(filesizeCache, { [filepath]: size });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const mime = getMimeType(el.name) || '';
|
|
81
|
+
const preview = mime.startsWith('image/') /* && mime !== 'image/svg+xml' */
|
|
82
|
+
? `${req.routeOptions.url}/${media ? media.id : Buffer.from(filepath).toString('base64url')}/preview`
|
|
83
|
+
: undefined;
|
|
84
|
+
|
|
85
|
+
return media ? {
|
|
86
|
+
type: 'file',
|
|
87
|
+
...media,
|
|
88
|
+
url: `${req.routeOptions.url}/${media.id}/file`,
|
|
89
|
+
preview,
|
|
90
|
+
filepath,
|
|
91
|
+
metadata: `${req.routeOptions.url}/${media.id}`,
|
|
92
|
+
} : {
|
|
93
|
+
id: createHash('md5').update(filepath).digest('hex'),
|
|
94
|
+
hash: true,
|
|
95
|
+
type: 'file',
|
|
96
|
+
filename: el.name,
|
|
97
|
+
filepath,
|
|
98
|
+
filetype: mime.startsWith('image/') ? "image" : "other",
|
|
99
|
+
filesize: filesizeCache[filepath] || 0,
|
|
100
|
+
url: filepath,
|
|
101
|
+
mime,
|
|
102
|
+
preview,
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
Object.assign(result, { data: subdirs.concat(files) });
|
|
107
|
+
return result;
|
|
108
|
+
|
|
95
109
|
}
|