2024-02-04 11:25:21 +00:00
|
|
|
import { cdnUrl, thumborUrl } from './config'
|
2024-01-06 23:52:24 +00:00
|
|
|
|
2024-01-23 15:21:37 +00:00
|
|
|
const getSizeUrlPart = (options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {}) => {
|
2023-10-27 18:50:13 +00:00
|
|
|
const widthString = options.width ? options.width.toString() : ''
|
|
|
|
const heightString = options.height ? options.height.toString() : ''
|
|
|
|
|
2024-01-23 15:21:37 +00:00
|
|
|
if ((!widthString && !heightString) || options.noSizeUrlPart) {
|
2023-10-27 18:50:13 +00:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${widthString}x${heightString}/`
|
|
|
|
}
|
|
|
|
|
2024-01-21 08:05:36 +00:00
|
|
|
export const getImageUrl = (
|
|
|
|
src: string,
|
|
|
|
options: { width?: number; height?: number; noSizeUrlPart?: boolean } = {},
|
|
|
|
) => {
|
2024-01-31 12:34:15 +00:00
|
|
|
const filename = src?.split('/').pop()
|
2024-01-23 15:31:59 +00:00
|
|
|
const isAudio = src.toLowerCase().split('.').pop() in ['wav', 'mp3', 'ogg', 'aif', 'flac']
|
2024-01-23 15:21:37 +00:00
|
|
|
const base = isAudio ? cdnUrl : `${thumborUrl}/unsafe/`
|
2024-01-22 20:38:31 +00:00
|
|
|
const sizeUrlPart = isAudio ? '' : getSizeUrlPart(options)
|
2024-01-23 15:04:12 +00:00
|
|
|
|
2024-01-23 15:43:18 +00:00
|
|
|
return `${base}${sizeUrlPart}production/${isAudio ? 'audio' : 'image'}/${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
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
const sizeUrlPart = getSizeUrlPart(options)
|
|
|
|
|
|
|
|
const filtersPart = `filters:discourstext('${encodeURIComponent(options.topic)}','${encodeURIComponent(
|
|
|
|
options.author,
|
|
|
|
)}','${encodeURIComponent(options.title)}')/`
|
|
|
|
|
2024-01-22 20:38:31 +00:00
|
|
|
if (src.startsWith(thumborUrl)) {
|
2024-02-04 09:03:15 +00:00
|
|
|
const thumborKey = src.replace(`${thumborUrl}/unsafe`, '')
|
2024-01-06 23:52:24 +00:00
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${thumborKey}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${src}`
|
|
|
|
}
|