Skip to content

Commit

Permalink
maybe this time
Browse files Browse the repository at this point in the history
  • Loading branch information
ItamarYuran committed Dec 8, 2024
1 parent f5a2762 commit 2e739ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
24 changes: 20 additions & 4 deletions webui/src/lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ export const defaultAPIHeaders = {
"X-Lakefs-Client": "lakefs-webui/__buildVersion",
};

export const parseRawHeaders = (rawHeaders) => {
const headersString = typeof rawHeaders === 'string' ? rawHeaders : rawHeaders.toString();
const cleanedHeadersString = headersString.trim();
const headerLines = cleanedHeadersString.split('\n');
const parsedHeaders = headerLines.reduce((acc, line) => {
let [key, ...value] = line.split(':'); // split into key and the rest of the value
key = key.trim();
value = value.join(':').trim();
if (key && value) {
acc[key.toLowerCase()] = value;
}
return acc;
}, {});
return parsedHeaders;
};

const authenticationError = "error authenticating request"

const apiRequest = async (uri, requestData = {}, additionalHeaders = {}) => {
Expand Down Expand Up @@ -670,15 +686,14 @@ export const uploadWithProgress = (url, file, method = 'POST', onProgress = null
resolve({
status: xhr.status,
body: xhr.responseText,
contentType: xhr.getResponseHeader('Content-Type'),
rawHeaders: xhr.getAllResponseHeaders(), // add raw headers
});
});
xhr.addEventListener('error', () => resolve({
xhr.addEventListener('error', () => reject({
message: 'Upload Failed',
status: xhr.status,
body: xhr.responseText,
rawHeaders: xhr.getAllResponseHeaders(),
error: new Error('Upload Failed'),
}));
xhr.addEventListener('abort', () => reject(new Error('Upload Aborted')));
xhr.open(method, url, true);
Expand Down Expand Up @@ -753,8 +768,9 @@ class Objects {
async upload(repoId, branchId, path, fileObject, onProgressFn = null) {
const query = qs({path});
const uploadUrl = `${API_ENDPOINT}/repositories/${encodeURIComponent(repoId)}/branches/${encodeURIComponent(branchId)}/objects?` + query;
const {status, body, contentType} = await uploadWithProgress(uploadUrl, fileObject, 'POST', onProgressFn)
const {status, body, rawHeaders} = await uploadWithProgress(uploadUrl, fileObject, 'POST', onProgressFn)
if (status !== 201) {
const contentType = rawHeaders ? parseRawHeaders(rawHeaders)['content-type'] : undefined;
if (contentType === "application/json" && body) {
const responseData = JSON.parse(body)
throw new Error(responseData.message)
Expand Down
42 changes: 16 additions & 26 deletions webui/src/pages/repositories/repository/objects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Alert from "react-bootstrap/Alert";
import { BsCloudArrowUp } from "react-icons/bs";

import {humanSize, Tree} from "../../../lib/components/repository/tree";
import {objects, staging, retention, repositories, imports, NotFoundError, uploadWithProgress} from "../../../lib/api";
import {objects, staging, retention, repositories, imports, NotFoundError, uploadWithProgress, parseRawHeaders} from "../../../lib/api";
import {useAPI, useAPIWithPagination} from "../../../lib/hooks/api";
import {useRefs} from "../../../lib/hooks/repo";
import {useRouter} from "../../../lib/hooks/router";
Expand Down Expand Up @@ -226,21 +226,7 @@ const ImportModal = ({config, repoId, referenceId, referenceType, path = '', onD
);
};

function parseRawHeaders(rawHeaders){
const headersString = typeof rawHeaders === 'string' ? rawHeaders : rawHeaders.toString();
const cleanedHeadersString = headersString.trim();
const headerLines = cleanedHeadersString.split('\n');
const parsedHeaders = headerLines.reduce((acc, line) => {
let [key, ...value] = line.split(':'); // split into key and the rest of the value
key = key.trim();
value = value.join(':').trim();
if (key && value) {
acc[key.toLowerCase()] = value;
}
return acc;
}, {});
return parsedHeaders
}


function extractChecksumFromResponse(parsedHeaders) {
if (parsedHeaders['content-md5']) {
Expand All @@ -263,16 +249,20 @@ 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 uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders);
if (uploadResponse.status >= 400) {
throw new Error(`Error uploading file: HTTP ${uploadResponse.status}`);
}
const parsedHeaders = parseRawHeaders(uploadResponse.rawHeaders);
const checksum = extractChecksumFromResponse(parsedHeaders);
if (checksum === null) {
throw new Error(`CORS settings error. Check documentation for more info.`);
}
await staging.link(repo.id, reference.id, fpath, getResp, checksum, file.size, file.type);
try {
const uploadResponse = await uploadWithProgress(getResp.presigned_url, file, 'PUT', onProgress, additionalHeaders);
const parsedHeaders = parseRawHeaders(uploadResponse.rawHeaders);
const checksum = extractChecksumFromResponse(parsedHeaders);
await staging.link(repo.id, reference.id, fpath, getResp, checksum, file.size, file.type);
} catch(error) {
if (error.status >= 400) {
throw new Error(`Error uploading file: HTTP ${error.status}`);
}
if (error.status === 0) {
throw new Error(`CORS settings error. Check documentation for more info.`);
}
throw error;
}
} else {
await objects.upload(repo.id, reference.id, fpath, file, onProgress);
}
Expand Down

0 comments on commit 2e739ac

Please sign in to comment.