refactor: DRY up validate image file

* Also, allows more types of image files
This commit is contained in:
Hussain Nagaria 2025-06-08 12:30:51 +05:30
parent cf91f3f72a
commit c6ad10857a
7 changed files with 19 additions and 52 deletions

View File

@ -1,7 +1,7 @@
<template>
<FileUploader
@success="(file) => setUserImage(file.file_url)"
:validateFile="validateFile"
:validateFile="validateIsImageFile"
>
<template v-slot="{ file, progress, error, uploading, openFileSelector }">
<div class="flex flex-col items-center">
@ -48,17 +48,11 @@
</template>
<script setup>
import { FileUploader } from 'frappe-ui'
import { validateIsImageFile } from '@/utils';
const profile = defineModel()
function setUserImage(url) {
profile.value.user_image = url
}
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg'].includes(extn)) {
return 'Only PNG and JPG images are allowed'
}
}
</script>

View File

@ -17,7 +17,7 @@
<div class="border-b">
<FileUploader
@success="changeContactImage"
:validateFile="validateFile"
:validateFile="validateIsImageFile"
>
<template #default="{ openFileSelector, error }">
<div class="flex flex-col items-start justify-start gap-4 p-5">
@ -186,7 +186,7 @@ import CameraIcon from '@/components/Icons/CameraIcon.vue'
import DealsIcon from '@/components/Icons/DealsIcon.vue'
import DealsListView from '@/components/ListViews/DealsListView.vue'
import AddressModal from '@/components/Modals/AddressModal.vue'
import { formatDate, timeAgo } from '@/utils'
import { formatDate, timeAgo, validateIsImageFile } from '@/utils'
import { getView } from '@/utils/view'
import { getSettings } from '@/stores/settings'
import { getMeta } from '@/stores/meta'
@ -298,13 +298,6 @@ usePageMeta(() => {
}
})
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg', 'jpeg'].includes(extn)) {
return __('Only PNG and JPG images are allowed')
}
}
async function changeContactImage(file) {
await call('frappe.client.set_value', {
doctype: 'Contact',

View File

@ -63,7 +63,7 @@
</div>
<FileUploader
@success="(file) => updateField('image', file.file_url)"
:validateFile="validateFile"
:validateFile="validateIsImageFile"
>
<template #default="{ openFileSelector, error }">
<div class="flex items-center justify-start gap-5 border-b p-5">
@ -347,6 +347,7 @@ import {
setupAssignees,
setupCustomizations,
copyToClipboard,
validateIsImageFile
} from '@/utils'
import { getView } from '@/utils/view'
import { getSettings } from '@/stores/settings'
@ -578,13 +579,6 @@ watch(tabs, (value) => {
}
})
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg', 'jpeg'].includes(extn)) {
return __('Only PNG and JPG images are allowed')
}
}
const sections = createResource({
url: 'crm.fcrm.doctype.crm_fields_layout.crm_fields_layout.get_sidepanel_sections',
cache: ['sidePanelSections', 'CRM Lead'],

View File

@ -11,7 +11,7 @@
</header>
</LayoutHeader>
<div v-if="contact.data" class="flex flex-col h-full overflow-hidden">
<FileUploader @success="changeContactImage" :validateFile="validateFile">
<FileUploader @success="changeContactImage" :validateFile="validateIsImageFile">
<template #default="{ openFileSelector, error }">
<div class="flex flex-col items-start justify-start gap-4 p-4">
<div class="flex gap-4 items-center">
@ -169,7 +169,7 @@ import CameraIcon from '@/components/Icons/CameraIcon.vue'
import DealsIcon from '@/components/Icons/DealsIcon.vue'
import DealsListView from '@/components/ListViews/DealsListView.vue'
import AddressModal from '@/components/Modals/AddressModal.vue'
import { formatDate, timeAgo } from '@/utils'
import { formatDate, timeAgo, validateIsImageFile } from '@/utils'
import { getView } from '@/utils/view'
import { getSettings } from '@/stores/settings'
import { getMeta } from '@/stores/meta'
@ -269,13 +269,6 @@ usePageMeta(() => {
}
})
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg', 'jpeg'].includes(extn)) {
return __('Only PNG and JPG images are allowed')
}
}
async function changeContactImage(file) {
await call('frappe.client.set_value', {
doctype: 'Contact',

View File

@ -13,7 +13,7 @@
<div v-if="organization.doc" class="flex flex-col h-full overflow-hidden">
<FileUploader
@success="changeOrganizationImage"
:validateFile="validateFile"
:validateFile="validateIsImageFile"
>
<template #default="{ openFileSelector, error }">
<div class="flex flex-col items-start justify-start gap-4 p-4">
@ -165,7 +165,7 @@ import { globalStore } from '@/stores/global'
import { usersStore } from '@/stores/users'
import { statusesStore } from '@/stores/statuses'
import { getView } from '@/utils/view'
import { formatDate, timeAgo } from '@/utils'
import { formatDate, timeAgo, validateIsImageFile } from '@/utils'
import {
Breadcrumbs,
Avatar,
@ -252,13 +252,6 @@ usePageMeta(() => {
}
})
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg', 'jpeg'].includes(extn)) {
return __('Only PNG and JPG images are allowed')
}
}
async function changeOrganizationImage(file) {
await call('frappe.client.set_value', {
doctype: 'CRM Organization',

View File

@ -17,7 +17,7 @@
<div class="border-b">
<FileUploader
@success="changeOrganizationImage"
:validateFile="validateFile"
:validateFile="validateIsImageFile"
>
<template #default="{ openFileSelector, error }">
<div class="flex flex-col items-start justify-start gap-4 p-5">
@ -192,7 +192,7 @@ import { globalStore } from '@/stores/global'
import { usersStore } from '@/stores/users'
import { statusesStore } from '@/stores/statuses'
import { getView } from '@/utils/view'
import { formatDate, timeAgo } from '@/utils'
import { formatDate, timeAgo, validateIsImageFile } from '@/utils'
import {
Tooltip,
Breadcrumbs,
@ -294,13 +294,6 @@ usePageMeta(() => {
}
})
function validateFile(file) {
let extn = file.name.split('.').pop().toLowerCase()
if (!['png', 'jpg', 'jpeg'].includes(extn)) {
return __('Only PNG and JPG images are allowed')
}
}
async function changeOrganizationImage(file) {
await call('frappe.client.set_value', {
doctype: 'CRM Organization',

View File

@ -395,6 +395,13 @@ export function isImage(extention) {
)
}
export function validateIsImageFile(file) {
const extn = file.name.split('.').pop().toLowerCase()
if (!isImage(extn)) {
return __('Only image files are allowed')
}
}
export function getRandom(len = 4) {
let text = ''
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'