70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
|
|
const CDN_HOST = 'img.yidaima.cn'
|
||
|
|
|
||
|
|
// JPEG is used deliberately here. Some older iOS/WeChat image decoders cannot
|
||
|
|
// display the WebP thumbnails returned by Qiniu.
|
||
|
|
const ARTICLE_CARD = 'imageView2/1/w/600/h/375/format/jpg/q/75/ignore-error/1'
|
||
|
|
const RESOURCE_CARD = 'imageView2/1/w/500/h/400/format/jpg/q/75/ignore-error/1'
|
||
|
|
|
||
|
|
function original(url) {
|
||
|
|
if (!url || typeof url !== 'string') {
|
||
|
|
return url || ''
|
||
|
|
}
|
||
|
|
|
||
|
|
const value = url.trim()
|
||
|
|
const httpPrefix = `http://${CDN_HOST}/`
|
||
|
|
if (value.indexOf(httpPrefix) === 0) {
|
||
|
|
return `https://${value.substring('http://'.length)}`
|
||
|
|
}
|
||
|
|
if (value.indexOf(`//${CDN_HOST}/`) === 0) {
|
||
|
|
return `https:${value}`
|
||
|
|
}
|
||
|
|
return value
|
||
|
|
}
|
||
|
|
|
||
|
|
function qiniuThumbnail(url, operation) {
|
||
|
|
const normalized = original(url)
|
||
|
|
if (!normalized || normalized.indexOf(`https://${CDN_HOST}/`) !== 0) {
|
||
|
|
return normalized
|
||
|
|
}
|
||
|
|
// Keep signed or already processed URLs intact.
|
||
|
|
if (normalized.indexOf('?') >= 0) {
|
||
|
|
return normalized
|
||
|
|
}
|
||
|
|
return `${normalized}?${operation}`
|
||
|
|
}
|
||
|
|
|
||
|
|
function compatibleThumbnail(url) {
|
||
|
|
const normalized = original(url)
|
||
|
|
if (!normalized) {
|
||
|
|
return ''
|
||
|
|
}
|
||
|
|
|
||
|
|
// The API may still return a cached showImgThumb containing format/webp.
|
||
|
|
// Rewrite that Qiniu operation on the client while the backend rolls over.
|
||
|
|
if (normalized.indexOf(`https://${CDN_HOST}/`) === 0) {
|
||
|
|
return normalized.replace(/\/format\/webp(?=\/|$)/i, '/format/jpg')
|
||
|
|
}
|
||
|
|
return normalized
|
||
|
|
}
|
||
|
|
|
||
|
|
function articleThumbnail(image) {
|
||
|
|
if (image && typeof image === 'object') {
|
||
|
|
return compatibleThumbnail(image.showImgThumb) || qiniuThumbnail(image.showImg, ARTICLE_CARD)
|
||
|
|
}
|
||
|
|
return qiniuThumbnail(image, ARTICLE_CARD)
|
||
|
|
}
|
||
|
|
|
||
|
|
function resourceThumbnail(image) {
|
||
|
|
if (image && typeof image === 'object') {
|
||
|
|
return compatibleThumbnail(image.showImgThumb) || qiniuThumbnail(image.showImg, RESOURCE_CARD)
|
||
|
|
}
|
||
|
|
return qiniuThumbnail(image, RESOURCE_CARD)
|
||
|
|
}
|
||
|
|
|
||
|
|
export default {
|
||
|
|
original,
|
||
|
|
compatibleThumbnail,
|
||
|
|
articleThumbnail,
|
||
|
|
resourceThumbnail
|
||
|
|
}
|