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 { PandocConverter } from "./pandoc.svelte";
import { VertdConverter } from "./vertd.svelte";
@ -9,3 +10,15 @@ export const converters = [
new VertdConverter(),
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 { converters } from "$lib/converters";
import { byNative, converters } from "$lib/converters";
import { error, log } from "$lib/logger";
import { VertFile } from "$lib/types";
import { parseBlob, selectCover } from "music-metadata";
@ -32,11 +32,13 @@ class Files {
this.thumbnailQueue.add(async () => {
const isAudio = converters
.find((c) => c.name === "ffmpeg")
?.formatStrings()
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
?.includes(file.from.toLowerCase());
const isVideo = converters
.find((c) => c.name === "vertd")
?.formatStrings()
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
?.includes(file.from.toLowerCase());
try {
@ -120,10 +122,10 @@ class Files {
log(["files"], `no extension found for ${file.name}`);
return;
}
const converter = converters.find((c) =>
c
.formatStrings()
.includes(format || ".somenonexistentextension"),
const converter = converters
.sort(byNative(format))
.find((converter) =>
converter.formatStrings().includes(format),
);
if (!converter) {
log(["files"], `no converter found for ${file.name}`);

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

View File

@ -94,19 +94,23 @@
)}
{@const isAudio = converters
.find((c) => c.name === "ffmpeg")
?.formatStrings((f) => f.fromSupported)
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)}
{@const isVideo = converters
.find((c) => c.name === "vertd")
?.formatStrings((f) => f.fromSupported)
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)}
{@const isImage = converters
.find((c) => c.name === "libvips")
?.formatStrings((f) => f.fromSupported)
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)}
{@const isDocument = converters
.find((c) => c.name === "pandoc")
?.formatStrings((f) => f.fromSupported)
?.supportedFormats.filter((f) => f.isNative)
.map((f) => f.name)
.includes(file.from)}
<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">