2024-01-22 20:38:31 +00:00
|
|
|
import { thumborUrl, cdnUrl } from './config'
|
2024-01-06 23:52:24 +00:00
|
|
|
|
2023-10-27 18:50:13 +00:00
|
|
|
const getSizeUrlPart = (options: { width?: number; height?: number } = {}) => {
|
|
|
|
const widthString = options.width ? options.width.toString() : ''
|
|
|
|
const heightString = options.height ? options.height.toString() : ''
|
|
|
|
|
|
|
|
if (!widthString && !heightString) {
|
|
|
|
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-22 20:38:31 +00:00
|
|
|
const isAudio = src.toLowerCase().split('.')[-1] in ['wav', 'mp3', 'ogg', 'aif', 'flac']
|
|
|
|
const base = isAudio ? cdnUrl : thumborUrl
|
|
|
|
const sizeUrlPart = isAudio ? '' : getSizeUrlPart(options)
|
2024-01-23 15:04:12 +00:00
|
|
|
|
|
|
|
// Используйте новую переменную вместо переназначения параметра
|
|
|
|
let modifiedSrc = src
|
|
|
|
.replaceAll(thumborUrl + '/', '')
|
|
|
|
.replaceAll(cdnUrl + '/', '')
|
|
|
|
.replaceAll('/unsafe', '')
|
2024-01-21 08:05:36 +00:00
|
|
|
|
|
|
|
if (options.noSizeUrlPart) {
|
|
|
|
modifiedSrc = modifiedSrc.replace(/\d+x.*?\//, '')
|
|
|
|
}
|
|
|
|
|
2024-01-22 20:38:31 +00:00
|
|
|
return `${base}/unsafe/${sizeUrlPart}${modifiedSrc}`
|
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)) {
|
|
|
|
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}`
|
|
|
|
}
|