Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const callIngestor = require('./utils/callIngestor');
const path = require('path');
const {DEBUG_MODE, SCHEDULER_ENABLED, SCHEDULED_INGESTOR_TIMES, printLog} = require('./constants.js')
const ClipUtils = require('./utils/ClipUtils');
const { AUDIO_EXTENSIONS, storageKeyFromUrl } = require('./utils/audioFormat');
const { AUDIO_EXTENSIONS, storageKeyFromUrl, rewriteAudioUrlsDeep } = require('./utils/audioFormat');
const { getPodcastFeed } = require('./utils/LandingPageService');
const {WorkProductV2, calculateLookupHash} = require('./models/WorkProductV2')
const QueueJob = require('./models/QueueJob');
Expand Down Expand Up @@ -227,6 +227,16 @@ app.use(cors(corsOptions));
app.enable('trust proxy');
app.set('trust proxy', true);
app.use(express.json());

// Rewrite any audioUrl in JSON responses to the Cloudflare-cached host, so no
// endpoint — including ones that dump raw metadataRaw — hands agents/clients the
// raw DigitalOcean origin (which bypasses the cache). Idempotent; no-ops on
// non-bucket URLs. SSE endpoints use res.write and are unaffected.
app.use((req, res, next) => {
const sendJson = res.json.bind(res);
res.json = (body) => sendJson(rewriteAudioUrlsDeep(body));
next();
});
app.use(cookieParser()); // Add this line before session middleware

// Add session middleware
Expand Down
28 changes: 27 additions & 1 deletion utils/audioFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,30 @@ function publicAudioUrl(url) {
return url.replace(SPACES_AUDIO_HOST_RE, PUBLIC_AUDIO_HOST);
}

module.exports = { AUDIO_EXTENSIONS, storageKeyFromUrl, publicAudioUrl, PUBLIC_AUDIO_HOST };
/**
* Recursively rewrite every `audioUrl` string field within a value (in place) to
* the public Cloudflare host. Catches audioUrl carried inside dumped `metadataRaw`
* objects / arrays that per-call-site wrapping misses. No-ops on non-bucket URLs
* and non-string values, so it's safe to run over any JSON response body.
*
* @param {*} value
* @returns {*} the same value, mutated in place
*/
function rewriteAudioUrlsDeep(value) {
if (!value || typeof value !== 'object') return value;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) rewriteAudioUrlsDeep(value[i]);
return value;
}
for (const key of Object.keys(value)) {
const v = value[key];
if (key === 'audioUrl' && typeof v === 'string') {
value[key] = publicAudioUrl(v);
} else if (v && typeof v === 'object') {
rewriteAudioUrlsDeep(v);
}
}
return value;
}

module.exports = { AUDIO_EXTENSIONS, storageKeyFromUrl, publicAudioUrl, PUBLIC_AUDIO_HOST, rewriteAudioUrlsDeep };