-
Notifications
You must be signed in to change notification settings - Fork 6
Add max_result_dimension processing option #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@imgproxy/imgproxy-js-core": minor | ||
| --- | ||
|
|
||
| Add support for [max_result_dimension](https://docs.imgproxy.net/latest/usage/processing#max-result-dimension) option |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { | ||
| MaxResultDimension, | ||
| MaxResultDimensionOptionsPartial, | ||
| } from "../types/maxResultDimension"; | ||
| import { guardIsUndef, guardIsNotNum } from "../utils"; | ||
|
|
||
| const getOpt = ( | ||
| options: MaxResultDimensionOptionsPartial | ||
| ): MaxResultDimension | undefined => { | ||
| return options.max_result_dimension ?? options.mrd; | ||
| }; | ||
|
|
||
| const test = (options: MaxResultDimensionOptionsPartial): boolean => | ||
| getOpt(options) !== undefined; | ||
|
|
||
| const build = (options: MaxResultDimensionOptionsPartial): string => { | ||
| const maxResultDimension = getOpt(options); | ||
|
|
||
| guardIsUndef(maxResultDimension, "max_result_dimension"); | ||
| guardIsNotNum(maxResultDimension, "max_result_dimension", { | ||
| addParam: { min: 0 }, | ||
| }); | ||
|
|
||
| return `mrd:${maxResultDimension}`; | ||
| }; | ||
|
|
||
| export { test, build }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * *Max result dimension* | ||
| * | ||
| * Allows redefining the `IMGPROXY_MAX_RESULT_DIMENSION` configuration. | ||
| * Specifies the maximum allowed dimension for the resulting image. | ||
| * | ||
| * @warning Since this option allows redefining a security restriction, | ||
| * its usage is not allowed unless the `IMGPROXY_ALLOW_SECURITY_OPTIONS` config is set to true. | ||
| * | ||
| * @see {@link https://docs.imgproxy.net/latest/usage/processing#max-result-dimension | max result dimension imgproxy docs} | ||
| */ | ||
| type MaxResultDimension = number; | ||
|
|
||
| /** | ||
| * *Max result dimension* | ||
| * | ||
| * To describe the max result dimension option, you can use the keyword `max_result_dimension` or `mrd`. | ||
| */ | ||
| interface MaxResultDimensionOptionsPartial { | ||
| max_result_dimension?: MaxResultDimension; | ||
| mrd?: MaxResultDimension; | ||
| } | ||
|
|
||
| export { MaxResultDimension, MaxResultDimensionOptionsPartial }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { test, build } from "../../src/options/maxResultDimension"; | ||
|
|
||
| describe("maxResultDimension", () => { | ||
| describe("test", () => { | ||
| it("should return true if max_result_dimension option is defined", () => { | ||
| expect(test({ max_result_dimension: 1920 })).toEqual(true); | ||
| }); | ||
|
|
||
| it("should return true if mrd option is defined", () => { | ||
| expect(test({ mrd: 2560 })).toEqual(true); | ||
| }); | ||
|
|
||
| it("should return false if max_result_dimension option is undefined", () => { | ||
| expect(test({})).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("build", () => { | ||
| it("should throw an error if max_result_dimension option is undefined", () => { | ||
| expect(() => build({})).toThrow( | ||
| "max_result_dimension option is undefined" | ||
| ); | ||
| }); | ||
|
|
||
| it("should throw an error if max_result_dimension is not a number", () => { | ||
| // @ts-expect-error: Let's ignore an error (check for users with vanilla js). | ||
| expect(() => build({ max_result_dimension: "1920" })).toThrow( | ||
| "max_result_dimension option is not a number" | ||
| ); | ||
| }); | ||
|
|
||
| it("should throw an error if max_result_dimension is less than 0", () => { | ||
| expect(() => build({ max_result_dimension: -1 })).toThrow( | ||
| "max_result_dimension option value can't be less then 0" | ||
| ); | ||
| }); | ||
|
|
||
| it("should return mrd:0 if max_result_dimension option is 0", () => { | ||
| expect(build({ max_result_dimension: 0 })).toEqual("mrd:0"); | ||
| }); | ||
|
|
||
| it("should return mrd:1920 if mrd option is 1920", () => { | ||
| expect(build({ mrd: 1920 })).toEqual("mrd:1920"); | ||
| }); | ||
|
|
||
| it("should return mrd:2560 if max_result_dimension option is 2560", () => { | ||
| expect(build({ max_result_dimension: 2560 })).toEqual("mrd:2560"); | ||
| }); | ||
|
|
||
| it("should work with 0", () => { | ||
| expect(build({ max_result_dimension: 0 })).toEqual("mrd:0"); | ||
| expect(build({ mrd: 0 })).toEqual("mrd:0"); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.