using alternate method to base64 encode arraybuffer

This commit is contained in:
Roland Osborne 2023-04-27 16:20:32 -07:00
parent 18c88c38b6
commit 808d507182
2 changed files with 18 additions and 7 deletions

View File

@ -220,7 +220,8 @@ async function upload(entry, update, complete) {
const thumb = await getThumb(url, type, position);
const parts = [];
for (let pos = 0; pos < size; pos += ENCRYPTED_BLOCK_SIZE) {
const { blockEncrypted, blockIv } = await getEncryptedBlock(pos, ENCRYPTED_BLOCK_SIZE);
const len = pos + ENCRYPTED_BLOCK_SIZE > size ? size - pos : ENCRYPTED_BLOCK_SIZE;
const { blockEncrypted, blockIv } = await getEncryptedBlock(pos, len);
const partId = await axios.post(`${entry.baseUrl}block${entry.urlParams}`, blockEncrypted, {
signal: entry.cancel.signal,
onUploadProgress: (ev) => {

View File

@ -64,24 +64,34 @@ export function useAddTopic(contentKey) {
updateState({ enableImage, enableAudio, enableVideo });
}, [conversation.state.channel?.data?.channelDetail]);
const loadFile = (file) => {
return new Promise((resolve) => {
const loadFileData = (file) => {
return new Promise(resolve => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onloadend = (res) => { console.log(reader.result); resolve(reader.result) }
reader.readAsArrayBuffer(file)
})
};
const arrayBufferToBase64 = (buffer) => {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
const setUrl = async (file) => {
const url = URL.createObjectURL(file);
objects.current.push(url);
if (contentKey) {
const buffer = await loadFile(file)
const buffer = await loadFileData(file)
const getEncryptedBlock = (pos, len) => {
if (pos + len > buffer.byteLen) {
if (pos + len > buffer.byteLength) {
return null;
}
const block = btoa(String.fromCharCode.apply(null, buffer.slice(pos, len)));
const block = arrayBufferToBase64(buffer.slice(pos, len));
return encryptBlock(block, contentKey);
}
return { url, encrypted: true, size: buffer.byteLength, getEncryptedBlock };