Skip to content

Commit ab13822

Browse files
committed
feat: Resize video thumb_url #347
1 parent dd01906 commit ab13822

13 files changed

+249
-164
lines changed

projects/stream-chat-angular/src/lib/attachment-configuration.service.spec.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ describe('AttachmentConfigurationService', () => {
1111

1212
it('should provide the correct configuration for image attachments', () => {
1313
let attachment: Attachment = {
14-
img_url: 'url/to/img',
14+
img_url: 'http://url/to/img',
1515
thumb_url: 'different/url',
1616
};
1717

1818
expect(
1919
service.getImageAttachmentConfiguration(attachment, 'single')
2020
).toEqual({
21-
url: 'url/to/img&h=600&w=600',
21+
url: 'http://url/to/img?h=600&w=600',
2222
height: '300px',
2323
width: '',
2424
});
2525

2626
attachment = {
27-
thumb_url: 'url/to/img',
27+
thumb_url: 'http://url/to/img',
2828
};
2929

3030
expect(
3131
service.getImageAttachmentConfiguration(attachment, 'single')
3232
).toEqual({
33-
url: 'url/to/img&h=600&w=600',
33+
url: 'http://url/to/img?h=600&w=600',
3434
height: '300px',
3535
width: '',
3636
});
3737

3838
attachment = {
39-
image_url: 'url/to/img',
39+
image_url: 'http://url/to/img',
4040
};
4141

4242
expect(
4343
service.getImageAttachmentConfiguration(attachment, 'single')
4444
).toEqual({
45-
url: 'url/to/img&h=600&w=600',
45+
url: 'http://url/to/img?h=600&w=600',
4646
height: '300px',
4747
width: '',
4848
});
@@ -52,7 +52,7 @@ describe('AttachmentConfigurationService', () => {
5252
const spy = jasmine.createSpy();
5353
service.customImageAttachmentConfigurationHandler = spy;
5454
const attachment: Attachment = {
55-
img_url: 'url/to/img',
55+
img_url: 'http://url/to/img',
5656
thumb_url: 'different/url',
5757
};
5858
service.getImageAttachmentConfiguration(attachment, 'carousel');
@@ -62,51 +62,74 @@ describe('AttachmentConfigurationService', () => {
6262

6363
it('should provide the correct configuration for gallery image attachments', () => {
6464
const attachment: Attachment = {
65-
img_url: 'url/to/img',
65+
img_url: 'http://url/to/img',
6666
};
6767

6868
expect(
6969
service.getImageAttachmentConfiguration(attachment, 'gallery')
7070
).toEqual({
71-
url: 'url/to/img&h=300&w=300',
71+
url: 'http://url/to/img?h=300&w=300',
7272
height: '150px',
7373
width: '',
7474
});
7575
});
7676

7777
it('should provide the correct configuration for image attachments inside the carousel', () => {
7878
const attachment: Attachment = {
79-
img_url: 'url/to/img',
79+
img_url: 'http://url/to/img',
8080
};
8181

8282
expect(
8383
service.getImageAttachmentConfiguration(attachment, 'carousel')
8484
).toEqual({
85-
url: 'url/to/img',
85+
url: 'http://url/to/img',
8686
height: '',
8787
width: '',
8888
});
8989
});
9090

9191
it('should provide the correct configuration for video attachments', () => {
92-
const attachment: Attachment = {
93-
asset_url: 'url/to/video',
94-
thumb_url: 'url/to/poster',
92+
let attachment: Attachment = {
93+
asset_url: 'http://url/to/video',
94+
thumb_url: 'http://url/to/poster',
95+
};
96+
97+
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
98+
url: 'http://url/to/video',
99+
height: '100%',
100+
width: '100%',
101+
thumbUrl: 'http://url/to/poster?h=600&w=600',
102+
});
103+
104+
attachment = {
105+
asset_url: 'http://url/to/video',
106+
thumb_url: 'http://url/to/poster?oh=1080&ow=1920',
107+
};
108+
109+
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
110+
url: 'http://url/to/video',
111+
height: '100%',
112+
width: '100%',
113+
thumbUrl: 'http://url/to/poster?oh=1080&ow=1920&h=600&w=1067',
114+
});
115+
116+
attachment = {
117+
asset_url: 'http://url/to/video',
95118
};
96119

97120
expect(service.getVideoAttachmentConfiguration(attachment)).toEqual({
98-
url: 'url/to/video',
121+
url: 'http://url/to/video',
99122
height: '100%',
100123
width: '100%',
101-
thumbUrl: 'url/to/poster',
124+
thumbUrl: undefined,
102125
});
103126
});
104127

105128
it('should call #customVideoAttachmentConfigurationHandler, if provided', () => {
106129
const spy = jasmine.createSpy();
107130
service.customVideoAttachmentConfigurationHandler = spy;
108131
const attachment: Attachment = {
109-
img_url: 'url/to/video',
132+
img_url: 'http://url/to/video',
110133
thumb_url: 'different/url',
111134
};
112135
service.getVideoAttachmentConfiguration(attachment);
@@ -169,12 +192,12 @@ describe('AttachmentConfigurationService', () => {
169192

170193
it('should provide correct configuration for scraped images', () => {
171194
let attachment: Attachment = {
172-
image_url: 'url/to/img',
195+
image_url: 'http://url/to/img',
173196
thumb_url: 'different/url',
174197
};
175198

176199
expect(service.getScrapedImageAttachmentConfiguration(attachment)).toEqual({
177-
url: 'url/to/img',
200+
url: 'http://url/to/img',
178201
width: '',
179202
height: '',
180203
});
@@ -203,16 +226,14 @@ describe('AttachmentConfigurationService', () => {
203226

204227
it('should provide integer values for image resize and make sure that each dimension is at least the size restriction', () => {
205228
const attachment = {
206-
img_url: 'url/to/img',
207-
original_width: 3534,
208-
original_height: 4417,
229+
img_url: 'http://url/to/img?ow=3534&oh=4417',
209230
};
210231

211232
const result = service.getImageAttachmentConfiguration(
212233
attachment,
213234
'single'
214235
);
215236

216-
expect(result.url).toContain('&h=750&w=600');
237+
expect(result.url).toContain('h=750&w=600');
217238
});
218239
});

projects/stream-chat-angular/src/lib/attachment-configuration.service.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,23 @@ export class AttachmentConfigurationService<
6161
const sizeResctriction = {
6262
gallery: { height: 300, width: 300 },
6363
single: { height: 600, width: 600 },
64-
carousel: { height: undefined, windt: undefined },
64+
carousel: { height: undefined, width: undefined },
6565
}[location];
6666

6767
const height = sizeResctriction.height
6868
? `${sizeResctriction.height / 2}px`
6969
: '';
7070

71-
const oh = attachment.original_height || 1;
72-
const ow = attachment.original_width || 1;
73-
const resizeParameters =
74-
sizeResctriction.height || sizeResctriction.width
75-
? `&h=${Math.round(
76-
Math.max(
77-
sizeResctriction.height,
78-
((sizeResctriction.width || 1) / ow) * oh
79-
)
80-
)}&w=${Math.round(
81-
Math.max(
82-
sizeResctriction.width,
83-
((sizeResctriction.height || 1) / oh) * ow
84-
)
85-
)}`
86-
: '';
71+
const url = new URL(
72+
(attachment.img_url ||
73+
attachment.thumb_url ||
74+
attachment.image_url ||
75+
'') as string
76+
);
77+
this.addResiziParamsToUrl(sizeResctriction, url);
8778

8879
return {
89-
url:
90-
((attachment.img_url ||
91-
attachment.thumb_url ||
92-
attachment.image_url ||
93-
'') as string) + resizeParameters,
80+
url: url.href,
9481
width: '',
9582
height,
9683
};
@@ -107,11 +94,17 @@ export class AttachmentConfigurationService<
10794
return this.customVideoAttachmentConfigurationHandler(attachment);
10895
}
10996

97+
let thumbUrl = undefined;
98+
if (attachment.thumb_url) {
99+
const url = new URL(attachment.thumb_url);
100+
this.addResiziParamsToUrl({ width: 600, height: 600 }, url);
101+
thumbUrl = url.href;
102+
}
110103
return {
111104
url: attachment.asset_url || '',
112105
width: '100%', // Set from CSS
113106
height: '100%',
114-
thumbUrl: attachment.thumb_url,
107+
thumbUrl: thumbUrl,
115108
};
116109
}
117110

@@ -152,4 +145,37 @@ export class AttachmentConfigurationService<
152145
height: '', // Set from CSS
153146
};
154147
}
148+
149+
private addResiziParamsToUrl(
150+
sizeResctriction: { width: number | undefined; height: number | undefined },
151+
url: URL
152+
) {
153+
const urlParams = url.searchParams;
154+
const originalHeight = Number(urlParams.get('oh')) || 1;
155+
const originalWidth = Number(urlParams.get('ow')) || 1;
156+
157+
const h = sizeResctriction.height
158+
? Math.round(
159+
Math.max(
160+
sizeResctriction.height,
161+
((sizeResctriction.width || 1) / originalWidth) * originalHeight
162+
)
163+
).toString()
164+
: undefined;
165+
const w = sizeResctriction.width
166+
? Math.round(
167+
Math.max(
168+
sizeResctriction.width,
169+
((sizeResctriction.height || 1) / originalHeight) * originalWidth
170+
)
171+
).toString()
172+
: undefined;
173+
174+
if (h) {
175+
url.searchParams.append('h', h);
176+
}
177+
if (w) {
178+
url.searchParams.append('w', w);
179+
}
180+
}
155181
}

0 commit comments

Comments
 (0)