backend: fix URL path extraction to remove basePath prefix

This commit is contained in:
cruizba 2026-02-03 01:56:58 +01:00
parent 177648e6d5
commit b08bb10f63

View File

@ -2,6 +2,7 @@ import { MeetRoom, MeetRoomFilters, MeetRoomStatus } from '@openvidu-meet/typing
import { inject, injectable } from 'inversify';
import { MeetRoomDocument, MeetRoomModel } from '../models/mongoose-schemas/room.schema.js';
import { LoggerService } from '../services/logger.service.js';
import { getBasePath } from '../utils/html-injection.utils.js';
import { getBaseUrl } from '../utils/url.utils.js';
import { BaseRepository } from './base.repository.js';
@ -211,20 +212,35 @@ export class RoomRepository<TRoom extends MeetRoom = MeetRoom> extends BaseRepos
}
/**
* Extracts the path from a URL, removing the base URL if present.
* Extracts the path from a URL, removing the base URL and basePath if present.
* This ensures only the route path is stored in the database, without the basePath prefix.
*
* @param url - The URL to process
* @returns The path portion of the URL
* @returns The path portion of the URL without the basePath prefix
*/
private extractPathFromUrl(url: string): string {
// If already a path, return as-is
const basePath = getBasePath();
// Remove trailing slash from basePath for comparison (e.g., '/meet/' -> '/meet')
const basePathWithoutTrailingSlash = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
// Helper to strip basePath from a path
const stripBasePath = (path: string): string => {
if (basePathWithoutTrailingSlash !== '' && path.startsWith(basePathWithoutTrailingSlash)) {
return path.slice(basePathWithoutTrailingSlash.length) || '/';
}
return path;
};
// If already a path, strip basePath and return
if (url.startsWith('/')) {
return url;
return stripBasePath(url);
}
try {
const urlObj = new URL(url);
return urlObj.pathname + urlObj.search + urlObj.hash;
const pathname = stripBasePath(urlObj.pathname);
return pathname + urlObj.search + urlObj.hash;
} catch {
// If URL parsing fails, assume it's already a path
return url;