mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-24 12:19:41 +00:00
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
// Copyright 2025 Signal Messenger, LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import type { AttachmentType } from '../types/Attachment.std.js';
|
|
import type { LoggerType } from '../types/Logging.std.js';
|
|
import { getLocalAttachmentUrl } from './getLocalAttachmentUrl.std.js';
|
|
import { toLogFormat } from '../types/errors.std.js';
|
|
|
|
export async function captureAudioDuration(
|
|
attachment: AttachmentType,
|
|
{
|
|
logger,
|
|
}: {
|
|
logger: LoggerType;
|
|
}
|
|
): Promise<AttachmentType> {
|
|
const audio = new window.Audio();
|
|
audio.muted = true;
|
|
audio.src = getLocalAttachmentUrl(attachment);
|
|
|
|
try {
|
|
await new Promise<void>((resolve, reject) => {
|
|
audio.addEventListener('loadedmetadata', () => {
|
|
resolve();
|
|
});
|
|
|
|
audio.addEventListener('error', event => {
|
|
const error = new Error(
|
|
`Failed to load audio from due to error: ${event.type}`
|
|
);
|
|
reject(error);
|
|
});
|
|
});
|
|
} catch (error) {
|
|
logger.warn(`captureAudioDuration failed ${toLogFormat(error)}`);
|
|
return attachment;
|
|
}
|
|
|
|
if (!Number.isNaN(audio.duration)) {
|
|
return {
|
|
...attachment,
|
|
duration: audio.duration,
|
|
};
|
|
}
|
|
|
|
return attachment;
|
|
}
|