कस्टम सत्यापन
अपने अपलोड के लिए परिष्कृत सत्यापन नियम बनाएं।
बुनियादी कस्टम नियम
import { useDropup } from '@samithahansaka/dropup';
function CustomValidationUploader() {
const { files, getDropProps, getInputProps } = useDropup({
customRules: [
// नियम 1: फ़ाइलनाम जांचें
(file) => {
if (file.name.includes(' ')) {
return 'फ़ाइलनाम में रिक्त स्थान नहीं हो सकते';
}
return true;
},
// नियम 2: एक्सटेंशन जांचें
(file) => {
const ext = file.name.split('.').pop()?.toLowerCase();
const blocked = ['exe', 'bat', 'cmd', 'sh'];
if (ext && blocked.includes(ext)) {
return 'निष्पादन योग्य फ़ाइलों की अनुमति नहीं है';
}
return true;
},
],
onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
alert(`${file.name}:\n${errors.join('\n')}`);
});
},
});
return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>यहाँ फ़ाइलें छोड़ें (नामों में रिक्त स्थान नहीं, कोई निष्पादन योग्य नहीं)</p>
</div>
);
}
एसिंक्रोनस सत्यापन
import { useDropup } from '@samithahansaka/dropup';
function AsyncValidationUploader() {
const { files, getDropProps, getInputProps } = useDropup({
customRules: [
// सर्वर पर डुप्लिकेट की जांच करें
async (file) => {
const hash = await calculateHash(file);
const response = await fetch('/api/check-duplicate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hash, filename: file.name }),
});
const { exists } = await response.json();
if (exists) {
return 'यह फ़ाइल पहले से अपलोड की जा चुकी है';
}
return true;
},
// फ़ाइल सामग्री सत्यापित करें
async (file) => {
if (file.type === 'application/json') {
const text = await file.text();
try {
JSON.parse(text);
return true;
} catch {
return 'अमान्य JSON फ़ाइल';
}
}
return true;
},
],
});
return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>यहाँ फ़ाइलें छोड़ें (सर्वर के विरुद्ध सत्यापित)</p>
</div>
);
}
// फ़ाइल हैश गणना करने के लिए सहायक फ़ंक्शन
async function calculateHash(file: File): Promise<string> {
const buffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
छवि सामग्री सत्यापन
import { useDropup } from '@samithahansaka/dropup';
function ImageContentValidator() {
const { files, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
customRules: [
// वास्तविक छवि आयाम जांचें
async (file) => {
if (!file.type.startsWith('image/')) return true;
const dimensions = await getImageDimensions(file);
if (dimensions.width < 200 || dimensions.height < 200) {
return 'छवि कम से कम 200x200 पिक्सेल की होनी चाहिए';
}
if (dimensions.width > 4000 || dimensions.height > 4000) {
return 'छवि 4000x4000 पिक्सेल से अधिक नहीं हो सकती';
}
return true;
},
// पहलू अनुपात जांचें
async (file) => {
if (!file.type.startsWith('image/')) return true;
const { width, height } = await getImageDimensions(file);
const ratio = width / height;
// लगभग वर्गाकार छवियों की आवश्यकता है (0.8 से 1.2 अनुपात)
if (ratio < 0.8 || ratio > 1.2) {
return 'छवि लगभग वर्गाकार होनी चाहिए (पहलू अनुपात 0.8-1.2)';
}
return true;
},
],
});
return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>वर्गाकार छवियाँ छोड़ें (न्यूनतम 200px, अधिकतम 4000px)</p>
<div style={styles.gallery}>
{files.map(file => (
<img
key={file.id}
src={file.preview}
alt=""
style={styles.preview}
/>
))}
</div>
</div>
);
}
// छवि आयाम प्राप्त करने के लिए सहायक
function getImageDimensions(file: File): Promise<{ width: number; height: number }> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
resolve({ width: img.width, height: img.height });
URL.revokeObjectURL(img.src);
};
img.onerror = reject;
img.src = URL.createObjectURL(file);
});
}
const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
gallery: {
display: 'flex',
gap: 8,
marginTop: 16,
justifyContent: 'center',
},
preview: {
width: 80,
height: 80,
objectFit: 'cover' as const,
borderRadius: 4,
},
};
निर्भरताओं के साथ सत्यापन
import { useDropup } from '@samithahansaka/dropup';
import { useState } from 'react';
function ConditionalValidation() {
const [category, setCategory] = useState('image');
const { files, getDropProps, getInputProps } = useDropup({
// श्रेणी के आधार पर गतिशील स्वीकृति
accept: category === 'image'
? 'image/*'
: category === 'document'
? '.pdf,.doc,.docx'
: '*/*',
customRules: [
(file) => {
// श्रेणी-विशिष्ट सत्यापन
if (category === 'image') {
if (!file.type.startsWith('image/')) {
return 'कृपया एक छवि फ़ाइल अपलोड करें';
}
if (file.size > 5 * 1024 * 1024) {
return 'छवियाँ 5MB से कम होनी चाहिए';
}
}
if (category === 'document') {
if (file.size > 10 * 1024 * 1024) {
return 'दस्तावेज़ 10MB से कम होने चाहिए';
}
}
return true;
},
],
});
return (
<div>
<div style={{ marginBottom: 20 }}>
<label>
<input
type="radio"
value="image"
checked={category === 'image'}
onChange={() => setCategory('image')}
/>
छवियाँ (अधिकतम 5MB)
</label>
<label style={{ marginLeft: 20 }}>
<input
type="radio"
value="document"
checked={category === 'document'}
onChange={() => setCategory('document')}
/>
दस्तावेज़ (अधिकतम 10MB)
</label>
</div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>
यहाँ {category === 'image' ? 'छवियाँ' : 'दस्तावेज़'} छोड़ें
</p>
</div>
</div>
);
}
वायरस/मैलवेयर स्कैनिंग
import { useDropup } from '@samithahansaka/dropup';
function MalwareScanUploader() {
const { files, state, actions, getDropProps, getInputProps } = useDropup({
customRules: [
// बाहरी सेवा के साथ फ़ाइल स्कैन करें
async (file) => {
// वायरस स्कैनिंग सेवा पर अपलोड करें
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('/api/scan', {
method: 'POST',
body: formData,
});
const { safe, threat } = await response.json();
if (!safe) {
return `मैलवेयर का पता चला: ${threat}`;
}
return true;
} catch (error) {
return 'फ़ाइल स्कैन नहीं कर सका। कृपया पुनः प्रयास करें।';
}
},
],
onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
console.error(`${file.name} अस्वीकृत:`, errors);
});
},
});
return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>अपलोड से पहले फ़ाइलें मैलवेयर के लिए स्कैन की जाती हैं</p>
</div>
);
}
पूर्व-निर्मित सत्यापन नियम
import { useDropup, commonRules } from '@samithahansaka/dropup';
function PrebuiltRulesUploader() {
const { files, getDropProps, getInputProps } = useDropup({
customRules: [
// अंतर्निहित नियम
commonRules.noExecutables, // .exe, .bat, आदि को ब्लॉक करें
commonRules.noHiddenFiles, // . से शुरू होने वाली फ़ाइलों को ब्लॉक करें
commonRules.maxFilenameLength(50), // अधिकतम 50 वर्ण
commonRules.allowedExtensions(['.jpg', '.png', '.pdf']),
// कस्टम नियमों के साथ संयोजित करें
(file) => {
if (file.name.toLowerCase().includes('temp')) {
return 'अस्थायी फ़ाइलों की अनुमति नहीं है';
}
return true;
},
],
});
return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>सख्त फ़ाइल सत्यापन सक्षम</p>
</div>
);
}
सत्यापन त्रुटि प्रदर्शन
import { useDropup } from '@samithahansaka/dropup';
import { useState } from 'react';
type ValidationErrorType = { file: File; errors: string[] };
function ValidationErrorDisplay() {
const [validationErrors, setValidationErrors] = useState<ValidationErrorType[]>([]);
const { files, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
maxSize: 5 * 1024 * 1024,
maxFiles: 3,
customRules: [
(file) => {
if (file.name.length > 50) {
return 'फ़ाइलनाम बहुत लंबा है (अधिकतम 50 वर्ण)';
}
return true;
},
],
onValidationError: (errors) => {
setValidationErrors(errors);
// 5 सेकंड के बाद साफ़ करें
setTimeout(() => setValidationErrors([]), 5000);
},
});
return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>3 छवियाँ तक छोड़ें (प्रत्येक अधिकतम 5MB)</p>
</div>
{/* सत्यापन त्रुटियाँ */}
{validationErrors.length > 0 && (
<div style={styles.errorContainer}>
<h4 style={styles.errorTitle}>कुछ फ़ाइलें अस्वीकार कर दी गईं:</h4>
{validationErrors.map(({ file, errors }, index) => (
<div key={index} style={styles.errorItem}>
<strong>{file.name}</strong>
<ul style={styles.errorList}>
{errors.map((error, i) => (
<li key={i}>{error}</li>
))}
</ul>
</div>
))}
</div>
)}
{/* स्वीकृत फ़ाइलें */}
{files.length > 0 && (
<div style={styles.acceptedFiles}>
<h4>स्वीकृत फ़ाइलें:</h4>
{files.map(file => (
<div key={file.id}>{file.name}</div>
))}
</div>
)}
</div>
);
}
const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
errorContainer: {
marginTop: 16,
padding: 16,
backgroundColor: '#ffebee',
borderRadius: 8,
border: '1px solid #f44336',
},
errorTitle: {
color: '#c62828',
margin: '0 0 12px',
},
errorItem: {
marginBottom: 8,
},
errorList: {
margin: '4px 0 0',
paddingLeft: 20,
color: '#c62828',
},
acceptedFiles: {
marginTop: 16,
padding: 16,
backgroundColor: '#e8f5e9',
borderRadius: 8,
},
};