Skip to content

Commit 47195ba

Browse files
committed
Merge branch 'master' into v5
2 parents 1abb764 + 1c2b7f7 commit 47195ba

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,17 @@ describe('AttachmentConfigurationService', () => {
345345
expect(url).toContain('h=600');
346346
expect(url).toContain('w=600');
347347
});
348+
349+
it('should handle if image attachments have invalid or missing urls', () => {
350+
const attachment: Attachment = {};
351+
const htmlElement = {
352+
'max-width': '300px',
353+
'max-height': '300px',
354+
height: '',
355+
} as any as HTMLElement;
356+
357+
expect(() =>
358+
service.getImageAttachmentConfiguration(attachment, 'single', htmlElement)
359+
).not.toThrow(jasmine.any(Error));
360+
});
348361
});

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

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,31 @@ export class AttachmentConfigurationService<
6767
);
6868
}
6969

70-
const url = new URL(
71-
(attachment.img_url ||
72-
attachment.thumb_url ||
73-
attachment.image_url ||
74-
'') as string
75-
);
70+
const defaultOriginalDimension = 1000000;
71+
const urlString = (attachment.img_url ||
72+
attachment.thumb_url ||
73+
attachment.image_url ||
74+
'') as string;
75+
let url: URL;
76+
try {
77+
url = new URL(urlString);
78+
} catch (error) {
79+
return {
80+
url: urlString,
81+
width: '', // Not set to respect responsive width
82+
height: '', // Set from CSS
83+
originalHeight: defaultOriginalDimension,
84+
originalWidth: defaultOriginalDimension,
85+
};
86+
}
7687
const originalHeight =
7788
Number(url.searchParams.get('oh')) > 1
7889
? Number(url.searchParams.get('oh'))
79-
: 1000000;
90+
: defaultOriginalDimension;
8091
const originalWidth =
8192
Number(url.searchParams.get('ow')) > 1
8293
? Number(url.searchParams.get('ow'))
83-
: 1000000;
94+
: defaultOriginalDimension;
8495
const displayWarning = location === 'gallery' || location === 'single';
8596
const sizeRestriction = this.getSizingRestrictions(
8697
url,
@@ -120,32 +131,37 @@ export class AttachmentConfigurationService<
120131
);
121132
}
122133

123-
let thumbUrl = undefined;
134+
let thumbUrl: string | undefined = undefined;
124135
let originalHeight = 1000000;
125136
let originalWidth = 1000000;
126137
if (attachment.thumb_url && this.shouldGenerateVideoThumbnail) {
127-
const url = new URL(attachment.thumb_url);
128-
originalHeight =
129-
Number(url.searchParams.get('oh')) > 1
130-
? Number(url.searchParams.get('oh'))
131-
: originalHeight;
132-
originalWidth =
133-
Number(url.searchParams.get('ow')) > 1
134-
? Number(url.searchParams.get('ow'))
135-
: originalWidth;
136-
const displayWarning = true;
137-
const sizeRestriction = this.getSizingRestrictions(
138-
url,
139-
element,
140-
displayWarning
141-
);
138+
let url: URL;
139+
try {
140+
url = new URL(attachment.thumb_url);
142141

143-
if (sizeRestriction) {
144-
sizeRestriction.height *= 2;
145-
sizeRestriction.width *= 2;
146-
this.addResizingParamsToUrl(sizeRestriction, url);
142+
originalHeight =
143+
Number(url.searchParams.get('oh')) > 1
144+
? Number(url.searchParams.get('oh'))
145+
: originalHeight;
146+
originalWidth =
147+
Number(url.searchParams.get('ow')) > 1
148+
? Number(url.searchParams.get('ow'))
149+
: originalWidth;
150+
const displayWarning = true;
151+
const sizeRestriction = this.getSizingRestrictions(
152+
url,
153+
element,
154+
displayWarning
155+
);
156+
if (sizeRestriction) {
157+
sizeRestriction.height *= 2;
158+
sizeRestriction.width *= 2;
159+
this.addResizingParamsToUrl(sizeRestriction, url);
160+
}
161+
thumbUrl = url.href;
162+
} catch {
163+
thumbUrl = attachment.thumb_url;
147164
}
148-
thumbUrl = url.href;
149165
}
150166
return {
151167
url: attachment.asset_url || '',

0 commit comments

Comments
 (0)