Skip to content

Commit 1cc5d45

Browse files
authored
Calc hashsums (#48)
* Add calc_hashsums option * Changesets
1 parent 1beb7e1 commit 1cc5d45

6 files changed

Lines changed: 123 additions & 0 deletions

File tree

.changeset/fluffy-news-wear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@imgproxy/imgproxy-js-core": minor
3+
---
4+
5+
Add [Calc hashsums](https://docs.imgproxy.net/usage/getting_info#calc-hashsums) info option
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type {
2+
HashsumType,
3+
CalcHashsumsImageInfoOptionsPartial,
4+
} from "../typesImageInfo/calcHashsums";
5+
import { guardIsUndef } from "../utils";
6+
7+
const getOpt = (
8+
options: CalcHashsumsImageInfoOptionsPartial
9+
): HashsumType[] | undefined => {
10+
if ("calcHashsums" in options) {
11+
return options.calcHashsums;
12+
} else if ("chs" in options) {
13+
return options.chs;
14+
}
15+
16+
return undefined;
17+
};
18+
19+
const test = (options: CalcHashsumsImageInfoOptionsPartial): boolean =>
20+
getOpt(options) !== undefined;
21+
22+
const build = (options: CalcHashsumsImageInfoOptionsPartial): string => {
23+
const hashsumTypes = getOpt(options);
24+
guardIsUndef(hashsumTypes, "calcHashsums");
25+
26+
if (!hashsumTypes.length) {
27+
return "chs:";
28+
}
29+
30+
return `chs:${hashsumTypes.join(":")}`;
31+
};
32+
33+
export { test, build };

src/optionsImageInfo/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * as average from "./average";
22
export * as blurhash from "./blurhash";
33
export * as cacheBuster from "../optionsShared/cacheBuster";
4+
export * as calcHashsums from "./calcHashsums";
45
export * as crop from "../optionsShared/crop";
56
export * as detectObjects from "./detectObjects";
67
export * as dimensions from "./dimensions";

src/typesImageInfo/calcHashsums.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* *CalcHashsums option*
3+
*
4+
* @warning **Slow**. This option requires the image to be fully downloaded and processed.
5+
*
6+
* When specified, imgproxy will calculate the specified hashsums of the source image.
7+
*
8+
* Available hashsum types: md5, sha1, sha256, sha512
9+
*
10+
* Response example:
11+
* {
12+
* "hashsums": {
13+
* "md5": "cc507f81206a4c7d0a995a07c3d9f43a",
14+
* "sha256": "621f6c6d68de754c6cdf3d286b7837634ce9f273f30f377b3e0df0568a23cee0"
15+
* }
16+
* }
17+
*
18+
* @note Hashsum calculation for video files is not supported.
19+
*
20+
* @see {@link https://docs.imgproxy.net/usage/getting_info#calc-hashsums | CalcHashsums imgproxy docs}
21+
*/
22+
type HashsumType = "md5" | "sha1" | "sha256" | "sha512";
23+
24+
/**
25+
* *CalcHashsums option*
26+
*
27+
* To specify hashsum types to calculate, you can use the keyword `calcHashsums` or `chs`.
28+
*
29+
* @see https://docs.imgproxy.net/usage/getting_info#calc-hashsums
30+
*/
31+
interface CalcHashsumsImageInfoOptionsPartial {
32+
calcHashsums?: HashsumType[];
33+
chs?: HashsumType[];
34+
}
35+
36+
export { HashsumType, CalcHashsumsImageInfoOptionsPartial };

src/typesImageInfo/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AverageImageInfoOptionsPartial } from "./average";
22
import type { BHImageInfoOptionsPartial } from "./blurhash";
33
import type { CacheBusterOptionsPartial } from "../typesShared/cacheBuster";
4+
import type { CalcHashsumsImageInfoOptionsPartial } from "./calcHashsums";
45
import type { CropOptionsPartial } from "../typesShared/crop";
56
import type { DCImageInfoOptionsPartial } from "./dominantColors";
67
import type { DetectObjectsImageInfoOptionsPartial } from "./detectObjects";
@@ -23,6 +24,7 @@ import type { WildOptionsPartial } from "../typesShared/wildOptions";
2324
export type OptionsImageInfo = AverageImageInfoOptionsPartial &
2425
BHImageInfoOptionsPartial &
2526
CacheBusterOptionsPartial &
27+
CalcHashsumsImageInfoOptionsPartial &
2628
CropOptionsPartial &
2729
DCImageInfoOptionsPartial &
2830
DetectObjectsImageInfoOptionsPartial &
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from "vitest";
2+
import { test, build } from "../../src/optionsImageInfo/calcHashsums";
3+
4+
describe("calcHashsums", () => {
5+
describe("test", () => {
6+
it("should return true if calcHashsums option is defined", () => {
7+
expect(test({ calcHashsums: ["md5", "sha256"] })).toEqual(true);
8+
});
9+
10+
it("should return true if chs option is defined", () => {
11+
expect(test({ chs: ["sha1"] })).toEqual(true);
12+
});
13+
14+
it("should return true if calcHashsums is an empty array", () => {
15+
expect(test({ calcHashsums: [] })).toEqual(true);
16+
});
17+
18+
it("should return false if calcHashsums option is undefined", () => {
19+
expect(test({})).toEqual(false);
20+
});
21+
});
22+
23+
describe("build", () => {
24+
it("should throw an error if calcHashsums option is undefined", () => {
25+
expect(() => build({})).toThrow("calcHashsums option is undefined");
26+
});
27+
28+
it("should return 'chs:' if calcHashsums is an empty array", () => {
29+
expect(build({ calcHashsums: [] })).toEqual("chs:");
30+
});
31+
32+
it("should return 'chs:md5' for a single hashsum type", () => {
33+
expect(build({ calcHashsums: ["md5"] })).toEqual("chs:md5");
34+
});
35+
36+
it("should return 'chs:md5:sha256' for multiple hashsum types", () => {
37+
expect(build({ calcHashsums: ["md5", "sha256"] })).toEqual(
38+
"chs:md5:sha256"
39+
);
40+
});
41+
42+
it("should work with the chs alias", () => {
43+
expect(build({ chs: ["sha1", "sha512"] })).toEqual("chs:sha1:sha512");
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)