UNPKG

ice.fo.utils

Version:

35 lines (26 loc) 1.04 kB
import getFilePathFromStorePath from './getFilePathFromStorePath'; /** * width: 300 * height: 0 * http://localhost:8080/productImage/file/202110/06/e84739fa-757e-4f93-a0e1-2e2780cbe5db.png/09.sample_thum01.png * -> * http://localhost:8080/image/productImage/file/202110/06/300/0/e84739fa-757e-4f93-a0e1-2e2780cbe5db.png */ export default function getResizeImagePath({ value, width = 0, height = 0 }) { if (!value) { return value; } const filePath = getFilePathFromStorePath(value); const supportExtensions = ['.png', '.jpg', '.jpeg']; if (!supportExtensions.some((i) => filePath.includes(i) || filePath.includes(i.toUpperCase()))) { return filePath; } const domainRegex = /(^([a-z]{3,5})?(:)?\/\/)((.*?)\/)/g; const dateRegex = /\/\d{6}\/\d{2}\//g; const [domain] = filePath.match(domainRegex) || []; const [date] = filePath.match(dateRegex) || []; if (!domain || !date) { return filePath; } return filePath.replace(domain, `${domain}image/`).replace(date, `${date}${width}/${height}/`); }