Skip to content

Commit

Permalink
refactor: make the width of starting and ending indicator of upload l…
Browse files Browse the repository at this point in the history
…og responsive
  • Loading branch information
vcheckzen committed Sep 21, 2024
1 parent 8cc8a86 commit 11c769e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
8 changes: 4 additions & 4 deletions back-end-cf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function handleRequest(request) {
);
}
if (queryString) querySplited = queryString.split('=');
if ((querySplited && querySplited[0] === 'file')) {
if (querySplited && querySplited[0] === 'file') {
const file = querySplited[1];
const fileName = file.split('/').pop();
if (fileName.toLowerCase() === PASSWD_FILENAME.toLowerCase())
Expand Down Expand Up @@ -278,9 +278,9 @@ async function uploadFiles(fileJsonList) {
requests: fileList.map((file, index) => ({
id: `${index + 1}`,
method: file['fileSize'] ? 'POST' : 'PUT',
url: `/me/drive/root:${encodeURI(
EXPOSE_PATH + file['remotePath']
)}${file['fileSize'] ? ':/createUploadSession' : ':/content'}`,
url: `/me/drive/root:${encodeURI(EXPOSE_PATH + file['remotePath'])}${
file['fileSize'] ? ':/createUploadSession' : ':/content'
}`,
headers: { 'Content-Type': 'application/json' },
body: {},
})),
Expand Down
72 changes: 40 additions & 32 deletions front-end/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@
padding: 1em;
background-color: rgb(245, 245, 245);
overflow: auto;
white-space: nowrap;
display: grid;
}

.file-upload-progress .log-separator {
background-image: url("data:image/svg+xml;utf8,<svg width='9' height='9' xmlns='http://www.w3.org/2000/svg' version='1.1'><text y='10'>=</text></svg>");
padding: 5px 0;
}

pre * {
Expand Down Expand Up @@ -1642,7 +1649,7 @@
);
const odPath = window.backFordwardCache.current;
const upFileList = fileInput.map((f) => ({
remotePath: `${odPath}/${f.webkitRelativePath || f.name}`,
remotePath: `${odPath}/${relativePath(f)}`,
fileSize: f.size ? 1 : 0,
}));

Expand All @@ -1659,7 +1666,7 @@
function sendPage(pageIndex = 0) {
if (pageIndex >= paginatedUpFileList.length) {
window.fileCache.delete(window.backFordwardCache.current);
createUploadLogLine().innerHTML = '='.repeat(70);
createUploadLogLine().className = 'log-separator';
return;
}

Expand All @@ -1672,27 +1679,27 @@
(response) => {
const uploadLinks = JSON.parse(response).files;
Promise.all(
uploadLinks
.filter((link) => link.uploadUrl)
.map(({ remotePath, uploadUrl }) =>
uploadFileWithProgress(
fileInput.find(
(f) =>
`${odPath}/${f.webkitRelativePath || f.name}` ===
remotePath
),
uploadUrl
)
uploadLinks.map(({ remotePath, uploadUrl }) =>
uploadFileWithProgress(
fileInput.find(
(f) => `${odPath}/${relativePath(f)}` === remotePath
),
uploadUrl
)
)
).finally(() => sendPage(pageIndex + 1));
}
);
}

createUploadLogLine().innerHTML = '='.repeat(70);
createUploadLogLine().className = 'log-separator';
document.querySelector('.file-upload-progress').dataset.hidden = '0';
sendPage(0);

function relativePath(file) {
return `${file.webkitRelativePath || file.name}`;
}

function createUploadLogLine() {
const progressDiv = document.querySelector('.file-upload-progress');
const progressText = document.createElement('div');
Expand All @@ -1709,23 +1716,22 @@
let start = 0;

const progressText = createUploadLogLine();
while (start < fileSize) {
try {
await uploadChunk(start);
start = Math.min(start + chunkSize, fileSize);
} catch (error) {
if (!(await handleUploadError(error))) {
progressText.innerHTML += ' <mark>Upload failed!</mark>';
return;
}
}
}

if (fileSize === 0) {
progressText.innerHTML = `${file.name}: Upload completed!`;
updateTotalProgress();
} else {
progressText.innerHTML += ' Upload completed!';
while (start < fileSize) {
try {
await uploadChunk(start);
start = Math.min(start + chunkSize, fileSize);
} catch (error) {
if (!(await handleUploadError(error))) {
progressText.innerHTML += ' <mark>Upload failed!</mark>';
return;
}
}
}
}
progressText.innerHTML += ' Upload completed!';

async function uploadChunk(start) {
const xhr = new XMLHttpRequest();
Expand Down Expand Up @@ -1791,15 +1797,17 @@
}

function updateTotalProgress() {
const percentComplete = Math.round(
(uploadedBytes / fileSize) * 100
);
const percentComplete = fileSize
? Math.round((uploadedBytes / fileSize) * 100)
: 100;
const progressBar =
'[' +
'='.repeat(percentComplete / 5) +
' '.repeat(20 - percentComplete / 5) +
']';
progressText.textContent = `${file.name}: ${progressBar} ${percentComplete}%`;
progressText.textContent = `${relativePath(
file
)}: ${progressBar} ${percentComplete}%`;
}
}
}
Expand Down

0 comments on commit 11c769e

Please sign in to comment.