2023-10-27 18:50:13 +00:00
|
|
|
import { thumborUrl } from './config'
|
|
|
|
|
|
|
|
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
|
|
|
const thumborPrefix = `${thumborUrl}/unsafe/`
|
2023-10-27 18:50:13 +00:00
|
|
|
|
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
|
|
|
}
|