Skip to content

Commit

Permalink
🩹 back: tolerate extraneous /s in env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlinagora authored and shepilov committed Nov 20, 2024
1 parent 00f819f commit dcadfa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<EmailPusherAPI>
Expand Down Expand Up @@ -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}`);
Expand Down
17 changes: 17 additions & 0 deletions tdrive/backend/node/src/utils/urls.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit dcadfa4

Please sign in to comment.