Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Oct 11, 2024
2 parents 835c425 + 5995375 commit ec08bd9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect, test } from "vitest";

import { fileUrlToAssetUrl } from "./utils";

test("fileUrlToAssetUrl", () => {
expect(
fileUrlToAssetUrl("https://example.com/image.png?X-Amz-Expires=1", ""),
).toBe("/static/image.webp");
expect(
fileUrlToAssetUrl(
"https://example.com/image.png?X-Amz-Expires=1",
"blockId",
),
).toBe("/static/blockId.webp");

expect(
fileUrlToAssetUrl("https://example.com/foobar?X-Amz-Expires=1", "blockId"),
).toBe("/static/blockId.webp");
expect(
fileUrlToAssetUrl("https://example.com/foobar?X-Amz-Expires=1", ""),
).toBe("/static/foobar");

expect(
fileUrlToAssetUrl(
"https://images.unsplash.com/photo-1514519273132-6a1abd48302c?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb&w=6000",
"blockId",
),
).toBe("/static/blockId.webp");
expect(
fileUrlToAssetUrl(
"https://images.unsplash.com/photo-1514519273132-6a1abd48302c?ixlib=rb-4.0.3&q=85&fm=jpg&crop=entropy&cs=srgb&w=6000",
"",
),
).toBe("/static/photo-1514519273132-6a1abd48302c");

expect(fileUrlToAssetUrl("https://example.com", "blockId")).toBeUndefined();
expect(fileUrlToAssetUrl("https://example.com", "")).toBeUndefined();
expect(fileUrlToAssetUrl(undefined, "blockId")).toBeUndefined();
expect(fileUrlToAssetUrl(undefined, "")).toBeUndefined();
});
8 changes: 7 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export function fileUrlToAssetUrl(
if (!imageUrl) return undefined; // should not download

const url = new URL(imageUrl);
if (!url.searchParams.has("X-Amz-Expires")) return undefined; // should not download
if (!url.searchParams.has("X-Amz-Expires") && !isUnsplash(url)) {
return undefined; // should not download
}

const filename = url.pathname.split("/").at(-1);
if (!filename) return imageUrl;
Expand All @@ -97,4 +99,8 @@ export function fileUrlToAssetUrl(
return newUrl;
}

function isUnsplash(url: URL): boolean {
return url.hostname === "images.unsplash.com";
}

export const assetsDir = "/static";

0 comments on commit ec08bd9

Please sign in to comment.