Changeset View
Changeset View
Standalone View
Standalone View
web/media/encryption-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import { hexToUintArray, uintArrayToHexString } from 'lib/media/data-utils.js'; | import { hexToUintArray, uintArrayToHexString } from 'lib/media/data-utils.js'; | ||||
import { fileInfoFromData } from 'lib/media/file-utils.js'; | import { fileInfoFromData } from 'lib/media/file-utils.js'; | ||||
import { fetchableMediaURI } from 'lib/media/media-utils.js'; | |||||
import type { | import type { | ||||
MediaMissionFailure, | MediaMissionFailure, | ||||
MediaMissionStep, | MediaMissionStep, | ||||
} from 'lib/types/media-types.js'; | } from 'lib/types/media-types.js'; | ||||
import { getMessageForException } from 'lib/utils/errors.js'; | import { getMessageForException } from 'lib/utils/errors.js'; | ||||
import { calculatePaddedLength, pad, unpad } from 'lib/utils/pkcs7-padding.js'; | import { calculatePaddedLength, pad, unpad } from 'lib/utils/pkcs7-padding.js'; | ||||
import * as AES from './aes-crypto-utils.js'; | import * as AES from './aes-crypto-utils.js'; | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | |||||
}> { | }> { | ||||
let success = true; | let success = true; | ||||
let exceptionMessage; | let exceptionMessage; | ||||
const steps: DecryptFileStep[] = []; | const steps: DecryptFileStep[] = []; | ||||
// Step 1 - Fetch the encrypted media and convert it to a Uint8Array | // Step 1 - Fetch the encrypted media and convert it to a Uint8Array | ||||
let data; | let data; | ||||
const fetchStartTime = Date.now(); | const fetchStartTime = Date.now(); | ||||
const url = fetchableMediaURI(holder); | |||||
try { | try { | ||||
const response = await fetch(holder); | const response = await fetch(url); | ||||
if (!response.ok) { | |||||
throw new Error(`HTTP error ${response.status}: ${response.statusText}`); | |||||
} | |||||
const buffer = await response.arrayBuffer(); | const buffer = await response.arrayBuffer(); | ||||
data = new Uint8Array(buffer); | data = new Uint8Array(buffer); | ||||
} catch (e) { | } catch (e) { | ||||
success = false; | success = false; | ||||
exceptionMessage = getMessageForException(e); | exceptionMessage = getMessageForException(e); | ||||
} | } | ||||
steps.push({ | steps.push({ | ||||
step: 'fetch_buffer', | step: 'fetch_buffer', | ||||
url: holder, | url, | ||||
time: Date.now() - fetchStartTime, | time: Date.now() - fetchStartTime, | ||||
success, | success, | ||||
exceptionMessage, | exceptionMessage, | ||||
}); | }); | ||||
if (!success || !data) { | if (!success || !data) { | ||||
return { | return { | ||||
steps, | steps, | ||||
result: { success: false, reason: 'fetch_failed' }, | result: { success: false, reason: 'fetch_failed' }, | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |