Skip to content

Commit 6cd7faf

Browse files
committed
feat: improve image key extraction logic in FeedbackImage component
1 parent 4352fcf commit 6cd7faf

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

apps/web/src/shared/ui/feedback-image.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,23 @@ const FeedbackImage = ({ url }: Props) => {
4040
});
4141

4242
const imageKey = useMemo(() => {
43-
if (!channelData?.imageConfig?.enablePresignedUrlDownload) return url;
43+
if (!channelData?.imageConfig?.enablePresignedUrlDownload) return '';
4444

45-
let parsed: URL | null = null;
46-
try {
47-
parsed = new URL(
48-
url,
49-
typeof window !== 'undefined' ?
50-
window.location.href
51-
: 'http://localhost',
52-
);
53-
} catch {
54-
return url;
55-
}
56-
if (!/^https?:$/.test(parsed.protocol)) return url;
45+
const s3Pattern = /(s3[.-][a-z0-9-]+\.amazonaws\.com|s3\.amazonaws\.com)/;
46+
if (!s3Pattern.test(url)) return '';
5747

58-
const rawPath = parsed.pathname.replace(/^\/+/, '');
59-
let key = rawPath;
60-
try {
61-
key = decodeURIComponent(rawPath);
62-
} catch {
63-
/* keep raw */
64-
}
48+
const parsedUrl = new URL(url);
49+
const key = decodeURIComponent(parsedUrl.pathname.replace(/^\/+/, ''));
50+
const host = parsedUrl.hostname;
6551

66-
const host = parsed.hostname;
6752
const parts = key.split('/', 2);
6853
if (
6954
(host === 's3.amazonaws.com' || host.startsWith('s3.')) &&
7055
parts.length >= 2
7156
) {
7257
return parts.slice(1).join('/');
7358
}
59+
7460
return key;
7561
}, [channelData, url]);
7662

0 commit comments

Comments
 (0)