2024-06-24 17:50:27 +00:00
|
|
|
import { cdnUrl, thumborUrl } from '../config/config'
|
2024-01-06 23:52:24 +00:00
|
|
|
|
2024-04-24 06:40:37 +00:00
|
|
|
const URL_CONFIG = {
|
|
|
|
cdnUrl: cdnUrl,
|
2024-04-24 07:22:05 +00:00
|
|
|
thumborUrl: `${thumborUrl}/unsafe/`,
|
2024-04-24 07:23:42 +00:00
|
|
|
audioSubfolder: 'audio',
|
|
|
|
imageSubfolder: 'image',
|
2024-06-26 08:22:05 +00:00
|
|
|
productionFolder: 'production/'
|
2024-04-24 07:23:42 +00:00
|
|
|
}
|
2023-10-27 18:50:13 +00:00
|
|
|
|
2024-04-24 06:40:37 +00:00
|
|
|
const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac'])
|
|
|
|
|
|
|
|
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() || ''
|
2023-10-27 18:50:13 +00:00
|
|
|
|
2024-04-24 06:40:37 +00:00
|
|
|
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}/` : ''
|
2023-10-27 18:50:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 08:05:36 +00:00
|
|
|
export const getImageUrl = (
|
|
|
|
src: string,
|
2024-06-26 08:22:05 +00:00
|
|
|
options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {}
|
2024-04-24 06:40:37 +00:00
|
|
|
): string => {
|
2024-03-25 13:07:14 +00:00
|
|
|
if (!src.includes('discours.io') && src.includes('http')) {
|
|
|
|
return src
|
|
|
|
}
|
2024-04-24 06:40:37 +00:00
|
|
|
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}${URL_CONFIG.productionFolder}${subfolder}/${filename}`
|
2023-10-27 18:50:13 +00:00
|
|
|
}
|
2024-01-06 23:52:24 +00:00
|
|
|
|
|
|
|
export const getOpenGraphImageUrl = (
|
|
|
|
src: string,
|
|
|
|
options: {
|
|
|
|
topic: string
|
|
|
|
title: string
|
|
|
|
author: string
|
|
|
|
width?: number
|
|
|
|
height?: number
|
2024-06-26 08:22:05 +00:00
|
|
|
}
|
2024-04-24 06:40:37 +00:00
|
|
|
): string => {
|
|
|
|
const sizeUrlPart = buildSizePart(options.width, options.height)
|
2024-01-06 23:52:24 +00:00
|
|
|
const filtersPart = `filters:discourstext('${encodeURIComponent(options.topic)}','${encodeURIComponent(
|
2024-06-26 08:22:05 +00:00
|
|
|
options.author
|
2024-01-06 23:52:24 +00:00
|
|
|
)}','${encodeURIComponent(options.title)}')/`
|
|
|
|
|
2024-04-24 06:40:37 +00:00
|
|
|
if (src.startsWith(URL_CONFIG.thumborUrl)) {
|
|
|
|
const thumborKey = src.replace(URL_CONFIG.thumborUrl, '')
|
|
|
|
return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${thumborKey}`
|
2024-01-06 23:52:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 06:40:37 +00:00
|
|
|
return `${URL_CONFIG.thumborUrl}${sizeUrlPart}${filtersPart}${src}`
|
2024-01-06 23:52:24 +00:00
|
|
|
}
|