mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
refactor(control): remove downloads from tasks
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
// TODO:
|
||||||
|
// 1. Download von tasks trennen
|
||||||
|
// 2. Sync hinzufügen
|
||||||
|
|
||||||
import { db } from './database';
|
import { db } from './database';
|
||||||
import { get_display_by_id } from './stores/displays';
|
import { get_display_by_id } from './stores/displays';
|
||||||
import {
|
import {
|
||||||
@@ -12,7 +16,8 @@ import {
|
|||||||
get_file_primary_key,
|
get_file_primary_key,
|
||||||
type FileOnDisplay,
|
type FileOnDisplay,
|
||||||
type FileTransferTask,
|
type FileTransferTask,
|
||||||
type Inode
|
type Inode,
|
||||||
|
type ShortDisplay
|
||||||
} from './types';
|
} from './types';
|
||||||
import { get_sanitized_file_url, make_valid_name } from './utils';
|
import { get_sanitized_file_url, make_valid_name } from './utils';
|
||||||
|
|
||||||
@@ -84,39 +89,76 @@ export async function add_upload(
|
|||||||
await start_task_processing();
|
await start_task_processing();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function add_download(selected_file_id: string) {
|
export async function add_sync(selected_file_id: string, selected_display_ids: string[]) {
|
||||||
const active_displays = await db.displays.toArray(); //TODO: where('status').equals('app_online').toArray();
|
const file_data = await find_file_data_on_active_selected_display(
|
||||||
const active_display_ids = active_displays.map((e) => e.id);
|
selected_file_id,
|
||||||
|
selected_display_ids
|
||||||
const file_on_display = await db.files_on_display
|
);
|
||||||
.where('file_primary_key')
|
if (!file_data) return;
|
||||||
.equals(selected_file_id)
|
|
||||||
.filter((e) => active_display_ids.includes(e.display_id))
|
|
||||||
.first();
|
|
||||||
if (!file_on_display) return;
|
|
||||||
|
|
||||||
const display_ip = active_displays.find((e) => e.id === file_on_display.display_id)?.ip;
|
|
||||||
if (!display_ip) return;
|
|
||||||
const file = await get_file_by_id(selected_file_id);
|
|
||||||
if (!file) return;
|
|
||||||
|
|
||||||
tasks.push({
|
tasks.push({
|
||||||
data: {
|
data: {
|
||||||
type: 'download'
|
type: 'sync',
|
||||||
|
destination_displays: file_data.short_displays_without_file
|
||||||
},
|
},
|
||||||
display: {
|
display: file_data.short_display_with_file,
|
||||||
id: file_on_display.display_id,
|
path: file_data.file.path,
|
||||||
ip: display_ip
|
file_name: file_data.file.name,
|
||||||
},
|
|
||||||
path: file.path,
|
|
||||||
file_name: file.name,
|
|
||||||
file_primary_key: selected_file_id,
|
file_primary_key: selected_file_id,
|
||||||
bytes_total: file.size
|
bytes_total: file_data.file.size
|
||||||
});
|
});
|
||||||
|
|
||||||
await start_task_processing();
|
await start_task_processing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function find_file_data_on_active_selected_display(
|
||||||
|
selected_file_id: string,
|
||||||
|
selected_display_ids: string[]
|
||||||
|
): Promise<{
|
||||||
|
file_on_display: FileOnDisplay;
|
||||||
|
file: Inode;
|
||||||
|
short_display_with_file: ShortDisplay;
|
||||||
|
short_displays_without_file: ShortDisplay[];
|
||||||
|
} | null> {
|
||||||
|
const active_selected_displays = await db.displays
|
||||||
|
.where('id')
|
||||||
|
.anyOf(selected_display_ids)
|
||||||
|
// .filter((d) => d.status === 'app_online') DEBUG
|
||||||
|
.toArray();
|
||||||
|
const active_selected_display_ids = active_selected_displays.map((d) => d.id);
|
||||||
|
|
||||||
|
const fods = await db.files_on_display
|
||||||
|
.where('file_primary_key')
|
||||||
|
.equals(selected_file_id)
|
||||||
|
.filter((e) => active_selected_display_ids.includes(e.display_id))
|
||||||
|
.toArray();
|
||||||
|
if (fods.length < 0) return null;
|
||||||
|
const fod_with_file = fods[0];
|
||||||
|
const display_ids_with_file = fods.map((f) => f.display_id);
|
||||||
|
|
||||||
|
const ip = active_selected_displays.find((e) => e.id === fod_with_file.display_id)?.ip;
|
||||||
|
if (!ip) return null;
|
||||||
|
|
||||||
|
const short_displays_without_file: ShortDisplay[] = selected_display_ids
|
||||||
|
.filter((d) => !display_ids_with_file.includes(d))
|
||||||
|
.map((id) => {
|
||||||
|
const ip = active_selected_displays.find((d) => d.id === id)?.ip;
|
||||||
|
if (!ip) return null;
|
||||||
|
return { id, ip };
|
||||||
|
})
|
||||||
|
.filter((sd) => !!sd);
|
||||||
|
|
||||||
|
const inode = await get_file_by_id(selected_file_id);
|
||||||
|
if (!inode) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
file_on_display: fod_with_file,
|
||||||
|
file: inode,
|
||||||
|
short_display_with_file: { id: fod_with_file.display_id, ip },
|
||||||
|
short_displays_without_file
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function generate_valid_file_name(original_file_name: string, used_file_names: string[]): string {
|
function generate_valid_file_name(original_file_name: string, used_file_names: string[]): string {
|
||||||
const regex = /\s\((\d{1,3})\)$/;
|
const regex = /\s\((\d{1,3})\)$/;
|
||||||
|
|
||||||
@@ -202,23 +244,29 @@ async function upload(task: FileTransferTask): Promise<void> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function download(task: FileTransferTask): Promise<void> {
|
export async function download_file(selected_file_id: string, selected_display_ids: string[]) {
|
||||||
if (task.data.type !== 'download') return;
|
const file_data = await find_file_data_on_active_selected_display(
|
||||||
|
selected_file_id,
|
||||||
|
selected_display_ids
|
||||||
|
);
|
||||||
|
if (!file_data || file_data.file.type === 'inode/directory') return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Update File_on_display.loading_data -> start
|
const url = `http://${file_data.short_display_with_file.ip}:1323/api${get_sanitized_file_url(file_data.file.path + file_data.file.name)}`;
|
||||||
const url = `http://${task.display.ip}:1323/api${get_sanitized_file_url(task.path + task.file_name)}`;
|
|
||||||
|
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
a.download = task.file_name; // Hint für den Browser (Server-Header ist trotzdem besser)
|
a.download = file_data.file.name; // Hint für den Browser (Server-Header ist trotzdem besser)
|
||||||
a.rel = 'noopener';
|
a.rel = 'noopener';
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
a.remove();
|
a.remove();
|
||||||
// TODO: Update File_on_display.loading_data -> finish
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
show_error(task, String(e));
|
notifications.push(
|
||||||
|
'error',
|
||||||
|
'Fehler beim Dateidownload',
|
||||||
|
`Datei: "${file_data.file.name}", Display-IP: ${file_data.short_display_with_file.ip}\nFehler: ${e}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,16 +281,11 @@ async function start_task_loop() {
|
|||||||
while (tasks.length > 0) {
|
while (tasks.length > 0) {
|
||||||
console.log('NOCH', tasks.length, 'ZU TUN');
|
console.log('NOCH', tasks.length, 'ZU TUN');
|
||||||
const current_task = tasks[0];
|
const current_task = tasks[0];
|
||||||
switch (current_task.data.type) {
|
if (current_task.data.type === 'upload') {
|
||||||
case 'upload':
|
|
||||||
await upload(current_task);
|
await upload(current_task);
|
||||||
break;
|
|
||||||
case 'download':
|
|
||||||
await download(current_task);
|
|
||||||
break;
|
|
||||||
case 'sync':
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
// else if (current_task.data.type === 'sync') {
|
||||||
|
// }
|
||||||
tasks.shift(); // Remove current_task from tasks
|
tasks.shift(); // Remove current_task from tasks
|
||||||
}
|
}
|
||||||
is_processing = false;
|
is_processing = false;
|
||||||
@@ -264,7 +307,7 @@ async function show_error(task: FileTransferTask, error: ProgressEvent | string)
|
|||||||
const task_data = task.data;
|
const task_data = task.data;
|
||||||
notifications.push(
|
notifications.push(
|
||||||
'error',
|
'error',
|
||||||
`Fehler beim ${task_data.type === 'upload' ? 'Dateiupload' : task_data.type === 'download' ? 'Dateidownload' : 'Sychronisieren von Dateien'}`,
|
`Fehler beim ${task_data.type === 'upload' ? 'Dateiupload' : 'Sychronisieren von Dateien'}`,
|
||||||
`Datei: "${task.file_name}", Display-IP: ${task.display.ip}\nFehler: ${error}`
|
`Datei: "${task.file_name}", Display-IP: ${task.display.ip}\nFehler: ${error}`
|
||||||
);
|
);
|
||||||
if (task_data.type === 'upload') {
|
if (task_data.type === 'upload') {
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ export type FileTransferTask = {
|
|||||||
export type FileTransferTaskData = {
|
export type FileTransferTaskData = {
|
||||||
type: 'upload',
|
type: 'upload',
|
||||||
file: File,
|
file: File,
|
||||||
} | {
|
|
||||||
type: 'download',
|
|
||||||
} | {
|
} | {
|
||||||
type: "sync",
|
type: "sync",
|
||||||
destination_displays: ShortDisplay[],
|
destination_displays: ShortDisplay[],
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
import { delete_files, rename_file } from '$lib/ts/api_handler';
|
import { delete_files, rename_file } from '$lib/ts/api_handler';
|
||||||
import HighlightedText from '$lib/components/HighlightedText.svelte';
|
import HighlightedText from '$lib/components/HighlightedText.svelte';
|
||||||
import { liveQuery, type Observable } from 'dexie';
|
import { liveQuery, type Observable } from 'dexie';
|
||||||
import { add_download, add_upload } from '$lib/ts/file_transfer_handler';
|
import { download_file, add_upload } from '$lib/ts/file_transfer_handler';
|
||||||
|
|
||||||
let current_name: string = $state('');
|
let current_name: string = $state('');
|
||||||
let current_valid: boolean = $state(false);
|
let current_valid: boolean = $state(false);
|
||||||
@@ -320,7 +320,7 @@
|
|||||||
<Button
|
<Button
|
||||||
title="Ausgewählte Datei herunterladen"
|
title="Ausgewählte Datei herunterladen"
|
||||||
className="px-3 flex"
|
className="px-3 flex"
|
||||||
click_function={() => (add_download($selected_file_ids[0]))}
|
click_function={() => (download_file($selected_file_ids[0], $selected_display_ids))}
|
||||||
disabled={!$one_file_selected}><Download /></Button
|
disabled={!$one_file_selected}><Download /></Button
|
||||||
>
|
>
|
||||||
<div class="border border-stone-700 my-1"></div>
|
<div class="border border-stone-700 my-1"></div>
|
||||||
|
|||||||
Reference in New Issue
Block a user