Merge pull request #445 from Discours/feature/getImage-refactoring
getImageUrl.ts refactoring
This commit is contained in:
commit
036443d7ee
|
@ -1,31 +1,41 @@
|
||||||
import { cdnUrl, thumborUrl } from './config'
|
import { cdnUrl, thumborUrl } from './config'
|
||||||
|
|
||||||
const getSizeUrlPart = (options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {}) => {
|
const URL_CONFIG = {
|
||||||
const widthString = options.width ? options.width.toString() : ''
|
cdnUrl: cdnUrl,
|
||||||
const heightString = options.height ? options.height.toString() : ''
|
thumborUrl: `${thumborUrl}/unsafe/`,
|
||||||
|
audioSubfolder: 'audio',
|
||||||
|
imageSubfolder: 'image',
|
||||||
|
productionFolder: 'production/',
|
||||||
|
}
|
||||||
|
|
||||||
if (!(widthString || heightString) || options.noSizeUrlPart) {
|
const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac'])
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = (
|
export const getImageUrl = (
|
||||||
src: string,
|
src: string,
|
||||||
options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {},
|
options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {},
|
||||||
) => {
|
): string => {
|
||||||
if (!src.includes('discours.io') && src.includes('http')) {
|
if (!src.includes('discours.io') && src.includes('http')) {
|
||||||
return src
|
return src
|
||||||
}
|
}
|
||||||
const filename = src.toLowerCase().split('/').pop()
|
const filename = getLastSegment(src)
|
||||||
const ext = filename.split('.').pop()
|
const base = isAudioFile(filename) ? URL_CONFIG.cdnUrl : URL_CONFIG.thumborUrl
|
||||||
const isAudio = ext in ['wav', 'mp3', 'ogg', 'aif', 'flac']
|
const suffix = options.noSizeUrlPart ? '' : buildSizePart(options.width, options.height)
|
||||||
const base = isAudio ? cdnUrl : `${thumborUrl}/unsafe/`
|
const subfolder = isAudioFile(filename) ? URL_CONFIG.audioSubfolder : URL_CONFIG.imageSubfolder
|
||||||
const suffix = isAudio || options.noSizeUrlPart ? '' : getSizeUrlPart(options)
|
|
||||||
const subfolder = isAudio ? 'audio' : 'image'
|
|
||||||
|
|
||||||
return `${base}${suffix}production/${subfolder}/${filename}`
|
return `${base}${suffix}${URL_CONFIG.productionFolder}${subfolder}/${filename}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getOpenGraphImageUrl = (
|
export const getOpenGraphImageUrl = (
|
||||||
|
@ -37,17 +47,16 @@ export const getOpenGraphImageUrl = (
|
||||||
width?: number
|
width?: number
|
||||||
height?: number
|
height?: number
|
||||||
},
|
},
|
||||||
) => {
|
): string => {
|
||||||
const sizeUrlPart = getSizeUrlPart(options)
|
const sizeUrlPart = buildSizePart(options.width, options.height)
|
||||||
|
|
||||||
const filtersPart = `filters:discourstext('${encodeURIComponent(options.topic)}','${encodeURIComponent(
|
const filtersPart = `filters:discourstext('${encodeURIComponent(options.topic)}','${encodeURIComponent(
|
||||||
options.author,
|
options.author,
|
||||||
)}','${encodeURIComponent(options.title)}')/`
|
)}','${encodeURIComponent(options.title)}')/`
|
||||||
|
|
||||||
if (src.startsWith(thumborUrl)) {
|
if (src.startsWith(URL_CONFIG.thumborUrl)) {
|
||||||
const thumborKey = src.replace(`${thumborUrl}/unsafe`, '')
|
const thumborKey = src.replace(URL_CONFIG.thumborUrl, '')
|
||||||
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${thumborKey}`
|
return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${thumborKey}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${src}`
|
return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${src}`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user