From 437e666b3c4ea83627f1658014ac83e4adeac088 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Wed, 24 Apr 2024 09:40:37 +0300 Subject: [PATCH 1/3] getImageUrl.ts refactoring --- src/utils/getImageUrl.ts | 53 +++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/utils/getImageUrl.ts b/src/utils/getImageUrl.ts index bc3c9073..e4bd09f6 100644 --- a/src/utils/getImageUrl.ts +++ b/src/utils/getImageUrl.ts @@ -1,31 +1,41 @@ import { cdnUrl, thumborUrl } from './config' -const getSizeUrlPart = (options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {}) => { - const widthString = options.width ? options.width.toString() : '' - const heightString = options.height ? options.height.toString() : '' +const URL_CONFIG = { + cdnUrl: cdnUrl, + thumborUrl: thumborUrl, + audioSubfolder: 'audio', + imageSubfolder: 'image', + productionFolder: 'production/', +} - if (!(widthString || heightString) || options.noSizeUrlPart) { - return '' - } +const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac']) - return `${widthString}x${heightString}/` +const isAudioFile = (filename: string): boolean => { + const extension = filename.split('.').pop()?.toLowerCase() + return AUDIO_EXTENSIONS.has(extension ?? '') +} +const getLastSegment = (url: string): string => url.toLowerCase().split('/').pop() || '' + +const buildSizePart = (width?: number, height?: number, includeSize = true): string => { + if (!includeSize) return '' + const widthPart = width ? width.toString() : '' + const heightPart = height ? height.toString() : '' + return widthPart || heightPart ? `${widthPart}x${heightPart}/` : '' } export const getImageUrl = ( src: string, options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {}, -) => { +): string => { if (!src.includes('discours.io') && src.includes('http')) { return src } - const filename = src.toLowerCase().split('/').pop() - const ext = filename.split('.').pop() - const isAudio = ext in ['wav', 'mp3', 'ogg', 'aif', 'flac'] - const base = isAudio ? cdnUrl : `${thumborUrl}/unsafe/` - const suffix = isAudio || options.noSizeUrlPart ? '' : getSizeUrlPart(options) - const subfolder = isAudio ? 'audio' : 'image' + const filename = getLastSegment(src) + const base = isAudioFile(filename) ? URL_CONFIG.cdnUrl : URL_CONFIG.thumborUrl + const suffix = options.noSizeUrlPart ? '' : buildSizePart(options.width, options.height) + const subfolder = isAudioFile(filename) ? URL_CONFIG.audioSubfolder : URL_CONFIG.imageSubfolder - return `${base}${suffix}production/${subfolder}/${filename}` + return `${base}${suffix}${URL_CONFIG.productionFolder}${subfolder}/${filename}` } export const getOpenGraphImageUrl = ( @@ -37,17 +47,16 @@ export const getOpenGraphImageUrl = ( width?: number height?: number }, -) => { - const sizeUrlPart = getSizeUrlPart(options) - +): string => { + const sizeUrlPart = buildSizePart(options.width, options.height) const filtersPart = `filters:discourstext('${encodeURIComponent(options.topic)}','${encodeURIComponent( options.author, )}','${encodeURIComponent(options.title)}')/` - if (src.startsWith(thumborUrl)) { - const thumborKey = src.replace(`${thumborUrl}/unsafe`, '') - return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${thumborKey}` + if (src.startsWith(URL_CONFIG.thumborUrl)) { + const thumborKey = src.replace(URL_CONFIG.thumborUrl, '') + return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${thumborKey}` } - return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${src}` + return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${src}` } From 59561ec26b9b631873b317fa80b9d8dfa828f442 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Wed, 24 Apr 2024 10:22:05 +0300 Subject: [PATCH 2/3] add unsafe --- src/utils/getImageUrl.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/utils/getImageUrl.ts b/src/utils/getImageUrl.ts index e4bd09f6..d820bfae 100644 --- a/src/utils/getImageUrl.ts +++ b/src/utils/getImageUrl.ts @@ -2,11 +2,11 @@ import { cdnUrl, thumborUrl } from './config' const URL_CONFIG = { cdnUrl: cdnUrl, - thumborUrl: thumborUrl, - audioSubfolder: 'audio', - imageSubfolder: 'image', - productionFolder: 'production/', -} + thumborUrl: `${thumborUrl}/unsafe/`, + audioSubfolder: "audio", + imageSubfolder: "image", + productionFolder: "production/", +}; const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac']) From fc056f14b853900bad49c8190d4a74047cf621cb Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Wed, 24 Apr 2024 10:23:42 +0300 Subject: [PATCH 3/3] fix biome --- src/utils/getImageUrl.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/getImageUrl.ts b/src/utils/getImageUrl.ts index d820bfae..e13872cc 100644 --- a/src/utils/getImageUrl.ts +++ b/src/utils/getImageUrl.ts @@ -3,10 +3,10 @@ import { cdnUrl, thumborUrl } from './config' const URL_CONFIG = { cdnUrl: cdnUrl, thumborUrl: `${thumborUrl}/unsafe/`, - audioSubfolder: "audio", - imageSubfolder: "image", - productionFolder: "production/", -}; + audioSubfolder: 'audio', + imageSubfolder: 'image', + productionFolder: 'production/', +} const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac'])