Refactor authentication for DICOM endpoints to use TokenAuth and enhance error handling in upload process
This commit is contained in:
@@ -128,14 +128,31 @@
|
||||
file._blake3_hash = hash; // Store for later use if needed
|
||||
}
|
||||
// Call the API
|
||||
return fetch("{% url 'api-1:check_images_hashes' %}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": "{{ csrf_token }}"
|
||||
},
|
||||
body: JSON.stringify(hashes)
|
||||
}).then(res => res.json());
|
||||
try {
|
||||
const res = await fetch("{% url 'api-1:check_images_hashes' %}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": "{{ csrf_token }}"
|
||||
},
|
||||
body: JSON.stringify(hashes)
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
console.error('Hash check failed', res.status, text);
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return await res.json();
|
||||
} catch (e) {
|
||||
const text = await res.text();
|
||||
console.error('Non-JSON response from hash check', text);
|
||||
return {};
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Network error while checking hashes', e);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -161,14 +178,35 @@
|
||||
}
|
||||
|
||||
$dupProgress.html('<span style="color:#0d6efd;">Checking hashes with server...</span>');
|
||||
const result = await fetch("{% url 'api-1:check_images_hashes' %}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": "{{ csrf_token }}"
|
||||
},
|
||||
body: JSON.stringify(hashes)
|
||||
}).then(res => res.json());
|
||||
let result = {};
|
||||
try {
|
||||
const res = await fetch("{% url 'api-1:check_images_hashes' %}", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-CSRFToken": "{{ csrf_token }}"
|
||||
},
|
||||
body: JSON.stringify(hashes)
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
console.error('Hash check failed', res.status, text);
|
||||
toastr.error('Error checking duplicates with server');
|
||||
result = {};
|
||||
} else {
|
||||
try {
|
||||
result = await res.json();
|
||||
} catch (e) {
|
||||
const text = await res.text();
|
||||
console.error('Non-JSON response from hash check', text);
|
||||
result = {};
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Network error while checking hashes', e);
|
||||
toastr.error('Network error while checking duplicates');
|
||||
result = {};
|
||||
}
|
||||
|
||||
// Build a map from hash to file for quick lookup
|
||||
const hashToFile = {};
|
||||
@@ -249,7 +287,8 @@
|
||||
window.upload_results = {
|
||||
"uploaded": [],
|
||||
"duplicates": [],
|
||||
"failed": []
|
||||
"failed": [],
|
||||
"duplicate_series": []
|
||||
};
|
||||
|
||||
chunked_files = [...chunks(window.to_upload, 10)];
|
||||
@@ -279,27 +318,67 @@
|
||||
window.upload_count += 1;
|
||||
if (xhr.status === 200) {
|
||||
// Handle successful response from the server
|
||||
window.upload_results["uploaded"].push(...JSON.parse(xhr.response)["uploaded"]);
|
||||
window.upload_results["duplicates"].push(...JSON.parse(xhr.response)["duplicates"]);
|
||||
window.upload_results["failed"].push(...JSON.parse(xhr.response)["failed"]);
|
||||
|
||||
for (let i = 0; i < JSON.parse(xhr.response)["uploaded"].length; i++) {
|
||||
let item = document.createElement("li");
|
||||
item.textContent = JSON.parse(xhr.response)["uploaded"][i];
|
||||
$("#uploaded-files").append(item);
|
||||
let res = null;
|
||||
try {
|
||||
res = JSON.parse(xhr.responseText || xhr.response);
|
||||
} catch (e) {
|
||||
const text = xhr.responseText || xhr.response;
|
||||
console.error('Upload response not JSON', text);
|
||||
alert('Upload failed: server returned non-JSON response');
|
||||
res = null;
|
||||
}
|
||||
|
||||
for (let i = 0; i < JSON.parse(xhr.response)["duplicates"].length; i++) {
|
||||
let item = document.createElement("li");
|
||||
item.textContent = JSON.parse(xhr.response)["duplicates"][i];
|
||||
$("#duplicate-files").append(item);
|
||||
}
|
||||
window.duplicate_series.add(...JSON.parse(xhr.response)["duplicate_series"]);
|
||||
if (res) {
|
||||
// Append arrays of tuples [filename, hash]
|
||||
if (res.uploaded && res.uploaded.length) {
|
||||
window.upload_results.uploaded.push(...res.uploaded);
|
||||
res.uploaded.forEach(u => {
|
||||
const item = document.createElement('li');
|
||||
if (Array.isArray(u) && u.length >= 1) {
|
||||
const name = u[0];
|
||||
const hash = u[1] || '';
|
||||
item.innerHTML = `${name}` + (hash ? ` <small class="text-muted">(${hash})</small>` : '');
|
||||
} else {
|
||||
item.textContent = u;
|
||||
}
|
||||
$('#uploaded-files').append(item);
|
||||
});
|
||||
}
|
||||
|
||||
for (let i = 0; i < JSON.parse(xhr.response)["failed"].length; i++) {
|
||||
let item = document.createElement("li");
|
||||
item.textContent = JSON.parse(xhr.response)["failed"][i];
|
||||
$("#failed-files").append(item);
|
||||
if (res.duplicates && res.duplicates.length) {
|
||||
window.upload_results.duplicates.push(...res.duplicates);
|
||||
res.duplicates.forEach(d => {
|
||||
const item = document.createElement('li');
|
||||
if (Array.isArray(d) && d.length >= 1) {
|
||||
const name = d[0];
|
||||
const hash = d[1] || '';
|
||||
item.innerHTML = `${name}` + (hash ? ` <small class="text-muted">(${hash})</small>` : '');
|
||||
} else {
|
||||
item.textContent = d;
|
||||
}
|
||||
$('#duplicate-files').append(item);
|
||||
});
|
||||
}
|
||||
|
||||
if (res.failed && res.failed.length) {
|
||||
window.upload_results.failed.push(...res.failed);
|
||||
res.failed.forEach(f => {
|
||||
const item = document.createElement('li');
|
||||
item.textContent = f;
|
||||
$('#failed-files').append(item);
|
||||
});
|
||||
}
|
||||
|
||||
// duplicate_series may be an array of urls
|
||||
if (res.duplicate_series && res.duplicate_series.length) {
|
||||
res.duplicate_series.forEach(url => {
|
||||
if (window.duplicate_series && typeof window.duplicate_series.add === 'function') {
|
||||
window.duplicate_series.add(url);
|
||||
} else {
|
||||
window.upload_results.duplicate_series.push(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Handle error response from the server
|
||||
|
||||
Reference in New Issue
Block a user