Skip to content

Commit

Permalink
classy
Browse files Browse the repository at this point in the history
  • Loading branch information
ItamarYuran committed Nov 21, 2024
1 parent 4bc6cdb commit 5934a42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
9 changes: 4 additions & 5 deletions webui/src/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,7 @@ export const uploadWithProgress = (url, file, method = 'POST', onProgress = null
status: xhr.status,
body: xhr.responseText,
contentType: xhr.getResponseHeader('Content-Type'),
rawHeaders: xhr.getAllResponseHeaders(), // add raw headers here
xhr: xhr,
rawHeaders: xhr.getAllResponseHeaders(), // add raw headers
});
});
xhr.addEventListener('error', () => reject(new Error('Upload Failed')));
Expand Down Expand Up @@ -1124,9 +1123,9 @@ class Statistics {
}

class Staging {
async get(repoId, branchId, path, presign = false,checksum = null) {
const query = qs({ path, presign,checksum });
const response = await apiRequest(`/repositories/${encodeURIComponent(repoId)}/branches/${encodeURIComponent(branchId)}/staging/backing?` + query, {
async get(repoId, branchId, path, presign = false) {
const query = qs({path, presign});
const response = await apiRequest(`/repositories/${encodeURIComponent(repoId)}/branches/${encodeURIComponent(branchId)}/staging/backing?` + query, {
method: 'GET'
});
if (response.status !== 200) {
Expand Down
50 changes: 22 additions & 28 deletions webui/src/pages/repositories/repository/objects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,53 +226,47 @@ const ImportModal = ({config, repoId, referenceId, referenceType, path = '', onD
);
};

const extractChecksumFromRawHeaders = (rawHeaders) => {
const headersString = typeof rawHeaders === 'string' ? rawHeaders : rawHeaders.toString();
const cleanedHeadersString = headersString.trim();
const headerLines = cleanedHeadersString.split('\n');
function extractChecksumFromResponse(rawHeaders) {
const headersString = typeof rawHeaders === 'string' ? rawHeaders : rawHeaders.toString();
const cleanedHeadersString = headersString.trim();
const headerLines = cleanedHeadersString.split('\n');
const parsedHeaders = {};

const parsedHeaders = {};
headerLines.forEach((line) => {
headerLines.forEach((line) => {
const [key, value] = line.split(':', 2).map((part) => part.trim());
if (key && value) {
parsedHeaders[key.toLowerCase()] = value;
}
});

if (parsedHeaders['content-md5']) {
console.log("Found content-md5:", parsedHeaders['content-md5']);
return parsedHeaders['content-md5'];
}

// fallback to ETag
if (parsedHeaders['etag']) {
const cleanedEtag = parsedHeaders['etag'].replace(/"/g, '');
return cleanedEtag;
}
return null;
};
}

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";
console.log("Azure storage detected, setting BlockBlob header");
}

const getResp = await staging.get(repo.id, reference.id, fpath, config.pre_sign_support_ui);
const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress);
const checksum = extractChecksumFromRawHeaders(uploadResponse.rawHeaders);

if (uploadResponse.status >= 400) {
throw new Error(`Error uploading file: HTTP ${uploadResponse.status}`);
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 getResp = await staging.get(repo.id, reference.id, fpath, config.pre_sign_support_ui);
const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders);
const checksum = extractChecksumFromResponse(uploadResponse.rawHeaders);
if (uploadResponse.status >= 400) {
throw new Error(`Error uploading file: HTTP ${uploadResponse.status}`);
}
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);
}
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 destinationPath = (path, file) => {
Expand Down

0 comments on commit 5934a42

Please sign in to comment.