From e3329d0a5f90a204809c6aac655fac0884e1bca9 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 8 Nov 2022 11:38:14 +0800 Subject: [PATCH] Return audio file if exists --- packages/text-to-speech/src/index.ts | 19 +++++++++++++++++++ .../src/realisticTextToSpeech.ts | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index 7bdb5f40f..86a048aa0 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -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 => { + const [files] = await storage.bucket(bucket).getFiles({ + prefix, + }) + return files.map((file) => file.name) +} + +export const downloadFromBucket = async ( + bucket: string, + filename: string +): Promise => { + const file = createGCSFile(bucket, filename) + const [data] = await file.download() + return data +} + const updateSpeech = async ( speechId: string, token: string, diff --git a/packages/text-to-speech/src/realisticTextToSpeech.ts b/packages/text-to-speech/src/realisticTextToSpeech.ts index 19bde0f5a..2a0603d08 100644 --- a/packages/text-to-speech/src/realisticTextToSpeech.ts +++ b/packages/text-to-speech/src/realisticTextToSpeech.ts @@ -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