From dcadfa4231967c5ba166b15cc75f298b9ea6bd13 Mon Sep 17 00:00:00 2001 From: Eric Doughty-Papassideris Date: Sat, 16 Nov 2024 06:08:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20back:=20tolerate=20extraneous=20?= =?UTF-8?q?`/`s=20in=20env=20vars?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/services/email-pusher/index.ts | 10 +++++++++- tdrive/backend/node/src/utils/urls.ts | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tdrive/backend/node/src/utils/urls.ts diff --git a/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts b/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts index a47c6256..81a9ffe7 100644 --- a/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts +++ b/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts @@ -16,6 +16,7 @@ import path from "path"; import { existsSync } from "fs"; import axios from "axios"; import short, { Translator } from "short-uuid"; +import { joinURL } from "../../../../utils/urls"; export default class EmailPusherClass extends TdriveService @@ -86,7 +87,14 @@ export default class EmailPusherClass const encodedCompanyId = translator.fromUUID(data.notifications[0].item.company_id); const encodedItemId = translator.fromUUID(data.notifications[0].item.id); const previewType = data.notifications[0].item.is_directory ? "d" : "preview"; - const encodedUrl = `${this.platformUrl}/client/${encodedCompanyId}/v/shared_with_me/${previewType}/${encodedItemId}`; + const encodedUrl = joinURL([ + this.platformUrl, + "client", + encodedCompanyId, + "v/shared_with_me", + previewType, + encodedItemId, + ]); if (!existsSync(templatePath)) { throw Error(`template not found: ${templatePath}`); diff --git a/tdrive/backend/node/src/utils/urls.ts b/tdrive/backend/node/src/utils/urls.ts new file mode 100644 index 00000000..6c6f0895 --- /dev/null +++ b/tdrive/backend/node/src/utils/urls.ts @@ -0,0 +1,17 @@ +/** Suitable type for query arguments */ +export type QueryParams = { [key: string]: string | number }; + +/** + * Compose a URL removing and adding slashes and query parameters as warranted. + * Does not encode paths. + */ +export function joinURL(path: string[], params?: QueryParams) { + let joinedPath = path.map(x => x.replace(/(?:^\/+)|(?:\/+$)/g, "")).join("/"); + if (path[path.length - 1].endsWith("/")) joinedPath += "/"; + const paramEntries = Object.entries(params || {}).filter( + ([, value]) => value !== undefined && value !== null, + ); + if (paramEntries.length === 0) return joinedPath; + const query = paramEntries.map(p => p.map(encodeURIComponent).join("=")).join("&"); + return joinedPath + (joinedPath.indexOf("?") > -1 ? "&" : "?") + query; +}