diff --git a/speech-to-text/functions/__tests__/util.test.ts b/speech-to-text/functions/__tests__/util.test.ts new file mode 100644 index 000000000..528efc651 --- /dev/null +++ b/speech-to-text/functions/__tests__/util.test.ts @@ -0,0 +1,27 @@ +import {getTranscodedStoragePath} from '../src/util'; + +describe('getTranscodedStoragePath', () => { + it('uses the file basename when no output path is configured', () => { + expect(getTranscodedStoragePath('myDirectory/audio.wav')).toBe( + 'audio.wav.wav' + ); + }); + + it('writes nested input files under the configured output path once', () => { + expect( + getTranscodedStoragePath('myDirectory/audio.wav', 'transcriptions') + ).toBe('transcriptions/audio.wav.wav'); + }); + + it('normalizes leading and trailing slashes in the configured output path', () => { + expect( + getTranscodedStoragePath('nested/audio.wav', '/transcriptions/') + ).toBe('transcriptions/audio.wav.wav'); + }); + + it('does not duplicate the source directory when output path matches it', () => { + expect( + getTranscodedStoragePath('myDirectory/audio.wav', 'myDirectory') + ).toBe('myDirectory/audio.wav.wav'); + }); +}); diff --git a/speech-to-text/functions/src/index.ts b/speech-to-text/functions/src/index.ts index 29658a5fd..771f77dc9 100644 --- a/speech-to-text/functions/src/index.ts +++ b/speech-to-text/functions/src/index.ts @@ -24,7 +24,12 @@ import * as mkdirp from 'mkdirp'; import {FieldValue} from 'firebase-admin/firestore'; import * as logs from './logs'; -import {publishFailureEvent, errorFromAny, publishCompleteEvent} from './util'; +import { + publishFailureEvent, + errorFromAny, + publishCompleteEvent, + getTranscodedStoragePath, +} from './util'; import { transcodeToLinear16, transcribeAndUpload, @@ -126,11 +131,14 @@ export const transcribeAudio = functions.storage message: 'Transcoding audio file.', }); + const transcodedStoragePath = getTranscodedStoragePath( + filePath, + config.outputStoragePath + ); + const transcodedUploadResult = await uploadTranscodedFile({ localPath: transcodeResult.outputPath, - storagePath: config.outputStoragePath - ? `${config.outputStoragePath}${transcodeResult.outputPath}` - : transcodeResult.outputPath.slice(1), + storagePath: transcodedStoragePath, bucket: bucket, }); diff --git a/speech-to-text/functions/src/util.ts b/speech-to-text/functions/src/util.ts index e6021e840..bbba9118f 100644 --- a/speech-to-text/functions/src/util.ts +++ b/speech-to-text/functions/src/util.ts @@ -15,6 +15,7 @@ */ import * as util from 'util'; import * as ffmpeg from 'fluent-ffmpeg'; +import * as path from 'path'; import {Failure, TranscribeAudioSuccess} from './types'; import {Channel} from 'firebase-admin/eventarc'; import {google} from '@google-cloud/speech/build/protos/protos'; @@ -89,6 +90,18 @@ export const probePromise = util.promisify( ffmpeg.ffprobe ); +export function getTranscodedStoragePath( + objectName: string, + outputStoragePath?: string +): string { + const fileName = `${path.posix.basename(objectName)}.wav`; + const normalizedOutputPath = outputStoragePath?.replace(/^\/+|\/+$/g, ''); + + return normalizedOutputPath + ? `${normalizedOutputPath}/${fileName}` + : fileName; +} + export async function publishFailureEvent( eventChannel: Channel, {...contents}: Failure,