smart-dropzone-react
Version:
🚀 A production-ready React dropzone component with smart defaults, drag & drop reordering, chunked uploads, resume functionality, and comprehensive provider support (Cloudinary, AWS S3, Supabase)
2 lines • 4.62 kB
JavaScript
import {a as a$2}from'./chunk-DQ5A5ZMM.js';import {a as a$1}from'./chunk-74G6TQLM.js';import {a}from'./chunk-5XGN7UAV.js';var u=class t{constructor(e={}){a(this,"options");this.options={maxFiles:e.maxFiles||10,maxFileSize:e.maxFileSize||a$1.DEFAULT_MAX_SIZE,allowedTypes:e.allowedTypes||["image/*","application/pdf","text/*"]};}async processFiles(e){let s=a$2.validateFileList(e);if(!s.isValid)return {success:false,errors:s.errors};if(!e.length)return {success:true,files:[]};if(e.length>this.options.maxFiles)return {success:false,errors:[`Maximum ${this.options.maxFiles} files allowed. Received ${e.length} files.`]};let r=[],i=[];for(let n of e)try{let o=await this.processFile(n);o.success&&o.file?r.push(o.file):o.error&&i.push(o.error);}catch(o){let d=o instanceof Error?o.message:"Unknown error occurred";i.push(`Failed to process ${n.name}: ${d}`);}return {success:r.length>0,files:r,errors:i.length>0?i:void 0}}async processFile(e){let s=this.validateFile(e);if(!s.valid)return {success:false,error:s.error||"Validation failed"};let r;if(t.isImage(e.type))try{r=await this.generatePreview(e);}catch{}return {success:true,file:{id:this.generateFileId(e),file:e,name:e.name,size:e.size,type:e.type,preview:r,progress:0,status:"pending"}}}validateFile(e){let s=[];if(e.size>this.options.maxFileSize){let r=Math.round(this.options.maxFileSize/a$1.BYTES_PER_MB),i=Math.round(e.size/a$1.BYTES_PER_MB);return {valid:false,error:`File "${e.name}" is too large. Maximum size: ${r}MB, Received: ${i}MB`}}return this.options.allowedTypes.length>0&&!this.options.allowedTypes.some(i=>{if(i==="*")return true;if(i.endsWith("/*")){let n=i.split("/")[0];return e.type.startsWith(n+"/")}return i.startsWith(".")?e.name.toLowerCase().endsWith(i.toLowerCase()):e.type===i})?{valid:false,error:`File type "${e.type}" not allowed. Allowed types: ${this.options.allowedTypes.join(", ")}`}:(e.size===0&&s.push("File appears to be empty"),e.name.length>255&&s.push("Filename is very long"),{valid:true,warnings:s.length>0?s:void 0})}generatePreview(e){return new Promise((s,r)=>{if(!t.isImage(e.type)){r(new Error("Preview only available for image files"));return}let i=new FileReader;i.onload=()=>{let n=i.result;typeof n=="string"?s(n):r(new Error("Failed to generate preview"));},i.onerror=()=>r(new Error("Failed to read file for preview")),i.readAsDataURL(e);})}generateFileId(e){let s=Date.now(),r=Math.random().toString(36).substring(2,15),i=e.name.split(".").pop()||"";return `${e.name.replace(/[^a-zA-Z0-9]/g,"_").substring(0,20)}_${s}_${r}${i?`.${i}`:""}`}static formatFileSize(e){if(e===0)return "0 Bytes";let s=a$1.BYTES_PER_KB,r=["Bytes","KB","MB","GB","TB"],i=Math.floor(Math.log(e)/Math.log(s));return i>=r.length?`${(e/Math.pow(s,r.length-1)).toFixed(2)} TB`:`${parseFloat((e/Math.pow(s,i)).toFixed(2))} ${r[i]}`}static getFileIcon(e){return t.isImage(e)?"\u{1F5BC}\uFE0F":t.isVideo(e)?"\u{1F3A5}":t.isAudio(e)?"\u{1F3B5}":t.isDocument(e)?"\u{1F4C4}":t.isArchive(e)?"\u{1F4E6}":t.isCode(e)?"\u{1F4BB}":"\u{1F4C1}"}static isImage(e){return e.startsWith("image/")}static isVideo(e){return e.startsWith("video/")}static isAudio(e){return e.startsWith("audio/")}static isDocument(e){return e.startsWith("text/")&&!e.includes("javascript")&&!e.includes("typescript")||e==="application/pdf"||e.includes("document")||e.includes("word")||e.includes("excel")||e.includes("powerpoint")||e.includes("presentation")}static isArchive(e){return e.includes("zip")||e.includes("rar")||e.includes("tar")||e.includes("7z")||e.includes("gzip")||e.includes("bzip2")}static isCode(e){return e.includes("javascript")||e.includes("typescript")||e.includes("python")||e.includes("java")||e.includes("c++")||e.includes("c#")||e.includes("php")||e.includes("ruby")||e.includes("go")||e.includes("rust")||e.includes("swift")||e.includes("kotlin")||e.includes("x-python")||e.includes("x-java")||e.includes("x-c++")||e.includes("x-c#")||e.includes("x-php")||e.includes("x-ruby")||e.includes("x-go")||e.includes("x-rust")||e.includes("x-swift")||e.includes("x-kotlin")}static getFileCategory(e){return t.isImage(e)?"image":t.isVideo(e)?"video":t.isAudio(e)?"audio":t.isDocument(e)?"document":t.isArchive(e)?"archive":t.isCode(e)?"code":"other"}static getFileExtension(e){let s=e.split(".");return s.length>1&&s.pop()?.toLowerCase()||""}static isPotentiallyDangerous(e){let s=["exe","bat","cmd","com","pif","scr","vbs","js","jar","msi","ps1","psm1","psd1","ps1xml","psc1","psc2","py","pyc","pyw"],r=t.getFileExtension(e);return s.includes(r)}};export{u as a};//# sourceMappingURL=chunk-MV3OE3QV.js.map
//# sourceMappingURL=chunk-MV3OE3QV.js.map