Skip to content

Commit

Permalink
feat: add Uploadcare support (#120)
Browse files Browse the repository at this point in the history
* feat: add uploadcare cdn

* feat: ensure the URL's always ends with an slash

* test: update test image

* test: fixed using correct uuid

* feat: add example

* fix: correct signature sign

* fix: correct signature sign
  • Loading branch information
danestves authored Feb 8, 2024
1 parent 05456eb commit 68307a4
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 3 deletions.
3 changes: 2 additions & 1 deletion data/domains.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"s7d1.scene7.com": "scene7",
"ip.keycdn.com": "keycdn",
"assets.caisy.io": "bunny",
"images.contentstack.io": "contentstack"
"images.contentstack.io": "contentstack",
"ucarecdn.com": "uploadcare"
}
3 changes: 2 additions & 1 deletion data/subdomains.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"kxcdn.com": "keycdn",
"imgeng.in": "imageengine",
"imagekit.io": "imagekit",
"cloudimg.io": "cloudimage"
"cloudimg.io": "cloudimage",
"ucarecdn.com": "uploadcare"
}
4 changes: 4 additions & 0 deletions demo/src/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@
"cloudimage": [
"Cloudimage",
"https://doc.cloudimg.io/sample.li/flat1.jpg?w=450&q=90"
],
"uploadcare": [
"Uploadcare",
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/"
]
}
2 changes: 2 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { parse as ipx } from "./transformers/ipx.ts";
import { parse as astro } from "./transformers/astro.ts";
import { parse as netlify } from "./transformers/netlify.ts";
import { parse as imagekit } from "./transformers/imagekit.ts";
import { parse as uploadcare } from "./transformers/uploadcare.ts";
import { ImageCdn, ParsedUrl, SupportedImageCdn, UrlParser } from "./types.ts";

export const parsers = {
Expand All @@ -48,6 +49,7 @@ export const parsers = {
astro,
netlify,
imagekit,
uploadcare,
};

export const cdnIsSupportedForParse = (
Expand Down
2 changes: 2 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { transform as ipx } from "./transformers/ipx.ts";
import { transform as astro } from "./transformers/astro.ts";
import { transform as netlify } from "./transformers/netlify.ts";
import { transform as imagekit } from "./transformers/imagekit.ts";
import { transform as uploadcare } from "./transformers/uploadcare.ts";
import { ImageCdn, UrlTransformer } from "./types.ts";
import { getCanonicalCdnForUrl } from "./canonical.ts";

Expand Down Expand Up @@ -49,6 +50,7 @@ export const getTransformer = (cdn: ImageCdn) => ({
astro,
netlify,
imagekit,
uploadcare,
}[cdn]);

/**
Expand Down
141 changes: 141 additions & 0 deletions src/transformers/uploadcare.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts";
import { ParsedUrl } from "../types.ts";
import { parse, transform, UploadcareParams } from "./uploadcare.ts";

const baseImage = "https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/";
const img =
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/";

const imgNoOperations = baseImage;

const imgSubdomain =
"https://private-name.example.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/";

const imgWithFilename =
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/auto/tshirt1.jpg";

Deno.test("uploadcare parser", async (t) => {
await t.step("parses a URL", () => {
const parsed = parse(img);
const expected: ParsedUrl<UploadcareParams> = {
base:
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/-/format/auto/",
cdn: "uploadcare",
params: {
host: "ucarecdn.com",
uuid: "661bd414-064c-477a-b50f-8ffd8f66aa49",
operations: {
resize: "800x550",
format: "auto",
},
filename: undefined,
},
};
assertEquals(parsed, expected);
});

await t.step("parses a URL without operations", () => {
const parsed = parse(imgNoOperations);
const expected: ParsedUrl<UploadcareParams> = {
base: `${baseImage}-/format/auto/`,
cdn: "uploadcare",
params: {
host: "ucarecdn.com",
uuid: "661bd414-064c-477a-b50f-8ffd8f66aa49",
operations: {
format: "auto",
},
filename: undefined,
},
};
assertEquals(parsed, expected);
});

await t.step("parses a URL with custom domain", () => {
const parsed = parse(imgSubdomain);
const expected: ParsedUrl<UploadcareParams> = {
base:
"https://private-name.example.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/-/format/auto/",
cdn: "uploadcare",
params: {
host: "private-name.example.com",
uuid: "661bd414-064c-477a-b50f-8ffd8f66aa49",
operations: {
resize: "800x550",
format: "auto",
},
filename: undefined,
},
};
assertEquals(parsed, expected);
});

await t.step("parses a URL with filename", () => {
const parsed = parse(imgWithFilename);
const expected: ParsedUrl<UploadcareParams> = {
base:
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/800x550/-/format/auto/tshirt1.jpg",
cdn: "uploadcare",
params: {
host: "ucarecdn.com",
uuid: "661bd414-064c-477a-b50f-8ffd8f66aa49",
operations: {
resize: "800x550",
format: "auto",
},
filename: "tshirt1.jpg",
},
};
assertEquals(parsed, expected);
});
});

Deno.test("uploadcare transformer", async (t) => {
await t.step("transforms a URL", () => {
const result = transform({
url: img,
width: 100,
height: 200,
});
assertEquals(
result?.toString(),
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/100x200/-/format/auto/",
);
});

await t.step("rounds non-integer values", () => {
const result = transform({
url: img,
width: 100.6,
height: 200.2,
});
assertEquals(
result?.toString(),
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/100.6x200.2/-/format/auto/",
);
});

await t.step("transforms a URL without parsed transforms", () => {
const result = transform({
url: imgNoOperations,
width: 100,
height: 200,
});
assertEquals(
result?.toString(),
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/format/auto/-/resize/100x200/",
);
});

await t.step("transforms a URL with path and version", () => {
const result = transform({
url: imgWithFilename,
width: 100,
height: 200,
});
assertEquals(
result?.toString(),
"https://ucarecdn.com/661bd414-064c-477a-b50f-8ffd8f66aa49/-/resize/100x200/-/format/auto/tshirt1.jpg",
);
});
});
Loading

0 comments on commit 68307a4

Please sign in to comment.