|
| 1 | +import { ConduitGrpcSdk } from '@conduitplatform/grpc-sdk'; |
| 2 | +import { ConfigController } from '@conduitplatform/module-tools'; |
| 3 | +import { File } from '../models/index.js'; |
| 4 | +import { applyCdnHost } from '../utils/index.js'; |
| 5 | + |
| 6 | +export type CdnConfiguration = Record<string, string>; |
| 7 | + |
| 8 | +/** |
| 9 | + * Compares two CDN configurations and returns the list of containers |
| 10 | + * that have actually changed (added, removed, or value modified). |
| 11 | + * Order-independent comparison. |
| 12 | + */ |
| 13 | +export function getChangedContainers( |
| 14 | + previousConfig: CdnConfiguration, |
| 15 | + currentConfig: CdnConfiguration, |
| 16 | +): string[] { |
| 17 | + const changedContainers: string[] = []; |
| 18 | + const allContainers = new Set([ |
| 19 | + ...Object.keys(previousConfig), |
| 20 | + ...Object.keys(currentConfig), |
| 21 | + ]); |
| 22 | + |
| 23 | + for (const container of allContainers) { |
| 24 | + const prevValue = previousConfig[container]; |
| 25 | + const currValue = currentConfig[container]; |
| 26 | + |
| 27 | + // Changed if: value differs, was added, or was removed |
| 28 | + if (prevValue !== currValue) { |
| 29 | + changedContainers.push(container); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return changedContainers; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Checks if two CDN configurations are equivalent (order-independent). |
| 38 | + */ |
| 39 | +export function cdnConfigsAreEqual( |
| 40 | + configA: CdnConfiguration | undefined, |
| 41 | + configB: CdnConfiguration | undefined, |
| 42 | +): boolean { |
| 43 | + const a = configA ?? {}; |
| 44 | + const b = configB ?? {}; |
| 45 | + |
| 46 | + const keysA = Object.keys(a); |
| 47 | + const keysB = Object.keys(b); |
| 48 | + |
| 49 | + // Different number of keys = not equal |
| 50 | + if (keysA.length !== keysB.length) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // Check each key has the same value |
| 55 | + for (const key of keysA) { |
| 56 | + if (a[key] !== b[key]) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Migration to update file URLs when CDN configuration changes. |
| 66 | + * Compares the new CDN config with the previous one and updates affected files. |
| 67 | + */ |
| 68 | +export async function migrateCdnConfigChanges( |
| 69 | + grpcSdk: ConduitGrpcSdk, |
| 70 | + previousCdnConfig: CdnConfiguration, |
| 71 | +): Promise<void> { |
| 72 | + const logger = ConduitGrpcSdk.Logger; |
| 73 | + const currentConfig = ConfigController.getInstance().config; |
| 74 | + const currentCdnConfig = (currentConfig.cdnConfiguration ?? {}) as CdnConfiguration; |
| 75 | + |
| 76 | + // Find containers with changed CDN configuration (order-independent) |
| 77 | + const changedContainers = getChangedContainers(previousCdnConfig, currentCdnConfig); |
| 78 | + |
| 79 | + if (changedContainers.length === 0) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + logger.log( |
| 84 | + `CDN configuration changed for ${ |
| 85 | + changedContainers.length |
| 86 | + } container(s): ${changedContainers.join(', ')}`, |
| 87 | + ); |
| 88 | + |
| 89 | + // Update URLs for files in changed containers |
| 90 | + for (const container of changedContainers) { |
| 91 | + try { |
| 92 | + // Find all public files in this container |
| 93 | + const allFiles = await File.getInstance().findMany({ |
| 94 | + container, |
| 95 | + isPublic: true, |
| 96 | + }); |
| 97 | + |
| 98 | + // Filter to only files with a sourceUrl |
| 99 | + const files = allFiles.filter(f => f.sourceUrl); |
| 100 | + |
| 101 | + if (files.length === 0) { |
| 102 | + logger.log(`No public files to update in container "${container}"`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + logger.log(`Updating ${files.length} file(s) in container "${container}"`); |
| 107 | + |
| 108 | + // Update each file's URL with the new CDN host |
| 109 | + for (const file of files) { |
| 110 | + const newUrl = applyCdnHost(file.sourceUrl, container); |
| 111 | + await File.getInstance().findByIdAndUpdate(file._id, { |
| 112 | + url: newUrl, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + logger.log(`Successfully updated files in container "${container}"`); |
| 117 | + } catch (error) { |
| 118 | + logger.error( |
| 119 | + `Failed to update files in container "${container}": ${(error as Error).message}`, |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + logger.log('CDN configuration migration completed'); |
| 125 | +} |
0 commit comments