refactor(control): improve db update of upload percentage

This commit is contained in:
E44
2026-01-05 18:29:05 +01:00
parent b775fae9c0
commit 12e500e988
@@ -89,11 +89,11 @@ function generate_valid_file_name(original_file_name: string, used_file_names: s
} else { } else {
const match = name_without_extension.match(regex); const match = name_without_extension.match(regex);
const current_number: number = match ? Number(match[1]) : 0; const current_number: number = match ? Number(match[1]) : 0;
console.log("current", current_number) console.log('current', current_number);
name_without_extension = name_without_extension.replace(regex, ` (${current_number + 1})`); name_without_extension = name_without_extension.replace(regex, ` (${current_number + 1})`);
} }
name = name_without_extension + extension; name = name_without_extension + extension;
console.log(name) console.log(name);
} }
return name; return name;
} }
@@ -121,28 +121,27 @@ async function start_task_loop() {
} }
} }
async function upload(task: FileTransferTask): Promise<void> { async function upload(task: FileTransferTask): Promise<void> {
if (task.type !== 'upload' || !task.file) return; if (task.type !== 'upload' || !task.file) return;
await db.files_on_display await db.files_on_display.update([task.display_id, task.file_primary_key], { percentage: 1 });
.where('[display_id+file_primary_key]')
.equals([task.display_id, task.file_primary_key])
.modify({ percentage: 1 });
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('POST', `http://${task.display_ip}:1323/api${get_sanitized_file_url(task.path + task.file_name)}`, true); xhr.open(
'POST',
`http://${task.display_ip}:1323/api${get_sanitized_file_url(task.path + task.file_name)}`,
true
);
xhr.setRequestHeader('content-type', 'application/octet-stream'); xhr.setRequestHeader('content-type', 'application/octet-stream');
xhr.upload.onprogress = (e) => { xhr.upload.onprogress = (e) => {
const total = e.lengthComputable ? e.total : task.bytes_total; const total = e.lengthComputable ? e.total : task.bytes_total;
const current_percentage = total > 0 ? Math.round((e.loaded / total) * 100) : 1; const current_percentage = total > 0 ? Math.round((e.loaded / total) * 100) : 1;
const apply = async () => { const apply = async () => {
await db.files_on_display await db.files_on_display.update([task.display_id, task.file_primary_key], {
.where('[display_id+file_primary_key]') percentage: current_percentage
.equals([task.display_id, task.file_primary_key]) });
.modify({ percentage: current_percentage });
}; };
apply(); apply();
}; };
@@ -155,10 +154,10 @@ async function upload(task: FileTransferTask): Promise<void> {
xhr.onreadystatechange = async () => { xhr.onreadystatechange = async () => {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if (xhr.status == 200) { if (xhr.status == 200) {
await db.files_on_display await db.files_on_display.update([task.display_id, task.file_primary_key], {
.where('[display_id+file_primary_key]') percentage: 100,
.equals([task.display_id, task.file_primary_key]) is_loading: false
.modify({ percentage: 100, is_loading: false }); });
} else { } else {
await show_error('upload', `HTTP ${xhr.status}`, task); await show_error('upload', `HTTP ${xhr.status}`, task);
} }