pangea-server 3.3.81 → 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,19 +56,32 @@ 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;
64
69
  }
65
- function normalizeArray(value) {
70
+ function normalizePluralValue(value) {
66
71
  if (Array.isArray(value))
67
- return value;
68
- if (typeof value === 'string' && value.length)
69
- return value.split(',').map((v) => v.trim()).filter(Boolean);
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);
70
78
  return [];
71
79
  }
80
+ function normalizeSingleValue(value) {
81
+ if (typeof value === 'string')
82
+ return value.length > 0 ? value : null;
83
+ return null;
84
+ }
72
85
  function collect(node) {
73
86
  if (!node)
74
87
  return;
@@ -81,62 +94,35 @@ async function setFilesUrls(data) {
81
94
  if (visited.has(node))
82
95
  return;
83
96
  visited.add(node);
84
- for (const [key, value] of Object.entries(node)) {
85
- if (isTargetKey(key)) {
86
- if (Array.isArray(value) || typeof value === 'string') {
87
- const arr = normalizeArray(value);
88
- if (arr.length > 1) {
89
- node[key] = arr;
90
- arrayTargets.push({ node, key });
91
- }
92
- else if (arr.length === 1) {
93
- node[key] = arr[0];
94
- singleTargets.push({ node, key });
95
- }
96
- else if (Array.isArray(value)) {
97
- node[key] = [];
98
- arrayTargets.push({ node, key });
99
- }
100
- else {
101
- node[key] = null;
102
- singleTargets.push({ node, key });
103
- }
104
- }
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 });
105
104
  }
106
105
  collect(value);
107
- }
106
+ });
108
107
  }
109
108
  collect(data);
110
- const filesSet = new Set();
111
- singleTargets.forEach(({ node, key }) => {
112
- const val = node[key];
113
- if (typeof val === 'string' && val.length)
114
- filesSet.add(val);
115
- });
116
- arrayTargets.forEach(({ node, key }) => {
117
- const arr = node[key];
118
- if (!Array.isArray(arr))
119
- return;
120
- arr.forEach((v) => {
121
- if (typeof v === 'string' && v.length)
122
- filesSet.add(v);
123
- });
124
- });
125
- const files = Array.from(filesSet);
126
- if (!files.length) {
127
- arrayTargets.forEach(({ node, key }) => (node[`${key}Urls`] = []));
128
- singleTargets.forEach(({ node, key }) => (node[`${key}Url`] = null));
109
+ if (!targets.length)
129
110
  return;
130
- }
131
- 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]) || [])));
132
114
  const map = new Map();
133
- files.forEach((file, i) => map.set(file, urls[i]));
134
- singleTargets.forEach(({ node, key }) => {
135
- const val = node[key];
136
- node[`${key}Url`] = typeof val === 'string' && val.length ? map.get(val) || null : null;
137
- });
138
- arrayTargets.forEach(({ node, key }) => {
139
- const arr = node[key];
140
- 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;
141
127
  });
142
128
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.3.81",
4
+ "version": "3.3.82",
5
5
  "files": [
6
6
  "dist"
7
7
  ],