Skip to content

Commit

Permalink
Fix UI upload azure presigned URL checksum as hex md5 (#6770)
Browse files Browse the repository at this point in the history
* Fix UI upload azure presigned checksum as hex md5

* remove console log
  • Loading branch information
nopcoder authored Oct 12, 2023
1 parent b8e67e3 commit 7295bb7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
18 changes: 9 additions & 9 deletions webui/src/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,30 +590,30 @@ export const uploadWithProgress = (url, file, method = 'POST', onProgress = null
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', event => {
if (onProgress)
onProgress((event.loaded / event.total) * 100)
if (onProgress) {
onProgress((event.loaded / event.total) * 100);
}
});
xhr.addEventListener('load', () => {
resolve({
status: xhr.status,
body: xhr.responseText,
contentType: xhr.getResponseHeader('Content-Type'),
etag: xhr.getResponseHeader('ETag')
etag: xhr.getResponseHeader('ETag'),
contentMD5: xhr.getResponseHeader('Content-MD5'),
})
});
xhr.addEventListener('error', () => reject(new Error('Upload Failed')));
xhr.addEventListener('abort', () => reject(new Error('Upload Aborted')));
xhr.open(method, url, true);
xhr.setRequestHeader('Accept', 'application/json')
xhr.setRequestHeader('X-Lakefs-Client', 'lakefs-webui/__buildVersion')
Object.keys(additionalHeaders).map(function(key, _) {
xhr.setRequestHeader(key, additionalHeaders[key])
})
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-Lakefs-Client', 'lakefs-webui/__buildVersion');
Object.keys(additionalHeaders).map(key => xhr.setRequestHeader(key, additionalHeaders[key]))
if (url.startsWith(API_ENDPOINT)) {
// swagger API requires a form with a "content" field
const data = new FormData();
data.append('content', file);
xhr.send(data)
xhr.send(data);
} else {
xhr.send(file);
}
Expand Down
26 changes: 22 additions & 4 deletions webui/src/pages/repositories/repository/objects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ 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 uploadFile = async (config, repo, reference, path, file, onProgress) => {
const fpath = destinationPath(path, file);
Expand All @@ -234,12 +252,12 @@ const uploadFile = async (config, repo, reference, path, file, onProgress) => {
additionalHeaders = { "x-ms-blob-type": "BlockBlob" }
}
const getResp = await staging.get(repo.id, reference.id, fpath, config.pre_sign_support_ui);
const { status, etag } = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders)
if (status >= 400) {
const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders)
if (uploadResponse.status >= 400) {
throw new Error(`Error uploading file: HTTP ${status}`)
}
const hash = etag.replace(/"/g, "");
await staging.link(repo.id, reference.id, fpath, getResp, hash, file.size, file.type);
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);
}
Expand Down

0 comments on commit 7295bb7

Please sign in to comment.