ice.fo.utils
Version:
42 lines (29 loc) • 960 B
JavaScript
import _isString from 'lodash/isString';
export default function getFileUrl(data, { $config }) {
if (!data) {
return '';
}
if (data.image) {
data = data.image;
}
let url = data.storePath || data.imageUrl || data;
if (!url || !_isString(url)) { return ''; }
if (url.includes(';base64')) {
return url;
}
if (url.startsWith('http') || url.startsWith('/')) {
if (url.lastIndexOf('http') != 0) {
const apiDomain = `${$config.fileUrl}/`;
url = url.replace(apiDomain, '');
}
const extension = url.includes('.') && url.substring(url.indexOf('.') + 1);
if (extension && extension.includes('/')) {
url = url.substring(0, url.lastIndexOf('/'));
}
return url;
}
// const protocol = 'http://'
const protocol = '//'; // use the current open web browser protocol
const result = url.startsWith($config.fileHost) ? `${protocol}${url}` : `${$config.fileUrl}/${url}`;
return result;
}