Changeset View
Changeset View
Standalone View
Standalone View
native/media/encryption-utils.js
Show All 16 Lines | |||||
import { getMessageForException } from 'lib/utils/errors.js'; | import { getMessageForException } from 'lib/utils/errors.js'; | ||||
import { pad, unpad, calculatePaddedLength } from 'lib/utils/pkcs7-padding.js'; | import { pad, unpad, calculatePaddedLength } from 'lib/utils/pkcs7-padding.js'; | ||||
import { temporaryDirectoryPath } from './file-utils.js'; | import { temporaryDirectoryPath } from './file-utils.js'; | ||||
import { getFetchableURI } from './identifier-utils.js'; | import { getFetchableURI } from './identifier-utils.js'; | ||||
import type { MediaResult } from './media-utils.js'; | import type { MediaResult } from './media-utils.js'; | ||||
import { commUtilsModule } from '../native-modules.js'; | import { commUtilsModule } from '../native-modules.js'; | ||||
import * as AES from '../utils/aes-crypto-module.js'; | import * as AES from '../utils/aes-crypto-module.js'; | ||||
import { arrayBufferFromBlob } from '../utils/blob-utils-module.js'; | |||||
const PADDING_THRESHOLD = 5000000; // we don't pad files larger than this | const PADDING_THRESHOLD = 5000000; // we don't pad files larger than this | ||||
type EncryptedFileResult = { | type EncryptedFileResult = { | ||||
+success: true, | +success: true, | ||||
+uri: string, | +uri: string, | ||||
+sha256Hash: string, | +sha256Hash: string, | ||||
+encryptionKey: string, | +encryptionKey: string, | ||||
Show All 25 Lines | async function encryptFile(uri: string): Promise<{ | ||||
try { | try { | ||||
const path = pathFromURI(uri); | const path = pathFromURI(uri); | ||||
// for local paths (file:// URI) we can use native module which is faster | // for local paths (file:// URI) we can use native module which is faster | ||||
if (path) { | if (path) { | ||||
const buffer = await commUtilsModule.readBufferFromFile(path); | const buffer = await commUtilsModule.readBufferFromFile(path); | ||||
data = new Uint8Array(buffer); | data = new Uint8Array(buffer); | ||||
} else { | } else { | ||||
const response = await fetch(getFetchableURI(uri)); | const response = await fetch(getFetchableURI(uri)); | ||||
const buffer = await response.arrayBuffer(); | const blob = await response.blob(); | ||||
const buffer = arrayBufferFromBlob(blob); | |||||
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: 'read_plaintext_file', | step: 'read_plaintext_file', | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | ): Promise<{ | ||||
// Step 1. Fetch the file and convert it to a Uint8Array | // Step 1. Fetch the file and convert it to a Uint8Array | ||||
const fetchStartTime = Date.now(); | const fetchStartTime = Date.now(); | ||||
let data; | let data; | ||||
try { | try { | ||||
const response = await fetch(getFetchableURI(holder)); | const response = await fetch(getFetchableURI(holder)); | ||||
if (!response.ok) { | if (!response.ok) { | ||||
throw new Error(`HTTP error ${response.status}: ${response.statusText}`); | throw new Error(`HTTP error ${response.status}: ${response.statusText}`); | ||||
} | } | ||||
const buf = await response.arrayBuffer(); | const blob = await response.blob(); | ||||
data = new Uint8Array(buf); | const buffer = arrayBufferFromBlob(blob); | ||||
data = new Uint8Array(buffer); | |||||
} catch (e) { | } catch (e) { | ||||
success = false; | success = false; | ||||
exceptionMessage = getMessageForException(e); | exceptionMessage = getMessageForException(e); | ||||
} | } | ||||
steps.push({ | steps.push({ | ||||
step: 'fetch_file', | step: 'fetch_file', | ||||
file: holder, | file: holder, | ||||
time: Date.now() - fetchStartTime, | time: Date.now() - fetchStartTime, | ||||
▲ Show 20 Lines • Show All 128 Lines • Show Last 20 Lines |