Prevent synthesizing PDF

This commit is contained in:
Hongbo Wu
2022-08-25 17:01:29 +08:00
parent 3cea785336
commit 3945c2f887
2 changed files with 18 additions and 7 deletions

View File

@ -36,17 +36,17 @@ export function speechServiceRouter() {
}
try {
const data: { userId: string; type: string; id: string } =
const data: { userId: string; type: string; id: string; state: string } =
JSON.parse(msgStr)
const { userId, type, id } = data
const { userId, type, id, state } = data
if (!userId || !type || !id) {
logger.info('Invalid data')
return res.status(400).send('Bad Request')
}
if (type.toUpperCase() !== 'PAGE') {
logger.info('Not a page')
return res.status(200).send('Not a page')
if (type.toUpperCase() !== 'PAGE' || state !== 'SUCCEEDED') {
logger.info('Not a page or not succeeded')
return res.status(200).send('Not a page or not succeeded')
}
const page = await getPageById(id)
@ -63,6 +63,7 @@ export function speechServiceRouter() {
user: { id: userId },
elasticPageId: id,
state: SpeechState.INITIALIZED,
voice: 'en-US-JennyNeural',
})
await synthesize(page, speech)
logger.info('page synthesized')

View File

@ -1,7 +1,7 @@
import { getRepository } from '../entity/utils'
import { Speech, SpeechState } from '../entity/speech'
import { searchPages } from '../elastic/pages'
import { Page } from '../elastic/types'
import { Page, PageType } from '../elastic/types'
import { SortBy, SortOrder } from '../utils/search'
import { synthesizeTextToSpeech } from '../utils/textToSpeech'
@ -21,6 +21,11 @@ export const shouldSynthesize = async (
userId: string,
page: Page
): Promise<boolean> => {
if (page.pageType === PageType.File || !page.content) {
// we don't synthesize files for now
return false
}
if (process.env.TEXT_TO_SPEECH_BETA_TEST) {
return true
}
@ -52,7 +57,12 @@ export const shouldSynthesize = async (
export const synthesize = async (page: Page, speech: Speech): Promise<void> => {
try {
console.log('synthesizing', speech.id)
if (page.pageType === PageType.File || !page.content) {
// we don't synthesize files for now
return
}
console.log('Start synthesizing', { pageId: page.id, speechId: speech.id })
const startTime = Date.now()
const speechOutput = await synthesizeTextToSpeech({
id: speech.id,