harden media

This commit is contained in:
2025-12-31 15:39:28 +01:00
parent 92226800df
commit 8260bf7baf
413 changed files with 8361 additions and 1544 deletions

View File

@@ -1,4 +1,5 @@
import { Driver } from '@core/racing/domain/entities/Driver';
import { MediaReference } from '@core/domain/media/MediaReference';
import { faker } from '@faker-js/faker';
import { seedId } from './SeedIdHelper';
@@ -26,22 +27,23 @@ export class RacingDriverFactory {
) {}
/**
* Get deterministic avatar URL for a driver based on their ID
* Uses static files from the website public directory
* Get deterministic MediaReference for a driver's avatar based on their ID
* Uses hash % 3 to determine variant: 0 -> male, 1 -> female, 2 -> neutral
*/
getDriverAvatarUrl(driverId: string): string {
// Deterministic selection based on driver ID
getDriverAvatarRef(driverId: string): MediaReference {
// Deterministic selection based on driver ID hash
const numericSuffixMatch = driverId.match(/(\d+)$/);
let useFemale = false;
let useNeutral = false;
let avatarVariant: 'male' | 'female' | 'neutral';
if (numericSuffixMatch && numericSuffixMatch[1]) {
const numericSuffix = parseInt(numericSuffixMatch[1], 10);
// 40% female, 40% male, 20% neutral
if (numericSuffix % 5 === 0) {
useNeutral = true;
} else if (numericSuffix % 2 === 0) {
useFemale = true;
const hashMod = numericSuffix % 3;
if (hashMod === 0) {
avatarVariant = 'male';
} else if (hashMod === 1) {
avatarVariant = 'female';
} else {
avatarVariant = 'neutral';
}
} else {
// Fallback hash
@@ -49,22 +51,18 @@ export class RacingDriverFactory {
for (let i = 0; i < driverId.length; i++) {
hash = (hash * 31 + driverId.charCodeAt(i)) | 0;
}
const hashValue = Math.abs(hash);
if (hashValue % 5 === 0) {
useNeutral = true;
} else if (hashValue % 2 === 0) {
useFemale = true;
const hashMod = Math.abs(hash) % 3;
if (hashMod === 0) {
avatarVariant = 'male';
} else if (hashMod === 1) {
avatarVariant = 'female';
} else {
avatarVariant = 'neutral';
}
}
// Return static file paths that Next.js can serve
if (useNeutral) {
return '/images/avatars/neutral-default-avatar.jpeg';
} else if (useFemale) {
return '/images/avatars/female-default-avatar.jpeg';
} else {
return '/images/avatars/male-default-avatar.jpg';
}
// Create system-default reference with avatar variant
return MediaReference.systemDefault(avatarVariant);
}
create(): Driver[] {
@@ -99,6 +97,8 @@ export class RacingDriverFactory {
// Assign category - use all available categories
const category = faker.helpers.arrayElement(categories);
const driverId = seedId(`driver-${i}`, this.persistence);
const driverData: {
id: string;
iracingId: string;
@@ -107,13 +107,15 @@ export class RacingDriverFactory {
bio?: string;
joinedAt?: Date;
category?: string;
avatarRef: MediaReference;
} = {
id: seedId(`driver-${i}`, this.persistence),
id: driverId,
iracingId: String(100000 + i),
name: faker.person.fullName(),
country: faker.helpers.arrayElement(countries),
joinedAt,
category,
avatarRef: this.getDriverAvatarRef(driverId),
};
if (hasBio) {