Return audio file if exists

This commit is contained in:
Hongbo Wu 2022-11-08 11:38:14 +08:00
parent e62765c3e6
commit e3329d0a5f
2 changed files with 28 additions and 0 deletions

View file

@ -86,6 +86,25 @@ export const createGCSFile = (bucket: string, filename: string): File => {
return storage.bucket(bucket).file(filename)
}
export const listGCSFiles = async (
bucket: string,
prefix: string
): Promise<string[]> => {
const [files] = await storage.bucket(bucket).getFiles({
prefix,
})
return files.map((file) => file.name)
}
export const downloadFromBucket = async (
bucket: string,
filename: string
): Promise<Buffer> => {
const file = createGCSFile(bucket, filename)
const [data] = await file.download()
return data
}
const updateSpeech = async (
speechId: string,
token: string,

View file

@ -54,6 +54,15 @@ export class RealisticTextToSpeech implements TextToSpeech {
// audio file to be saved in GCS
const audioFileName = `speech/${input.key}.mp3`
const audioFile = createGCSFile(bucket, audioFileName)
if (await audioFile.exists()) {
console.debug('Audio file already exists')
const [audioData] = await audioFile.download()
return {
audioData,
speechMarks: [],
}
}
const outputStream = audioFile.createWriteStream({
resumable: true,
}) as PassThrough