Отмена загрузки ISO была добавлена

Этот коммит содержится в:
Виктор
2026-05-07 06:21:19 +09:00
родитель a7126d4e51
Коммит 065726354e
+44 -6
Просмотреть файл
@@ -107,6 +107,8 @@ ls -lh /var/lib/virtuality/iso</pre>
const statusLine = document.getElementById('upload-status'); const statusLine = document.getElementById('upload-status');
const loadedLine = document.getElementById('upload-loaded'); const loadedLine = document.getElementById('upload-loaded');
const speedLine = document.getElementById('upload-speed'); const speedLine = document.getElementById('upload-speed');
let activeXhr = null;
let uploadActive = false;
function bytesText(bytes) { function bytesText(bytes) {
if (!bytes) return '0 MB'; if (!bytes) return '0 MB';
@@ -121,22 +123,51 @@ ls -lh /var/lib/virtuality/iso</pre>
return file && file.name && file.name.toLowerCase().endsWith('.iso'); return file && file.name && file.name.toLowerCase().endsWith('.iso');
} }
function setUploadMode(active) {
uploadActive = active;
if (active) {
button.disabled = false;
button.type = 'button';
button.textContent = 'Отменить загрузку';
button.classList.remove('primary');
button.classList.add('danger');
fileInput.disabled = true;
} else {
activeXhr = null;
button.type = 'submit';
button.textContent = 'Загрузить ISO';
button.classList.add('primary');
button.classList.remove('danger');
fileInput.disabled = false;
button.disabled = !validIsoFile(fileInput.files[0]);
}
}
fileInput.addEventListener('change', function () { fileInput.addEventListener('change', function () {
const file = fileInput.files[0]; const file = fileInput.files[0];
if (!file) { if (!file) {
hint.textContent = 'Файл ещё не выбран. В этом разделе разрешены только установочные образы .iso.'; hint.textContent = 'Файл ещё не выбран. В этом разделе разрешены только установочные образы .iso.';
button.disabled = false;
return; return;
} }
if (!validIsoFile(file)) { if (!validIsoFile(file)) {
hint.textContent = 'Неверный тип файла. Здесь можно выбрать только .iso.'; hint.textContent = 'Неверный тип файла. Здесь можно выбрать только .iso.';
fileInput.value = ''; fileInput.value = '';
button.disabled = true;
return; return;
} }
button.disabled = false;
hint.textContent = file.name + ' · ' + bytesText(file.size); hint.textContent = file.name + ' · ' + bytesText(file.size);
}); });
button.addEventListener('click', function () {
if (!uploadActive) return;
if (activeXhr) activeXhr.abort();
});
form.addEventListener('submit', function (event) { form.addEventListener('submit', function (event) {
event.preventDefault(); event.preventDefault();
if (uploadActive) return;
const file = fileInput.files[0]; const file = fileInput.files[0];
if (!validIsoFile(file)) { if (!validIsoFile(file)) {
hint.textContent = 'Выбери установочный образ .iso.'; hint.textContent = 'Выбери установочный образ .iso.';
@@ -144,12 +175,12 @@ ls -lh /var/lib/virtuality/iso</pre>
} }
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
activeXhr = xhr;
const data = new FormData(form); const data = new FormData(form);
const started = Date.now(); const started = Date.now();
box.hidden = false; box.hidden = false;
button.disabled = true; setUploadMode(true);
fileInput.disabled = true;
statusLine.textContent = 'Передача файла на сервер…'; statusLine.textContent = 'Передача файла на сервер…';
bar.style.width = '0%'; bar.style.width = '0%';
percent.textContent = '0%'; percent.textContent = '0%';
@@ -179,15 +210,22 @@ ls -lh /var/lib/virtuality/iso</pre>
window.location.href = '/iso'; window.location.href = '/iso';
} else { } else {
statusLine.textContent = 'Ошибка загрузки: HTTP ' + xhr.status; statusLine.textContent = 'Ошибка загрузки: HTTP ' + xhr.status;
button.disabled = false; setUploadMode(false);
fileInput.disabled = false;
} }
}; };
xhr.onabort = function () {
statusLine.textContent = 'Загрузка отменена пользователем.';
bar.style.width = '0%';
percent.textContent = '0%';
loadedLine.textContent = '0 MB';
speedLine.textContent = 'отменено';
setUploadMode(false);
};
xhr.onerror = function () { xhr.onerror = function () {
statusLine.textContent = 'Ошибка сети во время загрузки.'; statusLine.textContent = 'Ошибка сети во время загрузки.';
button.disabled = false; setUploadMode(false);
fileInput.disabled = false;
}; };
xhr.open('POST', form.action); xhr.open('POST', form.action);