feat: sorting by nativity

This commit is contained in:
not-nullptr 2025-04-14 23:54:20 +01:00
parent ea53fc7b9b
commit 4d2378e7ef
4 changed files with 39 additions and 16 deletions

View File

@ -1,3 +1,4 @@
import type { Converter, FormatInfo } from "./converter.svelte";
import { FFmpegConverter } from "./ffmpeg.svelte"; import { FFmpegConverter } from "./ffmpeg.svelte";
import { PandocConverter } from "./pandoc.svelte"; import { PandocConverter } from "./pandoc.svelte";
import { VertdConverter } from "./vertd.svelte"; import { VertdConverter } from "./vertd.svelte";
@ -9,3 +10,15 @@ export const converters = [
new VertdConverter(), new VertdConverter(),
new PandocConverter(), new PandocConverter(),
]; ];
export const byNative = (format: string) => {
return (a: Converter, b: Converter) => {
const aFormat = a.supportedFormats.find((f) => f.name === format);
const bFormat = b.supportedFormats.find((f) => f.name === format);
if (aFormat && bFormat) {
return aFormat.isNative ? -1 : 1;
}
return 0;
};
};

View File

@ -1,5 +1,5 @@
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import { converters } from "$lib/converters"; import { byNative, converters } from "$lib/converters";
import { error, log } from "$lib/logger"; import { error, log } from "$lib/logger";
import { VertFile } from "$lib/types"; import { VertFile } from "$lib/types";
import { parseBlob, selectCover } from "music-metadata"; import { parseBlob, selectCover } from "music-metadata";
@ -32,11 +32,13 @@ class Files {
this.thumbnailQueue.add(async () => { this.thumbnailQueue.add(async () => {
const isAudio = converters const isAudio = converters
.find((c) => c.name === "ffmpeg") .find((c) => c.name === "ffmpeg")
?.formatStrings() ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
?.includes(file.from.toLowerCase()); ?.includes(file.from.toLowerCase());
const isVideo = converters const isVideo = converters
.find((c) => c.name === "vertd") .find((c) => c.name === "vertd")
?.formatStrings() ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
?.includes(file.from.toLowerCase()); ?.includes(file.from.toLowerCase());
try { try {
@ -120,11 +122,11 @@ class Files {
log(["files"], `no extension found for ${file.name}`); log(["files"], `no extension found for ${file.name}`);
return; return;
} }
const converter = converters.find((c) => const converter = converters
c .sort(byNative(format))
.formatStrings() .find((converter) =>
.includes(format || ".somenonexistentextension"), converter.formatStrings().includes(format),
); );
if (!converter) { if (!converter) {
log(["files"], `no converter found for ${file.name}`); log(["files"], `no converter found for ${file.name}`);
this.files.push(new VertFile(file, format)); this.files.push(new VertFile(file, format));

View File

@ -1,4 +1,4 @@
import { converters } from "$lib/converters"; import { byNative, converters } from "$lib/converters";
import type { Converter } from "$lib/converters/converter.svelte"; import type { Converter } from "$lib/converters/converter.svelte";
import { error } from "$lib/logger"; import { error } from "$lib/logger";
import { addToast } from "$lib/store/ToastProvider"; import { addToast } from "$lib/store/ToastProvider";
@ -27,9 +27,13 @@ export class VertFile {
public converters: Converter[] = []; public converters: Converter[] = [];
public findConverters(supportedFormats: string[] = [this.from]) { public findConverters(supportedFormats: string[] = [this.from]) {
const converter = this.converters.filter((converter) => const converter = this.converters
converter.formatStrings().map((f) => supportedFormats.includes(f)), .filter((converter) =>
); converter
.formatStrings()
.map((f) => supportedFormats.includes(f)),
)
.sort(byNative(this.from));
return converter; return converter;
} }

View File

@ -94,19 +94,23 @@
)} )}
{@const isAudio = converters {@const isAudio = converters
.find((c) => c.name === "ffmpeg") .find((c) => c.name === "ffmpeg")
?.formatStrings((f) => f.fromSupported) ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)} .includes(file.from)}
{@const isVideo = converters {@const isVideo = converters
.find((c) => c.name === "vertd") .find((c) => c.name === "vertd")
?.formatStrings((f) => f.fromSupported) ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)} .includes(file.from)}
{@const isImage = converters {@const isImage = converters
.find((c) => c.name === "libvips") .find((c) => c.name === "libvips")
?.formatStrings((f) => f.fromSupported) ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)} .includes(file.from)}
{@const isDocument = converters {@const isDocument = converters
.find((c) => c.name === "pandoc") .find((c) => c.name === "pandoc")
?.formatStrings((f) => f.fromSupported) ?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)} .includes(file.from)}
<Panel class="p-5 flex flex-col min-w-0 gap-4 relative"> <Panel class="p-5 flex flex-col min-w-0 gap-4 relative">
<div class="flex-shrink-0 h-8 w-full flex items-center gap-2"> <div class="flex-shrink-0 h-8 w-full flex items-center gap-2">