Skip to content

armgit / VERT

Public Imported from GitHub
0

The next-generation file converter. Open source, fully local* and free forever.

fix: ensure same vertd server, more url normalization

JovannMCd8733c3f320a
diff --git a/src/lib/converters/vertd/vertd.svelte.ts b/src/lib/converters/vertd/vertd.svelte.ts
index 9344a07..798922f 100644
--- a/src/lib/converters/vertd/vertd.svelte.ts
+++ b/src/lib/converters/vertd/vertd.svelte.ts
@@ -50,19 +50,20 @@ interface RouteResponseMap {
export const vertdFetch: {
<U extends keyof RouteRequestMap>(
- url: U,
+ path: U,
options: RequestInit,
body: RouteRequestMap[U],
+ baseUrl?: string,
): Promise<RouteResponseMap[U]>;
<U extends Exclude<keyof RouteResponseMap, keyof RouteRequestMap>>(
- url: U,
+ path: U,
options: RequestInit,
+ body?: undefined,
+ baseUrl?: string,
): Promise<RouteResponseMap[U]>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
-} = async (url: any, options: RequestInit, body?: any) => {
- const domain = await VertdInstance.instance.url();
-
- if (!domain) return undefined;
+} = async (url: any, options: RequestInit, body?: any, baseUrl?: string) => {
+ const domain = baseUrl ?? (await VertdInstance.instance.url());
const headers = new Headers(options.headers);
for (const [key, value] of Object.entries(getVertdCustomHeaders()))
@@ -230,8 +231,10 @@ interface UploadTask {
abort: () => void;
}
-const createUploadTask = async (file: VertFile): Promise<UploadTask> => {
- const apiUrl = await VertdInstance.instance.url();
+const createUploadTask = async (
+ file: VertFile,
+ apiUrl: string,
+): Promise<UploadTask> => {
const formData = new FormData();
formData.append("file", file.file, file.name);
const xhr = new XMLHttpRequest();
@@ -416,6 +419,7 @@ export class VertdConverter extends Converter {
public async getAvailableSettings(
input: VertFile,
+ baseUrl?: string,
): Promise<SettingCategories> {
// video - bitrate, fps, resolution, trim, crop, rotate, flip/flop, audio settings?
@@ -449,9 +453,14 @@ export class VertdConverter extends Converter {
// get codecs for this format from vertd
try {
const targetFormat = input.to.replace(/^\./, "");
- const codecsJson = await vertdFetch(`/api/codecs/${targetFormat}`, {
- method: "GET",
- });
+ const codecsJson = await vertdFetch(
+ `/api/codecs/${targetFormat}`,
+ {
+ method: "GET",
+ },
+ undefined,
+ baseUrl,
+ );
const previousCodecs = JSON.stringify(this.codecs);
const newCodecs = JSON.stringify(codecsJson);
@@ -603,9 +612,10 @@ export class VertdConverter extends Converter {
public async getDefaultSettings(
input: VertFile,
+ baseUrl?: string,
): Promise<ConversionSettings> {
const defaults: ConversionSettings = {};
- const categories = await this.getAvailableSettings(input);
+ const categories = await this.getAvailableSettings(input, baseUrl);
Object.values(categories)
.flat()
.forEach((setting) => {
@@ -624,10 +634,14 @@ export class VertdConverter extends Converter {
if (to.startsWith(".")) to = to.slice(1);
const fileUpload = input;
+ const apiUrl = await VertdInstance.instance.url();
const conversionSettings = // vertd expects object not string json
Object.keys(settings).length > 4
? settings // user-provided settings
- : Object.assign(settings, await this.getDefaultSettings(input)); // use defaults if not provided
+ : Object.assign(
+ settings,
+ await this.getDefaultSettings(input, apiUrl),
+ ); // use defaults if not provided
let hash: string;
if (PUB_DISABLE_FAILURE_BLOCKS === "false") {
@@ -643,9 +657,7 @@ export class VertdConverter extends Converter {
}
}
- const apiUrl = await VertdInstance.instance.url();
-
- const uploadTask = await createUploadTask(fileUpload);
+ const uploadTask = await createUploadTask(fileUpload, apiUrl);
this.activeUploads.set(input.id, uploadTask);
let uploadRes: UploadResponse;
@@ -802,6 +814,8 @@ export class VertdConverter extends Converter {
{
method: "GET",
},
+ undefined,
+ apiUrl,
);
this.log(
`confirmed download for file ${input.name}`,
@@ -897,14 +911,16 @@ export class VertdConverter extends Converter {
}
public async valid(): Promise<boolean> {
- if (!(await VertdInstance.instance.url())) {
- return false;
- }
-
try {
- await vertdFetch("/api/version", {
- method: "GET",
- });
+ const apiUrl = await VertdInstance.instance.url();
+ await vertdFetch(
+ "/api/version",
+ {
+ method: "GET",
+ },
+ undefined,
+ apiUrl,
+ );
return true;
} catch (e) {
this.log(e as unknown as string);
diff --git a/src/lib/sections/settings/vertdSettings.svelte.ts b/src/lib/sections/settings/vertdSettings.svelte.ts
index 0f52673..b15b4ec 100644
--- a/src/lib/sections/settings/vertdSettings.svelte.ts
+++ b/src/lib/sections/settings/vertdSettings.svelte.ts
@@ -1,6 +1,6 @@
import { Settings } from "./index.svelte";
import { PUB_VERTD_URL } from "$env/static/public";
-import { log } from "$lib/util/logger";
+import { error, log } from "$lib/util/logger";
import { writable } from "svelte/store";
import { getVertdLimit } from "$lib/store/index.svelte";
@@ -37,6 +37,23 @@ export const getVertdCustomHeaders = (): Record<string, string> => {
export const vertdSizeLimit = writable(Number.POSITIVE_INFINITY);
+export const normalizeVertdUrl = (value: string): string => {
+ const trimmed = value.trim();
+ if (!trimmed) throw new Error("No vertd server URL configured");
+ try {
+ const url = new URL(
+ /^[a-z][a-z\d+.-]*:\/\//i.test(trimmed)
+ ? trimmed
+ : `https://${trimmed}`,
+ );
+ if (url.protocol !== "http:" && url.protocol !== "https:")
+ throw new Error("vertd server URL must use HTTP or HTTPS");
+ return url.href.replace(/\/+$/, "");
+ } catch (err) {
+ throw new Error(`Invalid vertd server URL: ${err}`);
+ }
+};
+
export class VertdInstance {
public static instance = new VertdInstance();
@@ -94,12 +111,6 @@ export class VertdInstance {
}
};
- const normalize = (url: string) => {
- let finalUrl = url;
- if (url.endsWith("/")) finalUrl = finalUrl.slice(0, -1);
- return finalUrl;
- };
-
switch (this.inner.type) {
case "auto": {
const results = await Promise.all(
@@ -127,7 +138,11 @@ export class VertdInstance {
if (fastest) return fastest.url;
// if none are reachable, fall back to custom
- return normalize(Settings.instance.settings.vertdURL);
+ if (!Settings.instance.settings.vertdURL.trim())
+ throw new Error(
+ "No reachable vertd server and no custom URL configured",
+ );
+ return normalizeVertdUrl(Settings.instance.settings.vertdURL);
}
case "eu": {
@@ -139,7 +154,7 @@ export class VertdInstance {
}
case "custom": {
- return normalize(Settings.instance.settings.vertdURL);
+ return normalizeVertdUrl(Settings.instance.settings.vertdURL);
}
}
}
@@ -171,7 +186,7 @@ export function useVertdSizeLimit() {
}
}
} catch (e) {
- log(
+ error(
["vertd"],
`failed to read vertd size limit from sessionStorage: ${e}`,
);
@@ -187,7 +202,7 @@ export function useVertdSizeLimit() {
return;
}
- const serverLimit = await getVertdLimit();
+ const serverLimit = await getVertdLimit(apiUrl);
const finalLimit = serverLimit ?? Number.POSITIVE_INFINITY;
vertdSizeLimit.set(finalLimit);
log(["vertd"], `fetched vertd size limit: ${finalLimit} bytes`);
@@ -196,7 +211,7 @@ export function useVertdSizeLimit() {
try {
sessionStorage.setItem(cacheKey, finalLimit.toString());
} catch (e) {
- log(
+ error(
["vertd"],
`failed to cache vertd size limit in sessionStorage: ${e}`,
);
@@ -204,7 +219,11 @@ export function useVertdSizeLimit() {
}
};
- void loadLimit();
+ void loadLimit().catch((err) => {
+ if (cancelled) return;
+ vertdSizeLimit.set(Number.POSITIVE_INFINITY);
+ error(["vertd"], `failed to resolve vertd size limit: ${err}`);
+ });
return () => {
cancelled = true;
diff --git a/src/lib/store/index.svelte.ts b/src/lib/store/index.svelte.ts
index 89ae486..b2f681c 100644
--- a/src/lib/store/index.svelte.ts
+++ b/src/lib/store/index.svelte.ts
@@ -686,11 +686,18 @@ export const getMaxArrayBufferSize = (): number => {
export const MAX_ARRAY_BUFFER_SIZE = getMaxArrayBufferSize();
-export const getVertdLimit = async (): Promise<number | null> => {
+export const getVertdLimit = async (
+ baseUrl?: string,
+): Promise<number | null> => {
try {
- const limit = await vertdFetch("/api/size_limit", {
- method: "GET",
- });
+ const limit = await vertdFetch(
+ "/api/size_limit",
+ {
+ method: "GET",
+ },
+ undefined,
+ baseUrl,
+ );
const parsed = Number(limit);
if (!Number.isFinite(parsed) || parsed <= 0) return null;