Skip to content

Commit

Permalink
lets start
Browse files Browse the repository at this point in the history
  • Loading branch information
ItamarYuran committed Nov 13, 2024
1 parent 1c7a5f3 commit 635d4df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 111 deletions.
65 changes: 12 additions & 53 deletions webui/src/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,80 +661,39 @@ class Pulls {
export const uploadWithProgress = (url, file, method = 'POST', onProgress = null, additionalHeaders = null) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();

// Log the URL and method
console.log(`Starting upload: ${method} ${url}`);

xhr.upload.addEventListener('progress', event => {
if (onProgress) {
const progress = (event.loaded / event.total) * 100;
console.log(`Upload Progress: ${progress.toFixed(2)}%`);
onProgress(progress);
onProgress((event.loaded / event.total) * 100);
}
});

// Log Content-MD5 if available
if (additionalHeaders && additionalHeaders['Content-MD5']) {
console.log(`Content-MD5: ${additionalHeaders['Content-MD5']}`);
}

// Add load event listener to capture success response
xhr.addEventListener('load', () => {
console.log("Upload complete. Status:", xhr.status);
if (xhr.status >= 200 && xhr.status < 300) {
console.log("Response body:", xhr.responseText);
resolve({
status: xhr.status,
body: xhr.responseText,
contentType: xhr.getResponseHeader('Content-Type'),
etag: xhr.getResponseHeader('ETag'),
contentMD5: xhr.getResponseHeader('Content-MD5'),
});
} else {
console.error(`Upload failed with status ${xhr.status}:`, xhr.responseText);
reject(new Error(`Upload failed with status ${xhr.status}: ${xhr.responseText}`));
}
});

// Add error event listener for network issues
xhr.addEventListener('error', () => {
console.error("Network error during upload.");
reject(new Error('Upload Failed due to network error.'));
});

// Add abort event listener
xhr.addEventListener('abort', () => {
console.warn("Upload aborted by user or network.");
reject(new Error('Upload Aborted'));
resolve({
status: xhr.status,
body: xhr.responseText,
contentType: xhr.getResponseHeader('Content-Type'),
etag: xhr.getResponseHeader('ETag'),
contentMD5: xhr.getResponseHeader('Content-MD5'),
})
});

// Open connection
xhr.addEventListener('error', () => reject(new Error('Upload Failed')));
xhr.addEventListener('abort', () => reject(new Error('Upload Aborted')));
xhr.open(method, url, true);

// Set headers and log each one
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-Lakefs-Client', 'lakefs-webui/__buildVersion');
if (additionalHeaders) {
Object.keys(additionalHeaders).forEach(key => {
console.log(`Setting header ${key}: ${additionalHeaders[key]}`);
xhr.setRequestHeader(key, additionalHeaders[key]);
});
Object.keys(additionalHeaders).map(key => xhr.setRequestHeader(key, additionalHeaders[key]))
}

// Send the file
if (url.startsWith(API_ENDPOINT)) {
console.log("Sending file as multipart form-data.");
// swagger API requires a form with a "content" field
const data = new FormData();
data.append('content', file);
xhr.send(data);
} else {
console.log("Sending file as raw binary data.");
xhr.send(file);
}
});
};


class Objects {

async list(repoId, ref, tree, after = "", presign = false, amount = DEFAULT_LISTING_AMOUNT, delimiter = "/") {
Expand Down
60 changes: 2 additions & 58 deletions webui/src/pages/repositories/repository/objects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,61 +227,8 @@ const ImportModal = ({config, repoId, referenceId, referenceType, path = '', onD
);
};

// function extractChecksumFromResponse(response) {
// if (response.contentMD5) {
// // convert base64 to hex
// const raw = atob(response.contentMD5)
// let result = '';
// for (let i = 0; i < raw.length; i++) {
// const hex = raw.charCodeAt(i).toString(16);
// result += (hex.length === 2 ? hex : '0' + hex);
// }
// return result;
// }

// if (response.etag) {
// // drop any quote and space
// return response.etag.replace(/[" ]+/g, "");
// }
// return ""
// }
// const calculateMD5Checksum = async (file) => {
// const arrayBuffer = await file.arrayBuffer();
// const hashBuffer = await crypto.subtle.digest('MD5', arrayBuffer);
// const hashArray = Array.from(new Uint8Array(hashBuffer));
// const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
// return hashHex;
// };

// const uploadFile = async (config, repo, reference, path, file, onProgress) => {
// const fpath = destinationPath(path, file);
// if (config.pre_sign_support_ui) {
// let additionalHeaders;
// if (config.blockstore_type === "azure") {
// additionalHeaders = { "x-ms-blob-type": "BlockBlob" }
// }
// const arrayBuffer = await file.arrayBuffer();
// const hashBuffer = await crypto.subtle.digest('MD5', arrayBuffer);
// const md5ChecksumBase64 = btoa(String.fromCharCode(...new Uint8Array(hashBuffer)));
// additionalHeaders['Content-MD5'] = md5ChecksumBase64;
// const getResp = await staging.get(repo.id, reference.id, fpath, config.pre_sign_support_ui);
// //console.log("sum: ", md5Checksum)
// const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders)
// if (uploadResponse.status >= 400) {
// throw new Error(`Error uploading file: HTTP ${status}`)
// }
// const checksum = extractChecksumFromResponse(uploadResponse)
// await staging.link(repo.id, reference.id, fpath, getResp, checksum, file.size, file.type);
// } else {
// await objects.upload(repo.id, reference.id, fpath, file, onProgress);
// }
// };
const uploadFile = async (config, repo, reference, path, file, onProgress) => {
console.log("Starting uploadFile...");

const fpath = destinationPath(path, file);
console.log("Destination path:", fpath);

const uploadFile = async (config, repo, reference, path, file, onProgress) => {
const fpath = destinationPath(path, file);
if (config.pre_sign_support_ui) {
let additionalHeaders = {};

Expand All @@ -295,8 +242,6 @@ const uploadFile = async (config, repo, reference, path, file, onProgress) => {
const wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
const md5Checksum = CryptoJS.MD5(wordArray).toString(CryptoJS.enc.Base64);
additionalHeaders["Content-MD5"] = md5Checksum;
console.log("MD5 Checksum (Base64):", md5Checksum);


const getResp = await staging.get(repo.id, reference.id, fpath, config.pre_sign_support_ui, md5Checksum);
const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders);
Expand All @@ -305,7 +250,6 @@ const uploadFile = async (config, repo, reference, path, file, onProgress) => {
throw new Error(`Error uploading file: HTTP ${uploadResponse.status}`);
}

//const checksum = extractChecksumFromResponse(uploadResponse);
await staging.link(repo.id, reference.id, fpath, getResp, md5Checksum, file.size, file.type);
} else {
console.log("Pre-sign support is disabled, using direct upload.");
Expand Down

0 comments on commit 635d4df

Please sign in to comment.