@opengis/cms 0.0.26 → 0.0.28

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 (90) hide show
  1. package/README.md +98 -98
  2. package/dist/index-W-qQIppj-DunG40EG.js +2452 -0
  3. package/dist/index.html +28 -28
  4. package/dist/index.js +12683 -6702
  5. package/dist/index.umd.cjs +87 -41
  6. package/dist/style.css +1 -0
  7. package/package.json +68 -68
  8. package/plugin.js +76 -76
  9. package/server/app.js +35 -35
  10. package/server/config.js +4 -4
  11. package/server/functions/getDraftKey.js +22 -22
  12. package/server/index.js +22 -22
  13. package/server/migrations/fixes.sql +123 -123
  14. package/server/migrations/site.sql +545 -545
  15. package/server/plugins/adminHook.js +78 -78
  16. package/server/plugins/hook.js +59 -59
  17. package/server/plugins/vite.js +84 -84
  18. package/server/routes/category/controllers/cms.category.delete.js +21 -21
  19. package/server/routes/category/controllers/cms.category.get.js +17 -17
  20. package/server/routes/category/controllers/cms.category.list.js +16 -16
  21. package/server/routes/category/controllers/cms.category.post.js +21 -21
  22. package/server/routes/category/controllers/cms.category.put.js +23 -23
  23. package/server/routes/category/index.mjs +22 -22
  24. package/server/routes/cms/controllers/cmsStat.js +55 -55
  25. package/server/routes/cms/controllers/cmsSuggest.js +57 -57
  26. package/server/routes/cms/controllers/deleteContent.js +114 -114
  27. package/server/routes/cms/controllers/deleteMedia.js +75 -75
  28. package/server/routes/cms/controllers/downloadMedia.js +48 -48
  29. package/server/routes/cms/controllers/getContent.js +110 -110
  30. package/server/routes/cms/controllers/getContentBySlug.js +95 -95
  31. package/server/routes/cms/controllers/getPermissions.js +15 -15
  32. package/server/routes/cms/controllers/insertContent.js +218 -218
  33. package/server/routes/cms/controllers/listMedia.js +93 -93
  34. package/server/routes/cms/controllers/metadataMedia.js +38 -38
  35. package/server/routes/cms/controllers/properties.get.js +53 -53
  36. package/server/routes/cms/controllers/properties.post.js +99 -99
  37. package/server/routes/cms/controllers/searchContent.js +205 -205
  38. package/server/routes/cms/controllers/setPermissions.js +49 -49
  39. package/server/routes/cms/controllers/translate.js +89 -89
  40. package/server/routes/cms/controllers/updateContent.js +238 -238
  41. package/server/routes/cms/controllers/uploadMedia.js +78 -78
  42. package/server/routes/cms/index.mjs +114 -114
  43. package/server/routes/cms/utils/additionalData.js +35 -35
  44. package/server/routes/cms/utils/getCollection.js +81 -81
  45. package/server/routes/cms/utils/getSingle.js +187 -187
  46. package/server/routes/cms/utils/insertContentLocalization.js +86 -86
  47. package/server/routes/cms/utils/requestTranslation.js +85 -85
  48. package/server/routes/cms/utils/updateLocalization.js +47 -47
  49. package/server/routes/cmsSpace/controllers/deleteSpace.js +25 -25
  50. package/server/routes/cmsSpace/controllers/getSpaces.js +27 -27
  51. package/server/routes/cmsSpace/controllers/insertSpace.js +21 -21
  52. package/server/routes/cmsSpace/controllers/updateSpace.js +23 -23
  53. package/server/routes/cmsSpace/index.mjs +20 -20
  54. package/server/routes/contentType/controllers/addContentType.js +162 -162
  55. package/server/routes/contentType/controllers/contentTypeList.js +54 -54
  56. package/server/routes/contentType/controllers/delContentType.js +75 -75
  57. package/server/routes/contentType/controllers/editContentType.js +61 -61
  58. package/server/routes/contentType/controllers/getContentType.js +37 -37
  59. package/server/routes/contentType/index.mjs +35 -35
  60. package/server/routes/contentType/utils/updateContents.js +28 -28
  61. package/server/routes/contentType/utils/updateCustomContentTable.js +55 -55
  62. package/server/routes/feedback/controllers/email.list.js +24 -24
  63. package/server/routes/feedback/controllers/feedback.js +48 -48
  64. package/server/routes/feedback/controllers/feedback.list.js +37 -37
  65. package/server/routes/feedback/controllers/news.subscriptions.js +44 -44
  66. package/server/routes/feedback/index.mjs +71 -71
  67. package/server/routes/logs/controllers/export.user.logs.js +77 -77
  68. package/server/routes/logs/controllers/user.logs.js +44 -44
  69. package/server/routes/logs/index.mjs +9 -9
  70. package/server/routes/menu/controllers/addMenu.js +37 -37
  71. package/server/routes/menu/controllers/delMenu.js +31 -31
  72. package/server/routes/menu/controllers/editMenu.js +41 -41
  73. package/server/routes/menu/controllers/getMenu.js +42 -42
  74. package/server/routes/menu/index.mjs +13 -13
  75. package/server/routes/migration/controllers/collectionToCustom.js +137 -137
  76. package/server/routes/migration/index.mjs +8 -8
  77. package/server/routes/root.mjs +8 -8
  78. package/server/routes/tags/controllers/add.tags.js +24 -24
  79. package/server/routes/tags/controllers/del.tags.js +19 -19
  80. package/server/routes/tags/controllers/edit.tags.js +25 -25
  81. package/server/routes/tags/controllers/get.tags.js +15 -15
  82. package/server/routes/tags/index.mjs +14 -14
  83. package/server/templates/cls/cms.category_type.json +9 -9
  84. package/server/templates/cls/cms.content_review_status.json +9 -9
  85. package/server/templates/cls/cms.content_status.json +9 -9
  86. package/server/templates/cls/cms.content_type.json +9 -9
  87. package/server/templates/cls/cms.lang.json +9 -9
  88. package/server/templates/page/login.html +126 -126
  89. package/server/templates/select/core.user_mentioned.sql +1 -1
  90. package/dist/index-W-qQIppj-DRzFSjU1.js +0 -2452
@@ -1,22 +1,22 @@
1
- import cmsCategoryGet from './controllers/cms.category.get.js';
2
- import cmsCategoryList from './controllers/cms.category.list.js';
3
- import cmsCategoryPut from './controllers/cms.category.put.js';
4
- import cmsCategoryPost from './controllers/cms.category.post.js';
5
- import cmsCategoryDelete from './controllers/cms.category.delete.js'
6
-
7
- export default async function route(app) {
8
- // category add
9
- app.post('/cms-category', {}, cmsCategoryPost);
10
-
11
- // category edit
12
- app.put('/cms-category/:id', {}, cmsCategoryPut);
13
-
14
- // category list
15
- app.get('/cms-category', { config: { policy: ['site'] }}, cmsCategoryList);
16
-
17
- // category get
18
- app.get('/cms-category/:id', {}, cmsCategoryGet);
19
-
20
- // category delete
21
- app.delete('/cms-category/:id', { config: { policy: ['site'] }}, cmsCategoryDelete);
22
- }
1
+ import cmsCategoryGet from './controllers/cms.category.get.js';
2
+ import cmsCategoryList from './controllers/cms.category.list.js';
3
+ import cmsCategoryPut from './controllers/cms.category.put.js';
4
+ import cmsCategoryPost from './controllers/cms.category.post.js';
5
+ import cmsCategoryDelete from './controllers/cms.category.delete.js'
6
+
7
+ export default async function route(app) {
8
+ // category add
9
+ app.post('/cms-category', {}, cmsCategoryPost);
10
+
11
+ // category edit
12
+ app.put('/cms-category/:id', {}, cmsCategoryPut);
13
+
14
+ // category list
15
+ app.get('/cms-category', { config: { policy: ['site'] }}, cmsCategoryList);
16
+
17
+ // category get
18
+ app.get('/cms-category/:id', {}, cmsCategoryGet);
19
+
20
+ // category delete
21
+ app.delete('/cms-category/:id', { config: { policy: ['site'] }}, cmsCategoryDelete);
22
+ }
@@ -1,56 +1,56 @@
1
- import { metaFormat, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- const q = `select
4
- count(distinct object_id) as cdtotal,
5
- count(distinct object_id) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as cdnewcount from site.content_data`;
6
-
7
- const q1 = `select
8
- count(*) as usercount,
9
- count(*) filter( where date_trunc('month', cdate) = date_trunc('month', CURRENT_DATE) ) as usernewcount from admin.users`;
10
-
11
- const q2 = `select
12
- count(*) as mediacount,
13
- count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as medianewcount from site.media`;
14
-
15
- const q3 = `select type, title, updated_at, content_type_id as object_id, slug as content_id from site.contents a
16
- left join lateral (select type from site.content_types where content_type_id=a.content_type_id limit 1)b on 1=1
17
- where 1=1 order by updated_at desc limit 5`;
18
-
19
- export default async function cmsStat({ pg = pgClients.client }, reply) {
20
- if (!pg) {
21
- return reply.status(500).send('empty pg');
22
- }
23
-
24
- const { cdtotal = 0, cdnewcount = 0 } = await pg.query(q).then(el => el.rows?.[0] || {});
25
-
26
- const customTables = await pg.query(`select array_agg(table_name) from site.content_types where type='collection' and table_name is not null`).then(el => el.rows?.[0]?.array_agg || []);
27
-
28
- const custom = await Promise.all(customTables.filter(table => pg?.pk?.[`data.${table}`]).map(async table => {
29
- // await pg.query(`alter table data.${table} add column if not exists created_at timestamp with time zone not null default now()`);
30
- const { total = 0, newcount = 0 } = await pg.queryCache(`select count(*) as total, count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as newcount from data.${table}`, { table: `data.${table}` }).then(el => el.rows?.[0] || {});
31
- return { table, total, newcount };
32
- }));
33
-
34
- const totalContent = custom.reduce((acc, el) => acc + +el.total, +cdtotal);
35
- const newContent = custom.reduce((acc, el) => acc + +el.newcount, +cdnewcount);
36
-
37
- const { usercount = 0, usernewcount = 0 } = await pg.query(q1).then(el => el.rows?.[0] || {});
38
-
39
- const { mediacount = 0, medianewcount = 0 } = await pg.query(q2).then(el => el.rows?.[0] || {});
40
-
41
- const { rows = [] } = await pg.query(q3);
42
-
43
- await metaFormat({ rows, cls: { updated_by: 'core.user_mentioned' }, sufix: false }, pg);
44
-
45
- return {
46
- stat: {
47
- totalContent,
48
- newContent,
49
- totalUsers: +usercount,
50
- newUsers: +usernewcount,
51
- totalMediaFiles: +mediacount,
52
- newMediaFiles: +medianewcount,
53
- },
54
- recentContent: rows,
55
- };
1
+ import { metaFormat, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ const q = `select
4
+ count(distinct object_id) as cdtotal,
5
+ count(distinct object_id) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as cdnewcount from site.content_data`;
6
+
7
+ const q1 = `select
8
+ count(*) as usercount,
9
+ count(*) filter( where date_trunc('month', cdate) = date_trunc('month', CURRENT_DATE) ) as usernewcount from admin.users`;
10
+
11
+ const q2 = `select
12
+ count(*) as mediacount,
13
+ count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as medianewcount from site.media`;
14
+
15
+ const q3 = `select type, title, updated_at, content_type_id as object_id, slug as content_id from site.contents a
16
+ left join lateral (select type from site.content_types where content_type_id=a.content_type_id limit 1)b on 1=1
17
+ where 1=1 order by updated_at desc limit 5`;
18
+
19
+ export default async function cmsStat({ pg = pgClients.client }, reply) {
20
+ if (!pg) {
21
+ return reply.status(500).send('empty pg');
22
+ }
23
+
24
+ const { cdtotal = 0, cdnewcount = 0 } = await pg.query(q).then(el => el.rows?.[0] || {});
25
+
26
+ const customTables = await pg.query(`select array_agg(table_name) from site.content_types where type='collection' and table_name is not null`).then(el => el.rows?.[0]?.array_agg || []);
27
+
28
+ const custom = await Promise.all(customTables.filter(table => pg?.pk?.[`data.${table}`]).map(async table => {
29
+ // await pg.query(`alter table data.${table} add column if not exists created_at timestamp with time zone not null default now()`);
30
+ const { total = 0, newcount = 0 } = await pg.queryCache(`select count(*) as total, count(*) filter( where date_trunc('month', created_at) = date_trunc('month', CURRENT_DATE) ) as newcount from data.${table}`, { table: `data.${table}` }).then(el => el.rows?.[0] || {});
31
+ return { table, total, newcount };
32
+ }));
33
+
34
+ const totalContent = custom.reduce((acc, el) => acc + +el.total, +cdtotal);
35
+ const newContent = custom.reduce((acc, el) => acc + +el.newcount, +cdnewcount);
36
+
37
+ const { usercount = 0, usernewcount = 0 } = await pg.query(q1).then(el => el.rows?.[0] || {});
38
+
39
+ const { mediacount = 0, medianewcount = 0 } = await pg.query(q2).then(el => el.rows?.[0] || {});
40
+
41
+ const { rows = [] } = await pg.query(q3);
42
+
43
+ await metaFormat({ rows, cls: { updated_by: 'core.user_mentioned' }, sufix: false }, pg);
44
+
45
+ return {
46
+ stat: {
47
+ totalContent,
48
+ newContent,
49
+ totalUsers: +usercount,
50
+ newUsers: +usernewcount,
51
+ totalMediaFiles: +mediacount,
52
+ newMediaFiles: +medianewcount,
53
+ },
54
+ recentContent: rows,
55
+ };
56
56
  }
@@ -1,58 +1,58 @@
1
- import { config, pgClients } from '@opengis/fastify-table/utils.js';
2
-
3
- const maxLimit = 50;
4
-
5
- export default async function suggest({ pg = pgClients.client, params = {}, query = {}, user = {} }, reply) {
6
- const t1 = Date.now();
7
-
8
- const { id } = params;
9
- const { key, val } = query;
10
-
11
- if (!pg?.pk) {
12
- return reply.status(500).send('empty pg');
13
- }
14
-
15
- if (!id) {
16
- return reply.status(400).send('not enough params');
17
- }
18
-
19
- const limit = Math.min(query.limit || 16, maxLimit);
20
- const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
21
-
22
- const { ctid, table } = await pg.query(
23
- 'select content_type_id as ctid, table_name as table, columns from site.content_types where $1 in (content_type_id, name)',
24
- [id],
25
- ).then(el => el.rows?.[0] || {});
26
-
27
- if (!ctid) {
28
- return reply.status(404).send('content type not found');
29
- }
30
-
31
- // site.content_data
32
- if (!table) {
33
- const q = `select content_id as id, title as text from site.contents where content_type_id=$1 and ${key ? `lower(title) ~ lower($2)` : (val ? 'content_id=$2' : '1=1')} limit ${limit}`;
34
-
35
- const { rows = [] } = await pg.query(q, [ctid, (key || val)].filter(Boolean));
36
-
37
- return {
38
- time: Date.now() - t1,
39
- limit,
40
- count: rows.length,
41
- mode: 'array',
42
- sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
43
- data: rows,
44
- };
45
- }
46
-
47
- const q = `select id, title as text from data.${table} where ${key ? `lower(title) ~ lower($1)` : (val ? 'id=$1' : '1=1')} limit ${limit}`;
48
- const { rows = [] } = await pg.query(q, [key || val].filter(Boolean));
49
-
50
- return {
51
- time: Date.now() - t1,
52
- limit,
53
- count: rows.length,
54
- mode: 'array',
55
- sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
56
- data: rows,
57
- };
1
+ import { config, pgClients } from '@opengis/fastify-table/utils.js';
2
+
3
+ const maxLimit = 50;
4
+
5
+ export default async function suggest({ pg = pgClients.client, params = {}, query = {}, user = {} }, reply) {
6
+ const t1 = Date.now();
7
+
8
+ const { id } = params;
9
+ const { key, val } = query;
10
+
11
+ if (!pg?.pk) {
12
+ return reply.status(500).send('empty pg');
13
+ }
14
+
15
+ if (!id) {
16
+ return reply.status(400).send('not enough params');
17
+ }
18
+
19
+ const limit = Math.min(query.limit || 16, maxLimit);
20
+ const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
21
+
22
+ const { ctid, table } = await pg.query(
23
+ 'select content_type_id as ctid, table_name as table, columns from site.content_types where $1 in (content_type_id, name)',
24
+ [id],
25
+ ).then(el => el.rows?.[0] || {});
26
+
27
+ if (!ctid) {
28
+ return reply.status(404).send('content type not found');
29
+ }
30
+
31
+ // site.content_data
32
+ if (!table) {
33
+ const q = `select content_id as id, title as text from site.contents where content_type_id=$1 and ${key ? `lower(title) ~ lower($2)` : (val ? 'content_id=$2' : '1=1')} limit ${limit}`;
34
+
35
+ const { rows = [] } = await pg.query(q, [ctid, (key || val)].filter(Boolean));
36
+
37
+ return {
38
+ time: Date.now() - t1,
39
+ limit,
40
+ count: rows.length,
41
+ mode: 'array',
42
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
43
+ data: rows,
44
+ };
45
+ }
46
+
47
+ const q = `select id, title as text from data.${table} where ${key ? `lower(title) ~ lower($1)` : (val ? 'id=$1' : '1=1')} limit ${limit}`;
48
+ const { rows = [] } = await pg.query(q, [key || val].filter(Boolean));
49
+
50
+ return {
51
+ time: Date.now() - t1,
52
+ limit,
53
+ count: rows.length,
54
+ mode: 'array',
55
+ sql: (config.local || user?.user_type?.includes?.('admin')) ? q : undefined,
56
+ data: rows,
57
+ };
58
58
  }
@@ -1,115 +1,115 @@
1
- import { config, pgClients, dataUpdate, dataDelete } from '@opengis/fastify-table/utils.js';
2
-
3
- export default async function deleteContent(req, reply) {
4
- const {
5
- pg = pgClients.client,
6
- params = {},
7
- user = {},
8
- headers = {},
9
- } = req;
10
-
11
- const { type, id } = params;
12
-
13
- if (!type) {
14
- return reply.status(400).send('not enough params: type');
15
- }
16
-
17
- if (!id) {
18
- return reply.status(400).send('not enough params: id');
19
- }
20
-
21
-
22
- if (id === 'pages') {
23
- return reply.status(403).send('access restricted: pages contents cannot be deleted');
24
- }
25
-
26
- const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
27
- left join pg_namespace b on a.relnamespace=b.oid
28
- where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
29
-
30
- if (!arr.length) {
31
- return reply.status(400).send('empty schema: data');
32
- }
33
-
34
- const table = arr.find(el => el === params.type);
35
-
36
- const { ctid, dbtable } = await pg.query('select content_type_id as ctid, table_name as dbtable from site.content_types where $1 in (content_type_id, name)', [params.type])
37
- .then(el => el.rows?.[0] || {});
38
-
39
- // singletone with extra at site.content_data
40
- if (!table && !dbtable && ctid) {
41
- const { cid, status } = ctid === 'pages' && id
42
- ? await pg.query(
43
- 'select content_id as cid, status from site.contents where content_id=$1 limit 1',
44
- [id]).then(el => el.rows?.[0] || {})
45
- : await pg.query(
46
- 'select content_id as cid, status from site.contents where content_type_id=$1 limit 1',
47
- [ctid],
48
- ).then(el => el.rows?.[0] || {});
49
-
50
- if (!cid) {
51
- return reply.status(404).send('contents not found');
52
- }
53
-
54
- if (cid === 'pages') {
55
- return reply.status(403).send('access restricted: pages contents cannot be deleted');
56
- }
57
-
58
- if (status === 'archived') {
59
- const row = await dataDelete({
60
- pg,
61
- id,
62
- table: 'site.contents',
63
- referer: headers?.referer,
64
- uid: user?.uid || 0,
65
- })
66
- return { id, ...row || {} };
67
- }
68
-
69
- // archived
70
- const row = await dataUpdate({
71
- pg,
72
- id,
73
- table: 'site.contents',
74
- data: {
75
- status: 'archived',
76
- },
77
- referer: headers?.referer,
78
- uid: user?.uid || 0,
79
- })
80
- return { id, ...row || {} };
81
- }
82
-
83
- // custom table
84
- if (!table && !dbtable) {
85
- return reply.status(400).send('invalid params: type');
86
- }
87
-
88
- const status = await pg.query(`select status from data."${(table || dbtable)}" where id =$1`, [id]).then(el => el.rows?.[0]?.status);
89
-
90
- if (status === 'archived') {
91
- const row = await dataDelete({
92
- pg,
93
- id,
94
- table: 'data.' + `"${(table || dbtable)}"`,
95
- referer: headers?.referer,
96
- uid: user?.uid || 0,
97
- })
98
- return { id, ...row || {} };
99
- }
100
-
101
- // archived
102
- const result = await dataUpdate({
103
- pg,
104
- id,
105
- table: 'data.' + `"${(table || dbtable)}"`,
106
- data: {
107
- status: 'archived',
108
- },
109
- referer: headers?.referer,
110
- uid: user?.uid || 0,
111
- });
112
-
113
- const pk = pg.pk?.['data.' + table || dbtable];
114
- return reply.status(200).send({ id: result?.[pk], ...result || {} });
1
+ import { config, pgClients, dataUpdate, dataDelete } from '@opengis/fastify-table/utils.js';
2
+
3
+ export default async function deleteContent(req, reply) {
4
+ const {
5
+ pg = pgClients.client,
6
+ params = {},
7
+ user = {},
8
+ headers = {},
9
+ } = req;
10
+
11
+ const { type, id } = params;
12
+
13
+ if (!type) {
14
+ return reply.status(400).send('not enough params: type');
15
+ }
16
+
17
+ if (!id) {
18
+ return reply.status(400).send('not enough params: id');
19
+ }
20
+
21
+
22
+ if (id === 'pages') {
23
+ return reply.status(403).send('access restricted: pages contents cannot be deleted');
24
+ }
25
+
26
+ const arr = config.pg ? await pg.query(`select array_agg(relname)::text[] from pg_class a
27
+ left join pg_namespace b on a.relnamespace=b.oid
28
+ where a.relam=2 and b.nspname='data'`).then(el => el.rows?.[0]?.array_agg || []) : [];
29
+
30
+ if (!arr.length) {
31
+ return reply.status(400).send('empty schema: data');
32
+ }
33
+
34
+ const table = arr.find(el => el === params.type);
35
+
36
+ const { ctid, dbtable } = await pg.query('select content_type_id as ctid, table_name as dbtable from site.content_types where $1 in (content_type_id, name)', [params.type])
37
+ .then(el => el.rows?.[0] || {});
38
+
39
+ // singletone with extra at site.content_data
40
+ if (!table && !dbtable && ctid) {
41
+ const { cid, status } = ctid === 'pages' && id
42
+ ? await pg.query(
43
+ 'select content_id as cid, status from site.contents where content_id=$1 limit 1',
44
+ [id]).then(el => el.rows?.[0] || {})
45
+ : await pg.query(
46
+ 'select content_id as cid, status from site.contents where content_type_id=$1 limit 1',
47
+ [ctid],
48
+ ).then(el => el.rows?.[0] || {});
49
+
50
+ if (!cid) {
51
+ return reply.status(404).send('contents not found');
52
+ }
53
+
54
+ if (cid === 'pages') {
55
+ return reply.status(403).send('access restricted: pages contents cannot be deleted');
56
+ }
57
+
58
+ if (status === 'archived') {
59
+ const row = await dataDelete({
60
+ pg,
61
+ id,
62
+ table: 'site.contents',
63
+ referer: headers?.referer,
64
+ uid: user?.uid || 0,
65
+ })
66
+ return { id, ...row || {} };
67
+ }
68
+
69
+ // archived
70
+ const row = await dataUpdate({
71
+ pg,
72
+ id,
73
+ table: 'site.contents',
74
+ data: {
75
+ status: 'archived',
76
+ },
77
+ referer: headers?.referer,
78
+ uid: user?.uid || 0,
79
+ })
80
+ return { id, ...row || {} };
81
+ }
82
+
83
+ // custom table
84
+ if (!table && !dbtable) {
85
+ return reply.status(400).send('invalid params: type');
86
+ }
87
+
88
+ const status = await pg.query(`select status from data."${(table || dbtable)}" where id =$1`, [id]).then(el => el.rows?.[0]?.status);
89
+
90
+ if (status === 'archived') {
91
+ const row = await dataDelete({
92
+ pg,
93
+ id,
94
+ table: 'data.' + `"${(table || dbtable)}"`,
95
+ referer: headers?.referer,
96
+ uid: user?.uid || 0,
97
+ })
98
+ return { id, ...row || {} };
99
+ }
100
+
101
+ // archived
102
+ const result = await dataUpdate({
103
+ pg,
104
+ id,
105
+ table: 'data.' + `"${(table || dbtable)}"`,
106
+ data: {
107
+ status: 'archived',
108
+ },
109
+ referer: headers?.referer,
110
+ uid: user?.uid || 0,
111
+ });
112
+
113
+ const pk = pg.pk?.['data.' + table || dbtable];
114
+ return reply.status(200).send({ id: result?.[pk], ...result || {} });
115
115
  }
@@ -1,76 +1,76 @@
1
- import path from 'node:path';
2
- import { existsSync } from 'node:fs';
3
- import { rm, stat, readdir } from 'node:fs/promises';
4
-
5
- import { config, dataDelete, getFolder, pgClients } from "@opengis/fastify-table/utils.js";
6
-
7
- const rootDir = getFolder(config, 'local');
8
- const dir = '/files';
9
-
10
- export default async function deleteMedia({
11
- pg = pgClients.client, params = {}, query = {}, user = {}, method = 'DELETE',
12
- }, reply) {
13
- if (!config.debug && method !== 'DELETE') {
14
- return reply.status(403).send('access restricted');
15
- }
16
-
17
- if (!params.id && query.subdir) {
18
- const dirpath = path.join(rootDir, dir, query.subdir || '');
19
- const exists = existsSync(dirpath);
20
- const stats = await stat(dirpath);
21
-
22
- if (!stats.isDirectory()) {
23
- return reply.status(400).send('not a directory');
24
- }
25
-
26
- if (!exists) {
27
- return reply.status(404).send('subdir not found: ' + query.subdir);
28
- }
29
-
30
- const content = await readdir(dirpath, { recursive: true });
31
-
32
- if (query.subdir.startsWith('uploads') && !query.subdir.split('/')[1]) {
33
- return reply.status(403).send('access restricted: uploads directory');
34
- }
35
-
36
- // only admins are allowed to delete non-empty directories
37
- if (content?.length && !user?.user_type?.includes?.('admin')) {
38
- return reply.status(400).send('directory is not empty');
39
- }
40
-
41
- await rm(dirpath, { recursive: true });
42
- return reply.status(200).send('subdirectory successfully deleted');
43
- }
44
-
45
- if (!params?.id) {
46
- return reply.status(400).send('not enough params: id');
47
- }
48
-
49
- if (!pg.pk?.['site.media']) {
50
- return reply.status(404).send('table not found');
51
- }
52
-
53
- const { url: relpath, id } = await pg.query(
54
- 'select media_id as id, url from site.media where media_id = $1 and url is not null',
55
- [params.id],
56
- ).then(el => el.rows?.[0] || {});
57
-
58
- if (!id) {
59
- return reply.status(404).send('media not found: ' + params.id);
60
- }
61
-
62
- const res = await dataDelete({
63
- pg,
64
- id,
65
- table: 'site.media',
66
- uid: user?.uid || 0,
67
- });
68
-
69
- const filepath = path.join(rootDir, relpath);
70
-
71
- if (existsSync(filepath)) {
72
- await rm(filepath, { recursive: true });
73
- }
74
-
75
- return { id, ...res || {} };
1
+ import path from 'node:path';
2
+ import { existsSync } from 'node:fs';
3
+ import { rm, stat, readdir } from 'node:fs/promises';
4
+
5
+ import { config, dataDelete, getFolder, pgClients } from "@opengis/fastify-table/utils.js";
6
+
7
+ const rootDir = getFolder(config, 'local');
8
+ const dir = '/files';
9
+
10
+ export default async function deleteMedia({
11
+ pg = pgClients.client, params = {}, query = {}, user = {}, method = 'DELETE',
12
+ }, reply) {
13
+ if (!config.debug && method !== 'DELETE') {
14
+ return reply.status(403).send('access restricted');
15
+ }
16
+
17
+ if (!params.id && query.subdir) {
18
+ const dirpath = path.join(rootDir, dir, query.subdir || '');
19
+ const exists = existsSync(dirpath);
20
+ const stats = await stat(dirpath);
21
+
22
+ if (!stats.isDirectory()) {
23
+ return reply.status(400).send('not a directory');
24
+ }
25
+
26
+ if (!exists) {
27
+ return reply.status(404).send('subdir not found: ' + query.subdir);
28
+ }
29
+
30
+ const content = await readdir(dirpath, { recursive: true });
31
+
32
+ if (query.subdir.startsWith('uploads') && !query.subdir.split('/')[1]) {
33
+ return reply.status(403).send('access restricted: uploads directory');
34
+ }
35
+
36
+ // only admins are allowed to delete non-empty directories
37
+ if (content?.length && !user?.user_type?.includes?.('admin')) {
38
+ return reply.status(400).send('directory is not empty');
39
+ }
40
+
41
+ await rm(dirpath, { recursive: true });
42
+ return reply.status(200).send('subdirectory successfully deleted');
43
+ }
44
+
45
+ if (!params?.id) {
46
+ return reply.status(400).send('not enough params: id');
47
+ }
48
+
49
+ if (!pg.pk?.['site.media']) {
50
+ return reply.status(404).send('table not found');
51
+ }
52
+
53
+ const { url: relpath, id } = await pg.query(
54
+ 'select media_id as id, url from site.media where media_id = $1 and url is not null',
55
+ [params.id],
56
+ ).then(el => el.rows?.[0] || {});
57
+
58
+ if (!id) {
59
+ return reply.status(404).send('media not found: ' + params.id);
60
+ }
61
+
62
+ const res = await dataDelete({
63
+ pg,
64
+ id,
65
+ table: 'site.media',
66
+ uid: user?.uid || 0,
67
+ });
68
+
69
+ const filepath = path.join(rootDir, relpath);
70
+
71
+ if (existsSync(filepath)) {
72
+ await rm(filepath, { recursive: true });
73
+ }
74
+
75
+ return { id, ...res || {} };
76
76
  }