Skip to content

Commit

Permalink
Fix code for new MediaDownloadFailure type
Browse files Browse the repository at this point in the history
  • Loading branch information
philmcmahon committed Oct 10, 2024
1 parent 706ea26 commit f2b17c6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
9 changes: 7 additions & 2 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ export const TranscriptionOutputSuccess = TranscriptionOutputBase.extend({

export const MediaDownloadFailure = z.object({
id: z.string(),
status: z.literal('FAILURE'),
status: z.literal('MEDIA_DOWNLOAD_FAILURE'),
url: z.string(),
});

export type MediaDownloadFailure = z.infer<typeof MediaDownloadFailure>;

export const TranscriptionOutputFailure = TranscriptionOutputBase.extend({
status: z.literal('FAILURE'),
status: z.literal('TRANSCRIPTION_FAILURE'),
});

export const TranscriptionOutput = z.union([
Expand All @@ -108,6 +108,11 @@ export const transcriptionOutputIsSuccess = (
output: TranscriptionOutput,
): output is TranscriptionOutputSuccess => output.status === 'SUCCESS';

export const transcriptionOutputIsTranscriptionFailure = (
output: TranscriptionOutput,
): output is TranscriptionOutputSuccess =>
output.status === 'TRANSCRIPTION_FAILURE';

export type TranscriptionOutput = z.infer<typeof TranscriptionOutput>;

export const SignedUrlResponseBody = z.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/media-download/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const reportDownloadFailure = async (
) => {
const mediaDownloadFailure: MediaDownloadFailure = {
id: job.id,
status: 'FAILURE',
status: 'MEDIA_DOWNLOAD_FAILURE',
url: job.url,
};
const result = await sendMessage(
Expand Down
22 changes: 20 additions & 2 deletions packages/output-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
logger,
getConfig,
TranscriptionConfig,
getSignedDownloadUrl,
} from '@guardian/transcription-service-backend-common';
import {
getDynamoClient,
Expand All @@ -16,6 +17,7 @@ import {
transcriptionOutputIsSuccess,
TranscriptionOutputSuccess,
TranscriptionOutputFailure,
transcriptionOutputIsTranscriptionFailure,
} from '@guardian/transcription-service-common';
import {
MetricsService,
Expand All @@ -28,23 +30,27 @@ const successMessageBody = (
originalFilename: string,
rootUrl: string,
isTranslation: boolean,
sourceMediaDownloadUrl: string,
): string => {
const exportUrl = `${rootUrl}/export?transcriptId=${transcriptId}`;
return `
<h1>${isTranslation ? 'English translation ' : 'Transcription'} for ${originalFilename} ready</h1>
<p>Click <a href="${exportUrl}">here</a> to export to a google doc.</p>
<p><b>Note:</b> transcripts will expire after 7 days. Export your transcript to a doc now if you want to keep it. </p>
<p>Click <a href="${sourceMediaDownloadUrl}"> to download the input media.</p>
<p><b>Note:</b> transcripts and input media will expire after 7 days. Export your transcript to a doc now if you want to keep it. </p>
`;
};

const failureMessageBody = (
originalFilename: string,
id: string,
isTranslation: boolean,
sourceMediaDownloadUrl: string,
): string => {
return `
<h1>${isTranslation ? 'English translation ' : 'Transcription'}for ${originalFilename} has failed.</h1>
<p>Please make sure that the file is a valid audio or video file.</p>
<p>Click <a href="${sourceMediaDownloadUrl}"> to download the input media.</p>
<p>Contact the digital investigations team for support.</p>
<p>Transcription ID: ${id}</p>
`;
Expand All @@ -55,6 +61,7 @@ const handleTranscriptionSuccess = async (
transcriptionOutput: TranscriptionOutputSuccess,
sesClient: SESClient,
metrics: MetricsService,
sourceMediaDownloadUrl: string,
) => {
const dynamoItem: TranscriptionDynamoItem = {
id: transcriptionOutput.id,
Expand Down Expand Up @@ -86,6 +93,7 @@ const handleTranscriptionSuccess = async (
transcriptionOutput.originalFilename,
config.app.rootUrl,
transcriptionOutput.isTranslation,
sourceMediaDownloadUrl,
),
);

Expand All @@ -108,6 +116,7 @@ const handleTranscriptionFailure = async (
transcriptionOutput: TranscriptionOutputFailure,
sesClient: SESClient,
metrics: MetricsService,
sourceMediaDownloadUrl: string,
) => {
try {
await sendEmail(
Expand All @@ -119,6 +128,7 @@ const handleTranscriptionFailure = async (
transcriptionOutput.originalFilename,
transcriptionOutput.id,
transcriptionOutput.isTranslation,
sourceMediaDownloadUrl,
),
);

Expand Down Expand Up @@ -154,21 +164,29 @@ const processMessage = async (event: unknown) => {

for (const record of parsedEvent.data.Records) {
const transcriptionOutput = record.body;
const sourceMediaDownloadUrl = await getSignedDownloadUrl(
config.aws.region,
config.app.sourceMediaBucket,
transcriptionOutput.id,
7 * 24 * 60 * 60,
);
if (transcriptionOutputIsSuccess(transcriptionOutput)) {
logger.info('handling transcription success');
await handleTranscriptionSuccess(
config,
transcriptionOutput,
sesClient,
metrics,
sourceMediaDownloadUrl,
);
} else {
} else if (transcriptionOutputIsTranscriptionFailure(transcriptionOutput)) {
logger.info('handling transcription failure');
await handleTranscriptionFailure(
config,
transcriptionOutput,
sesClient,
metrics,
sourceMediaDownloadUrl,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const publishTranscriptionOutputFailure = async (
logger.info('publishing transcription output failed');
const failureMessage: TranscriptionOutputFailure = {
id: job.id,
status: 'FAILURE',
status: 'TRANSCRIPTION_FAILURE',
userEmail: job.userEmail,
originalFilename: job.originalFilename,
isTranslation: job.translate,
Expand Down

0 comments on commit f2b17c6

Please sign in to comment.