2019-02-15 15:39:39 +05:30
|
|
|
export default (buttonSelector, fileSelector) => {
|
|
|
|
const btn = document.querySelector(buttonSelector);
|
|
|
|
const fileInput = document.querySelector(fileSelector);
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
if (!btn || !fileInput) return;
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
const form = btn.closest('form');
|
|
|
|
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
fileInput.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
fileInput.addEventListener('change', () => {
|
|
|
|
form.querySelector('.js-filename').textContent = fileInput.value.replace(/^.*[\\\/]/, ''); // eslint-disable-line no-useless-escape
|
|
|
|
});
|
|
|
|
};
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
export const getFilename = (file) => {
|
|
|
|
let fileName;
|
|
|
|
if (file) {
|
|
|
|
fileName = file.name;
|
2020-04-22 19:07:51 +05:30
|
|
|
}
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
return fileName;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const validateImageName = (file) => {
|
|
|
|
const fileName = file.name ? file.name : 'image.png';
|
|
|
|
const legalImageRegex = /^[\w.\-+]+\.(png|jpg|jpeg|gif|bmp|tiff|ico|webp)$/;
|
|
|
|
return legalImageRegex.test(fileName) ? fileName : 'image.png';
|
2020-04-22 19:07:51 +05:30
|
|
|
};
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
export const validateFileFromAllowList = (fileName, allowList) => {
|
|
|
|
const parts = fileName.split('.');
|
|
|
|
const ext = `.${parts[parts.length - 1]}`;
|
|
|
|
|
|
|
|
return allowList.includes(ext);
|
|
|
|
};
|