cynx-lib 1.3.81 → 1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cynx-lib",
3
- "version": "1.3.81",
3
+ "version": "1.3.82",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -274,7 +274,7 @@ class StorageManager {
274
274
  fileType,
275
275
  mimeType,
276
276
  fileSize: Number(f.file_size || 0),
277
- // cloudUrl: f.cloud_url,
277
+ cloudUrl: f.cloud_url || f.cloudUrl || null,
278
278
  viewUrl: `${base}/cdn/${fileId}/view`,
279
279
  downloadUrl: `${base}/cdn/${fileId}/download`,
280
280
  url: `${base}/cdn/${fileId}`,
@@ -943,6 +943,12 @@ class StorageManager {
943
943
  }
944
944
  return res.status(404).send("File URL not found");
945
945
  }
946
+ if (targetUrl.startsWith("/")) {
947
+ const serverBase = (this.storageServerUrl || process.env?.STORAGE_SERVER_URL || "").replace(/\/+$/, "");
948
+ if (serverBase) {
949
+ targetUrl = `${serverBase}${targetUrl}`;
950
+ }
951
+ }
946
952
  if (!/^https?:\/\//i.test(targetUrl)) {
947
953
  targetUrl = `https://${targetUrl}`;
948
954
  }
@@ -1067,10 +1073,30 @@ class StorageManager {
1067
1073
  const fileType = rf.fileType || rf.file_type || (0, import_storage.getFileTypeCategory)(mimeType, rf.originalName || rf.original_name);
1068
1074
  const originalName = rf.originalName || rf.original_name || "file";
1069
1075
  const fileSize = Number(rf.fileSize || rf.file_size || 0);
1070
- const cloudUrl = rf.cloudUrl || rf.cloud_url || rf.url || (this.storageServerUrl ? `${this.storageServerUrl}/cdn/${fileId}` : "");
1076
+ const rawUrl = rf.cloudUrl || rf.cloud_url || rf.url || "";
1077
+ let cloudUrl = rawUrl;
1078
+ if (this.storageServerUrl) {
1079
+ if (rawUrl.startsWith("http")) {
1080
+ cloudUrl = this.storageServerUrl.startsWith("https://") ? rawUrl.replace(/^http:\/\//, "https://") : rawUrl;
1081
+ } else if (rawUrl.startsWith("/")) {
1082
+ cloudUrl = `${this.storageServerUrl}${rawUrl}`;
1083
+ } else {
1084
+ cloudUrl = `${this.storageServerUrl}/cdn/${fileId}`;
1085
+ }
1086
+ }
1071
1087
  const cloudPublicId = rf.cloudPublicId || rf.cloud_public_id || fileId;
1072
1088
  const storageId = rf.storageId || rf.storage_id || null;
1073
1089
  if (this.query) {
1090
+ let validStorageId = null;
1091
+ if (storageId) {
1092
+ try {
1093
+ const sCheck = await this.query(`SELECT 1 FROM ${this.storagesTable} WHERE id = $1 LIMIT 1`, [storageId]);
1094
+ if (sCheck && sCheck.rows && sCheck.rows.length > 0) {
1095
+ validStorageId = storageId;
1096
+ }
1097
+ } catch {
1098
+ }
1099
+ }
1074
1100
  const hasInspectionCol = await this._hasInspectionColumn();
1075
1101
  const insertSql = hasInspectionCol ? `INSERT INTO ${this.filesTable} (
1076
1102
  id, entity_type, entity_id, inspection_id, storage_id,
@@ -1094,7 +1120,7 @@ class StorageManager {
1094
1120
  cloud_public_id = EXCLUDED.cloud_public_id,
1095
1121
  updated_at = NOW()
1096
1122
  RETURNING *`;
1097
- const params = hasInspectionCol ? [fileId, entityType, entityId, inspectionId, storageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId] : [fileId, entityType, entityId, storageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId];
1123
+ const params = hasInspectionCol ? [fileId, entityType, entityId, inspectionId, validStorageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId] : [fileId, entityType, entityId, validStorageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId];
1098
1124
  const r = await this.query(insertSql, params);
1099
1125
  this.fileMetadataCache.set(fileId, { data: r.rows[0], ts: Date.now() });
1100
1126
  return this.formatFileRecord(r.rows[0]);
@@ -284,7 +284,7 @@ export class StorageManager {
284
284
  fileType,
285
285
  mimeType,
286
286
  fileSize: Number(f.file_size || 0),
287
- // cloudUrl: f.cloud_url,
287
+ cloudUrl: f.cloud_url || f.cloudUrl || null,
288
288
  viewUrl: `${base}/cdn/${fileId}/view`,
289
289
  downloadUrl: `${base}/cdn/${fileId}/download`,
290
290
  url: `${base}/cdn/${fileId}`,
@@ -1044,6 +1044,13 @@ export class StorageManager {
1044
1044
  return res.status(404).send('File URL not found');
1045
1045
  }
1046
1046
 
1047
+ if (targetUrl.startsWith('/')) {
1048
+ const serverBase = (this.storageServerUrl || process.env?.STORAGE_SERVER_URL || '').replace(/\/+$/, '');
1049
+ if (serverBase) {
1050
+ targetUrl = `${serverBase}${targetUrl}`;
1051
+ }
1052
+ }
1053
+
1047
1054
  if (!/^https?:\/\//i.test(targetUrl)) {
1048
1055
  targetUrl = `https://${targetUrl}`;
1049
1056
  }
@@ -1195,11 +1202,31 @@ export class StorageManager {
1195
1202
  const fileType = rf.fileType || rf.file_type || getFileTypeCategory(mimeType, rf.originalName || rf.original_name);
1196
1203
  const originalName = rf.originalName || rf.original_name || 'file';
1197
1204
  const fileSize = Number(rf.fileSize || rf.file_size || 0);
1198
- const cloudUrl = rf.cloudUrl || rf.cloud_url || rf.url || (this.storageServerUrl ? `${this.storageServerUrl}/cdn/${fileId}` : '');
1205
+ const rawUrl = rf.cloudUrl || rf.cloud_url || rf.url || '';
1206
+ let cloudUrl = rawUrl;
1207
+ if (this.storageServerUrl) {
1208
+ if (rawUrl.startsWith('http')) {
1209
+ cloudUrl = this.storageServerUrl.startsWith('https://') ? rawUrl.replace(/^http:\/\//, 'https://') : rawUrl;
1210
+ } else if (rawUrl.startsWith('/')) {
1211
+ cloudUrl = `${this.storageServerUrl}${rawUrl}`;
1212
+ } else {
1213
+ cloudUrl = `${this.storageServerUrl}/cdn/${fileId}`;
1214
+ }
1215
+ }
1199
1216
  const cloudPublicId = rf.cloudPublicId || rf.cloud_public_id || fileId;
1200
1217
  const storageId = rf.storageId || rf.storage_id || null;
1201
1218
 
1202
1219
  if (this.query) {
1220
+ let validStorageId = null;
1221
+ if (storageId) {
1222
+ try {
1223
+ const sCheck = await this.query(`SELECT 1 FROM ${this.storagesTable} WHERE id = $1 LIMIT 1`, [storageId]);
1224
+ if (sCheck && sCheck.rows && sCheck.rows.length > 0) {
1225
+ validStorageId = storageId;
1226
+ }
1227
+ } catch {}
1228
+ }
1229
+
1203
1230
  const hasInspectionCol = await this._hasInspectionColumn();
1204
1231
  const insertSql = hasInspectionCol
1205
1232
  ? `INSERT INTO ${this.filesTable} (
@@ -1227,8 +1254,8 @@ export class StorageManager {
1227
1254
  RETURNING *`;
1228
1255
 
1229
1256
  const params = hasInspectionCol
1230
- ? [fileId, entityType, entityId, inspectionId, storageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId]
1231
- : [fileId, entityType, entityId, storageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId];
1257
+ ? [fileId, entityType, entityId, inspectionId, validStorageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId]
1258
+ : [fileId, entityType, entityId, validStorageId, originalName, fileType, mimeType, fileSize, cloudUrl, cloudPublicId];
1232
1259
 
1233
1260
  const r = await this.query(insertSql, params);
1234
1261
  this.fileMetadataCache.set(fileId, { data: r.rows[0], ts: Date.now() });