Set correct type (base64), remove isBase64

This commit is contained in:
Nicola Benaglia 2025-05-24 14:32:51 +02:00
parent 7deb2501e2
commit a778aa7a0f
2 changed files with 88 additions and 53 deletions

View File

@ -146,7 +146,6 @@ export const encryptAndPublishSymmetricKeyGroupChat = async ({
data: encryptedData, data: encryptedData,
file: encryptedData, file: encryptedData,
identifier: `symmetric-qchat-group-${groupId}`, identifier: `symmetric-qchat-group-${groupId}`,
isBase64: true,
registeredName, registeredName,
service: 'DOCUMENT_PRIVATE', service: 'DOCUMENT_PRIVATE',
uploadType: 'base64', uploadType: 'base64',
@ -212,14 +211,12 @@ export const encryptAndPublishSymmetricKeyGroupChatForAdmins = async ({
if (encryptedData) { if (encryptedData) {
const registeredName = await getNameInfo(); const registeredName = await getNameInfo();
const data = await publishData({ const data = await publishData({
registeredName,
data: encryptedData, data: encryptedData,
service: 'DOCUMENT_PRIVATE',
identifier: `admins-symmetric-qchat-group-${groupId}`,
uploadType: 'base64',
file: encryptedData, file: encryptedData,
uploadType: 'file', identifier: `admins-symmetric-qchat-group-${groupId}`,
isBase64: true, registeredName,
service: 'DOCUMENT_PRIVATE',
uploadType: 'base64',
withFee: true, withFee: true,
}); });
return { return {
@ -252,13 +249,12 @@ export const publishGroupEncryptedResource = async ({
}) })
); );
const data = await publishData({ const data = await publishData({
registeredName,
data: encryptedData, data: encryptedData,
uploadType: 'base64',
file: encryptedData, file: encryptedData,
service: 'DOCUMENT',
identifier, identifier,
isBase64: true, registeredName,
service: 'DOCUMENT',
uploadType: 'base64',
withFee: true, withFee: true,
}); });
return data; return data;
@ -275,19 +271,19 @@ export const publishGroupEncryptedResource = async ({
}; };
export const publishOnQDN = async ({ export const publishOnQDN = async ({
data,
identifier,
service,
title,
description,
category, category,
data,
description,
identifier,
name,
service,
tag1, tag1,
tag2, tag2,
tag3, tag3,
tag4, tag4,
tag5, tag5,
uploadType = 'file', title,
name, uploadType = 'base64',
}) => { }) => {
if (data && service) { if (data && service) {
const registeredName = name || (await getNameInfo()); const registeredName = name || (await getNameInfo());
@ -305,7 +301,6 @@ export const publishOnQDN = async ({
service, service,
identifier, identifier,
uploadType, uploadType,
isBase64: true,
withFee: true, withFee: true,
title, title,
description, description,

View File

@ -70,7 +70,6 @@ async function uploadChunkWithRetry(endpoint, formData, index, maxRetries = 3) {
await new Promise((res) => setTimeout(res, 10_000)); await new Promise((res) => setTimeout(res, 10_000));
} }
} }
return data;
} }
async function getKeyPair() { async function getKeyPair() {
@ -248,79 +247,120 @@ export const publishData = async ({
return signAndProcessRes; return signAndProcessRes;
}; };
const uploadData = async (registeredName: string, file: any, fee: number) => { const uploadData = async (registeredName: string, data: any, fee: number) => {
console.log('data', uploadType, data);
let postBody = ''; let postBody = '';
let urlSuffix = ''; let urlSuffix = '';
if (file != null) { if (data != null) {
// If we're sending zipped data, make sure to use the /zip version of the POST /arbitrary/* API if (uploadType === 'base64') {
if (uploadType === 'zip') {
urlSuffix = '/zip';
}
// If we're sending file data, use the /base64 version of the POST /arbitrary/* API
else if (uploadType === 'file') {
urlSuffix = '/base64'; urlSuffix = '/base64';
} }
// Base64 encode the file to work around compatibility issues between javascript and java byte arrays if (uploadType === 'base64') {
if (isBase64) { postBody = data;
postBody = file; }
} else {
throw new Error('No data provided');
} }
if (!isBase64) { let uploadDataUrl = `/arbitrary/${service}/${registeredName}`;
let fileBuffer = new Uint8Array(await file.arrayBuffer()); let paramQueries = '';
postBody = Buffer.from(fileBuffer).toString('base64');
}
}
let uploadDataUrl = `/arbitrary/${service}/${registeredName}${urlSuffix}`;
if (identifier?.trim().length > 0) { if (identifier?.trim().length > 0) {
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}`; uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}`;
} }
uploadDataUrl = uploadDataUrl + `?fee=${fee}`; paramQueries = paramQueries + `?fee=${fee}`;
if (filename != null && filename != 'undefined') { if (filename != null && filename != 'undefined') {
uploadDataUrl = paramQueries = paramQueries + '&filename=' + encodeURIComponent(filename);
uploadDataUrl + '&filename=' + encodeURIComponent(filename);
} }
if (title != null && title != 'undefined') { if (title != null && title != 'undefined') {
uploadDataUrl = uploadDataUrl + '&title=' + encodeURIComponent(title); paramQueries = paramQueries + '&title=' + encodeURIComponent(title);
} }
if (description != null && description != 'undefined') { if (description != null && description != 'undefined') {
uploadDataUrl = paramQueries =
uploadDataUrl + '&description=' + encodeURIComponent(description); paramQueries + '&description=' + encodeURIComponent(description);
} }
if (category != null && category != 'undefined') { if (category != null && category != 'undefined') {
uploadDataUrl = paramQueries = paramQueries + '&category=' + encodeURIComponent(category);
uploadDataUrl + '&category=' + encodeURIComponent(category);
} }
if (tag1 != null && tag1 != 'undefined') { if (tag1 != null && tag1 != 'undefined') {
uploadDataUrl = uploadDataUrl + '&tags=' + encodeURIComponent(tag1); paramQueries = paramQueries + '&tags=' + encodeURIComponent(tag1);
} }
if (tag2 != null && tag2 != 'undefined') { if (tag2 != null && tag2 != 'undefined') {
uploadDataUrl = uploadDataUrl + '&tags=' + encodeURIComponent(tag2); paramQueries = paramQueries + '&tags=' + encodeURIComponent(tag2);
} }
if (tag3 != null && tag3 != 'undefined') { if (tag3 != null && tag3 != 'undefined') {
uploadDataUrl = uploadDataUrl + '&tags=' + encodeURIComponent(tag3); paramQueries = paramQueries + '&tags=' + encodeURIComponent(tag3);
} }
if (tag4 != null && tag4 != 'undefined') { if (tag4 != null && tag4 != 'undefined') {
uploadDataUrl = uploadDataUrl + '&tags=' + encodeURIComponent(tag4); paramQueries = paramQueries + '&tags=' + encodeURIComponent(tag4);
} }
if (tag5 != null && tag5 != 'undefined') { if (tag5 != null && tag5 != 'undefined') {
uploadDataUrl = uploadDataUrl + '&tags=' + encodeURIComponent(tag5); paramQueries = paramQueries + '&tags=' + encodeURIComponent(tag5);
}
if (uploadType === 'zip') {
paramQueries = paramQueries + '&isZip=' + true;
} }
if (uploadType === 'base64') {
if (urlSuffix) {
uploadDataUrl = uploadDataUrl + urlSuffix;
}
uploadDataUrl = uploadDataUrl + paramQueries;
return await reusablePost(uploadDataUrl, postBody); return await reusablePost(uploadDataUrl, postBody);
}
const file = data;
const urlCheck = `/arbitrary/check-tmp-space?totalSize=${file.size}`;
const checkEndpoint = await createEndpoint(urlCheck);
const checkRes = await fetch(checkEndpoint);
if (!checkRes.ok) {
throw new Error('Not enough space on your hard drive');
}
const chunkUrl = uploadDataUrl + `/chunk`;
const chunkSize = 5 * 1024 * 1024; // 5MB
const totalChunks = Math.ceil(file.size / chunkSize);
for (let index = 0; index < totalChunks; index++) {
const start = index * chunkSize;
const end = Math.min(start + chunkSize, file.size);
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append('chunk', chunk, file.name); // Optional: include filename
formData.append('index', index);
await uploadChunkWithRetry(chunkUrl, formData, index);
}
const finalizeUrl = uploadDataUrl + `/finalize` + paramQueries;
const finalizeEndpoint = await createEndpoint(finalizeUrl);
const response = await fetch(finalizeEndpoint, {
method: 'POST',
headers: {},
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Finalize failed: ${errorText}`);
}
const result = await response.text(); // Base58-encoded unsigned transaction
return result;
}; };
try { try {