Skip to content

Commit

Permalink
fix: updated document id and added created date
Browse files Browse the repository at this point in the history
  • Loading branch information
dackers86 committed Oct 19, 2023
1 parent 659ff1e commit ddaa8d6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Binary file not shown.
16 changes: 16 additions & 0 deletions speech-to-text/functions/src/firestore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as admin from 'firebase-admin';
import config from './config';

import {Timestamp} from 'firebase-admin/firestore';

export async function updateFirestoreDocument(documentId: string, data: any) {
if (!config.collectionPath) return;

Expand All @@ -11,3 +13,17 @@ export async function updateFirestoreDocument(documentId: string, data: any) {

await document.set({...data}, {merge: true});
}

export async function getFirestoreDocument(fileName: string): Promise<string> {
if (!config.collectionPath) return '';

const db = admin.firestore();

const doc = await db.collection(config.collectionPath).add({
status: 'PROCESSING',
fileName,
created: Timestamp.now(),
});

return doc.id;
}
18 changes: 8 additions & 10 deletions speech-to-text/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {

import {Status, TranscribeAudioResult} from './types';
import config from './config';
import {updateFirestoreDocument} from './firestore';
import {getFirestoreDocument, updateFirestoreDocument} from './firestore';

admin.initializeApp();

Expand Down Expand Up @@ -61,20 +61,18 @@ export const transcribeAudio = functions.storage
return;
}

/** Set a sanitized document ID */
const sanitizedDocumentId = object.name?.replace(/\//g, '_');

/** Start tracking progress in Firestore, if configured */
await updateFirestoreDocument(sanitizedDocumentId, {status: 'PROCESSING'});

if (object.metadata && object.metadata.isTranscodeOutput === 'true') {
logs.audioAlreadyProcessed();
return;
}

/** Create a new document and extract the document ID */
const docId = await getFirestoreDocument(object.name);

if (!contentType) {
/** Start tracking progress in Firestore, if configured */
await updateFirestoreDocument(sanitizedDocumentId, {
await updateFirestoreDocument(docId, {
status: 'FAILED',
message: 'No content type provided.',
});
Expand All @@ -85,7 +83,7 @@ export const transcribeAudio = functions.storage

if (!contentType.startsWith('audio/')) {
/** Updated failed status Firestore, if configured */
await updateFirestoreDocument(sanitizedDocumentId!, {
await updateFirestoreDocument(docId!, {
status: 'FAILED',
message: 'Invalid content type.',
});
Expand Down Expand Up @@ -123,7 +121,7 @@ export const transcribeAudio = functions.storage
logs.debug('uploading transcoded file');

/** Update processing status */
await updateFirestoreDocument(sanitizedDocumentId, {
await updateFirestoreDocument(docId, {
status: 'PROCESSING',
message: 'Transcoding audio file.',
});
Expand Down Expand Up @@ -160,7 +158,7 @@ export const transcribeAudio = functions.storage
});

/** Update the collecton with the transcribed audio */
await updateFirestoreDocument(sanitizedDocumentId, {
await updateFirestoreDocument(docId, {
...(transcriptionResult as TranscribeAudioResult),
message: FieldValue.delete(),
status: Status[transcriptionResult.status],
Expand Down

0 comments on commit ddaa8d6

Please sign in to comment.