pangea-server 3.3.76 → 3.3.78

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