2023-10-27 18:50:13 +00:00
|
|
|
import { thumborUrl } from './config'
|
|
|
|
|
2024-01-06 23:52:24 +00:00
|
|
|
const thumborPrefix = `${thumborUrl}/unsafe/`
|
|
|
|
|
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}/`
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getImageUrl = (src: string, options: { width?: number; height?: number } = {}) => {
|
|
|
|
const sizeUrlPart = getSizeUrlPart(options)
|
|
|
|
|
2023-11-01 11:17:31 +00:00
|
|
|
if (src.startsWith(thumborPrefix)) {
|
|
|
|
const thumborKey = src.replace(thumborPrefix, '')
|
2023-10-27 18:50:13 +00:00
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${thumborKey}`
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:17:31 +00:00
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${src}`
|
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)}')/`
|
|
|
|
|
|
|
|
if (src.startsWith(thumborPrefix)) {
|
|
|
|
const thumborKey = src.replace(thumborPrefix, '')
|
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${thumborKey}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${thumborUrl}/unsafe/${sizeUrlPart}${filtersPart}${src}`
|
|
|
|
}
|