refactor: change used key for pronunciation cache to be independent of requesting locale, only on used voice

This commit is contained in:
Valentyne Stigloher 2025-09-14 19:56:46 +02:00
parent 8a7e9fe844
commit 8f5753012b

View File

@ -3,16 +3,16 @@ import { NoSuchKey } from '@aws-sdk/client-s3';
import type { S3 } from '@aws-sdk/client-s3';
import type { NodeJsClient } from '@smithy/types';
import { Base64 } from 'js-base64';
import sha1 from 'sha1';
import {
convertPronunciationStringToSsml,
convertPronunciationStringToNarakeetFormat,
sha256,
} from '#shared/helpers.ts';
import { voices } from '#shared/pronunciation/voices.ts';
import type { VoiceKey, AwsPollyVoice, NarakeetVoice, Voice } from '#shared/pronunciation/voices.ts';
import { s3, awsConfig, s3BucketParams } from '~~/server/cloudServices.ts';
import { getLocale, loadConfig, localesDict } from '~~/server/data.ts';
import { localesDict } from '~~/server/data.ts';
type ProviderKey = 'aws_polly' | 'narakeet';
interface Provider {
@ -82,9 +82,6 @@ export default defineEventHandler(async (event) => {
});
}
const locale = getLocale(event);
const config = await loadConfig(locale);
const voiceKey = getRouterParam(event, 'voice');
if (voiceKey === undefined || !(voiceKey in voices)) {
throw createError({
@ -96,7 +93,7 @@ export default defineEventHandler(async (event) => {
const voice = voices[voiceKey as VoiceKey];
const provider = providers[(voice.provider || 'aws_polly') as ProviderKey];
const tokenised = provider.tokenised(text);
const key = `pronunciation/${config.locale}-${voiceKey}/${sha1(tokenised)}.mp3`;
const key = `pronunciation/${voiceKey}/${await sha256(tokenised)}.mp3`;
try {
const s3Response = await (s3 as NodeJsClient<S3>).getObject({ Key: key, ...s3BucketParams });