pangea-server 3.3.80 → 3.3.82

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.
@@ -56,11 +56,31 @@ function hasInstancesAndTotalCount(obj) {
56
56
  }
57
57
  async function setFilesUrls(data) {
58
58
  const visited = new WeakSet();
59
- const singleTargets = [];
60
- const arrayTargets = [];
61
- function isTargetKey(key) {
62
- const k = key.toLowerCase();
63
- return k.includes('image') || k.includes('video');
59
+ const targets = [];
60
+ function getFieldMode(key) {
61
+ const lowerKey = key.toLowerCase();
62
+ if (lowerKey.endsWith('url') || lowerKey.endsWith('urls'))
63
+ return null;
64
+ if (lowerKey.includes('images') || lowerKey.includes('videos'))
65
+ return 'plural';
66
+ if (lowerKey.includes('image') || lowerKey.includes('video'))
67
+ return 'single';
68
+ return null;
69
+ }
70
+ function normalizePluralValue(value) {
71
+ if (Array.isArray(value))
72
+ return value.filter((item) => typeof item === 'string' && item.length > 0);
73
+ if (typeof value === 'string' && value.length > 0)
74
+ return value
75
+ .split(',')
76
+ .map((item) => item.trim())
77
+ .filter(Boolean);
78
+ return [];
79
+ }
80
+ function normalizeSingleValue(value) {
81
+ if (typeof value === 'string')
82
+ return value.length > 0 ? value : null;
83
+ return null;
64
84
  }
65
85
  function collect(node) {
66
86
  if (!node)
@@ -74,49 +94,35 @@ async function setFilesUrls(data) {
74
94
  if (visited.has(node))
75
95
  return;
76
96
  visited.add(node);
77
- for (const [key, value] of Object.entries(node)) {
78
- if (isTargetKey(key)) {
79
- if (Array.isArray(value) && value.every((v) => typeof v === 'string')) {
80
- arrayTargets.push({ node, key });
81
- }
82
- else if (typeof value === 'string') {
83
- singleTargets.push({ node, key });
84
- }
97
+ Object.entries(node).forEach(([key, value]) => {
98
+ const mode = getFieldMode(key);
99
+ if (mode === 'plural') {
100
+ targets.push({ node, key, mode });
101
+ }
102
+ else if (mode === 'single' && typeof value === 'string') {
103
+ targets.push({ node, key, mode });
85
104
  }
86
105
  collect(value);
87
- }
106
+ });
88
107
  }
89
108
  collect(data);
90
- const filesSet = new Set();
91
- singleTargets.forEach(({ node, key }) => {
92
- const val = node[key];
93
- if (typeof val === 'string' && val.length)
94
- filesSet.add(val);
95
- });
96
- arrayTargets.forEach(({ node, key }) => {
97
- const arr = node[key];
98
- if (!Array.isArray(arr))
99
- return;
100
- arr.forEach((v) => {
101
- if (typeof v === 'string' && v.length)
102
- filesSet.add(v);
103
- });
104
- });
105
- const files = Array.from(filesSet);
106
- if (!files.length) {
107
- arrayTargets.forEach(({ node, key }) => (node[`${key}Urls`] = []));
108
- singleTargets.forEach(({ node, key }) => (node[`${key}Url`] = null));
109
+ if (!targets.length)
109
110
  return;
110
- }
111
- const urls = await Promise.all(files.map((file) => helpers_1.FileStorage.GenerateDownloadUrl(file)));
111
+ const allFiles = Array.from(new Set(targets.flatMap((target) => target.mode === 'plural'
112
+ ? normalizePluralValue(target.node[target.key])
113
+ : normalizeSingleValue(target.node[target.key]) || [])));
112
114
  const map = new Map();
113
- files.forEach((file, i) => map.set(file, urls[i]));
114
- singleTargets.forEach(({ node, key }) => {
115
- const val = node[key];
116
- node[`${key}Url`] = typeof val === 'string' && val.length ? map.get(val) || null : null;
117
- });
118
- arrayTargets.forEach(({ node, key }) => {
119
- const arr = node[key];
120
- node[`${key}Urls`] = Array.isArray(arr) ? arr.map((v) => (v && map.has(v) ? map.get(v) : null)) : [];
115
+ if (allFiles.length) {
116
+ const urls = await Promise.all(allFiles.map((file) => helpers_1.FileStorage.GenerateDownloadUrl(file)));
117
+ allFiles.forEach((file, i) => map.set(file, urls[i]));
118
+ }
119
+ targets.forEach((target) => {
120
+ if (target.mode === 'plural') {
121
+ const files = normalizePluralValue(target.node[target.key]);
122
+ target.node[`${target.key}Urls`] = files.map((file) => map.get(file) || null);
123
+ return;
124
+ }
125
+ const file = normalizeSingleValue(target.node[target.key]);
126
+ target.node[`${target.key}Url`] = file ? map.get(file) || null : null;
121
127
  });
122
128
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.3.80",
4
+ "version": "3.3.82",
5
5
  "files": [
6
6
  "dist"
7
7
  ],