Skip to content

Commit 0072623

Browse files
fix: fixing logic for resizing image url
1 parent c7d5553 commit 0072623

File tree

4 files changed

+113
-25
lines changed

4 files changed

+113
-25
lines changed

package/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"path": "0.12.7",
8181
"react-art": "^17.0.2",
8282
"react-native-markdown-package": "1.8.1",
83+
"react-native-url-polyfill": "^1.3.0",
8384
"stream-chat": "^5.1.2"
8485
},
8586
"peerDependencies": {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { PixelRatio } from 'react-native';
2+
3+
import { getResizedImageUrl } from '../getResizedImageUrl';
4+
5+
describe('getResizedImageUrl', () => {
6+
it('should return same url if its not cloudfront cdn', () => {
7+
const testUrl = 'http://foo.com/blah_(wikipedia)#cite-1';
8+
9+
const resizedUrl = getResizedImageUrl({ height: 100, url: testUrl, width: 100 });
10+
expect(resizedUrl).toEqual(testUrl);
11+
});
12+
13+
it('should append given height, width and resize mode to url', () => {
14+
const testUrl = 'http://us-east.stream-io-cdn.com/blah';
15+
16+
const testConfigs = [
17+
{ height: 100, resize: 'scale', width: 100 },
18+
{ height: 200, resize: 'fill', width: 300 },
19+
{ height: 1000, resize: 'clip', width: 10 },
20+
];
21+
22+
testConfigs.forEach(({ height, resize, width }) => {
23+
const resizedUrl = getResizedImageUrl({ height, resize, url: testUrl, width });
24+
const parsedUrl = new URL(resizedUrl);
25+
expect(parsedUrl.searchParams.get('h')).toEqual(
26+
`${PixelRatio.getPixelSizeForLayoutSize(height)}`,
27+
);
28+
expect(parsedUrl.searchParams.get('w')).toEqual(
29+
`${PixelRatio.getPixelSizeForLayoutSize(width)}`,
30+
);
31+
expect(parsedUrl.searchParams.get('resize')).toEqual(resize);
32+
});
33+
});
34+
35+
it('should replace wildcards with given height, width and resize mode within url', () => {
36+
const testUrl = 'http://us-east.stream-io-cdn.com/blah?h=*&w=*&resize=*';
37+
38+
const testConfigs = [
39+
{ height: 100, resize: 'scale', width: 100 },
40+
{ height: 200, resize: 'fill', width: 300 },
41+
{ height: 1000, resize: 'clip', width: 10 },
42+
];
43+
44+
testConfigs.forEach(({ height, resize, width }) => {
45+
const resizedUrl = getResizedImageUrl({ height, resize, url: testUrl, width });
46+
const parsedUrl = new URL(resizedUrl);
47+
expect(parsedUrl.searchParams.get('h')).toEqual(
48+
`${PixelRatio.getPixelSizeForLayoutSize(height)}`,
49+
);
50+
expect(parsedUrl.searchParams.get('w')).toEqual(
51+
`${PixelRatio.getPixelSizeForLayoutSize(width)}`,
52+
);
53+
expect(parsedUrl.searchParams.get('resize')).toEqual(resize);
54+
});
55+
});
56+
});
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { PixelRatio } from 'react-native';
2+
import { setupURLPolyfill } from 'react-native-url-polyfill';
3+
4+
setupURLPolyfill();
25

36
type GetResizedImageUrlParams = {
47
url: string;
58
height?: string | number;
9+
resize?: 'clip' | 'crop' | 'fill' | 'scale';
610
width?: string | number;
711
};
812

@@ -18,26 +22,27 @@ type GetResizedImageUrlParams = {
1822
*
1923
* @returns {string} Url of the image with given height and width.
2024
*/
21-
export function getResizedImageUrl({ height, url, width }: GetResizedImageUrlParams) {
22-
const isResizableUrl = url.includes('&h=*') && url.includes('&w=*') && url.includes('&resize=*');
23-
25+
export function getResizedImageUrl({
26+
height,
27+
resize = 'clip',
28+
url,
29+
width,
30+
}: GetResizedImageUrlParams) {
31+
// Check if url belongs to cloudfront CDN
32+
const isResizableUrl = url.includes('.stream-io-cdn.com');
2433
if (!isResizableUrl || (!height && !width)) return url;
2534

26-
let resizedUrl = url;
35+
const parsedUrl = new URL(url);
2736

2837
if (height) {
29-
resizedUrl = resizedUrl.replace(
30-
'h=*',
31-
`h=${PixelRatio.getPixelSizeForLayoutSize(Number(height))}`,
32-
);
38+
parsedUrl.searchParams.set('h', `${PixelRatio.getPixelSizeForLayoutSize(Number(height))}`);
3339
}
3440

3541
if (width) {
36-
resizedUrl = resizedUrl.replace(
37-
'w=*',
38-
`w=${PixelRatio.getPixelSizeForLayoutSize(Number(width))}`,
39-
);
42+
parsedUrl.searchParams.set('w', `${PixelRatio.getPixelSizeForLayoutSize(Number(width))}`);
4043
}
4144

42-
return resizedUrl.replace('resize=*', `resize=clip`);
45+
parsedUrl.searchParams.set('resize', `${resize}`);
46+
47+
return parsedUrl.toString();
4348
}

package/yarn.lock

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,7 +3183,7 @@ base-64@^0.1.0:
31833183
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
31843184
integrity sha1-eAqZyE59YAJgNhURxId2E78k9rs=
31853185

3186-
base64-js@^1.0.2, base64-js@^1.1.2, base64-js@^1.5.1:
3186+
base64-js@^1.0.2, base64-js@^1.1.2, base64-js@^1.3.1, base64-js@^1.5.1:
31873187
version "1.5.1"
31883188
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
31893189
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
@@ -3467,6 +3467,14 @@ buffer@^4.3.0:
34673467
ieee754 "^1.1.4"
34683468
isarray "^1.0.0"
34693469

3470+
buffer@^5.4.3:
3471+
version "5.7.1"
3472+
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
3473+
integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==
3474+
dependencies:
3475+
base64-js "^1.3.1"
3476+
ieee754 "^1.1.13"
3477+
34703478
builtin-status-codes@^3.0.0:
34713479
version "3.0.0"
34723480
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8"
@@ -5544,7 +5552,7 @@ flush-write-stream@^1.0.0:
55445552
inherits "^2.0.3"
55455553
readable-stream "^2.3.6"
55465554

5547-
follow-redirects@^1.0.0, follow-redirects@^1.10.0:
5555+
follow-redirects@^1.0.0, follow-redirects@^1.14.4:
55485556
version "1.14.7"
55495557
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685"
55505558
integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==
@@ -6149,7 +6157,7 @@ [email protected]:
61496157
dependencies:
61506158
safer-buffer ">= 2.1.2 < 3"
61516159

6152-
ieee754@^1.1.4:
6160+
ieee754@^1.1.13, ieee754@^1.1.4:
61536161
version "1.2.1"
61546162
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
61556163
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==
@@ -6414,21 +6422,14 @@ is-ci@^2.0.0:
64146422
dependencies:
64156423
ci-info "^2.0.0"
64166424

6417-
is-core-module@^2.2.0, is-core-module@^2.8.0:
6425+
is-core-module@^2.2.0:
64186426
version "2.8.0"
64196427
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.0.tgz#0321336c3d0925e497fd97f5d95cb114a5ccd548"
64206428
integrity sha512-vd15qHsaqrRL7dtH6QNuy0ndJmRDrS9HAM1CAiSifNUFv4x1a0CCVsj18hJ1mShxIG6T2i1sO78MkP56r0nYRw==
64216429
dependencies:
64226430
has "^1.0.3"
64236431

6424-
is-core-module@^2.8.0:
6425-
version "2.8.1"
6426-
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
6427-
integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==
6428-
dependencies:
6429-
has "^1.0.3"
6430-
6431-
is-core-module@^2.8.0:
6432+
is-core-module@^2.8.0, is-core-module@^2.8.1:
64326433
version "2.8.1"
64336434
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
64346435
integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==
@@ -9746,6 +9747,13 @@ [email protected]:
97469747
semver "^5.4.1"
97479748
source-map "^0.5.6"
97489749

9750+
react-native-url-polyfill@^1.3.0:
9751+
version "1.3.0"
9752+
resolved "https://registry.yarnpkg.com/react-native-url-polyfill/-/react-native-url-polyfill-1.3.0.tgz#c1763de0f2a8c22cc3e959b654c8790622b6ef6a"
9753+
integrity sha512-w9JfSkvpqqlix9UjDvJjm1EjSt652zVQ6iwCIj1cVVkwXf4jQhQgTNXY6EVTwuAmUjg6BC6k9RHCBynoLFo3IQ==
9754+
dependencies:
9755+
whatwg-url-without-unicode "8.0.0-3"
9756+
97499757
97509758
version "0.15.0"
97519759
resolved "https://registry.yarnpkg.com/react-native-web/-/react-native-web-0.15.0.tgz#c97d47dfc43bfd99a562370fefb43ca38beb38b0"
@@ -10248,6 +10256,15 @@ resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.14.2, resolve@^1.1
1024810256
is-core-module "^2.2.0"
1024910257
path-parse "^1.0.6"
1025010258

10259+
resolve@^1.20.0:
10260+
version "1.22.0"
10261+
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.22.0.tgz#5e0b8c67c15df57a89bdbabe603a002f21731198"
10262+
integrity sha512-Hhtrw0nLeSrFQ7phPp4OOcVjLPIeMnRlr5mcnVuMe7M/7eBn98A3hmFRLoFo3DLZkivSYwhRUJTyPyWAk56WLw==
10263+
dependencies:
10264+
is-core-module "^2.8.1"
10265+
path-parse "^1.0.7"
10266+
supports-preserve-symlinks-flag "^1.0.0"
10267+
1025110268
resolve@^2.0.0-next.3:
1025210269
version "2.0.0-next.3"
1025310270
resolved "https://registry.yarnpkg.com/resolve/-/resolve-2.0.0-next.3.tgz#d41016293d4a8586a39ca5d9b5f15cbea1f55e46"
@@ -12123,6 +12140,15 @@ whatwg-mimetype@^2.3.0:
1212312140
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
1212412141
integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==
1212512142

12143+
12144+
version "8.0.0-3"
12145+
resolved "https://registry.yarnpkg.com/whatwg-url-without-unicode/-/whatwg-url-without-unicode-8.0.0-3.tgz#ab6df4bf6caaa6c85a59f6e82c026151d4bb376b"
12146+
integrity sha512-HoKuzZrUlgpz35YO27XgD28uh/WJH4B0+3ttFqRo//lmq+9T/mIOJ6kqmINI9HpUpz1imRC/nR/lxKpJiv0uig==
12147+
dependencies:
12148+
buffer "^5.4.3"
12149+
punycode "^2.1.1"
12150+
webidl-conversions "^5.0.0"
12151+
1212612152
whatwg-url@^5.0.0:
1212712153
version "5.0.0"
1212812154
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"

0 commit comments

Comments
 (0)