MCPcopy Index your code
hub / github.com/cursor/community-plugins / handleFile

Function handleFile

apps/cursor/src/components/upload-logo.tsx:22–68  ·  view source on GitHub ↗
(file: File)

Source from the content-addressed store, hash-verified

20 const fileInputRef = useRef<HTMLInputElement>(null);
21
22 const handleFile = async (file: File) => {
23 if (!file.type.startsWith("image/")) {
24 return;
25 }
26
27 const MAX_FILE_SIZE = 1024 * 1024; // 1MB in bytes
28 if (file.size > MAX_FILE_SIZE) {
29 return;
30 }
31
32 try {
33 // Create preview
34 const reader = new FileReader();
35 reader.onload = (e) => {
36 const dataUrl = e.target?.result as string;
37 setPreview(dataUrl);
38 };
39 reader.readAsDataURL(file);
40
41 // Upload to Supabase Storage
42 const supabase = createClient();
43 const fileExt = file.name.split(".").pop();
44 const fileName = `${Math.random().toString(36).substring(2)}.${fileExt}`;
45
46 const { error } = await supabase.storage
47 .from("avatars")
48 .upload(`${prefix}/${fileName}`, file, {
49 cacheControl: "3600",
50 upsert: false,
51 });
52
53 if (error) {
54 throw error;
55 }
56
57 // Get public URL
58 const {
59 data: { publicUrl },
60 } = supabase.storage
61 .from("avatars")
62 .getPublicUrl(`${prefix}/${fileName}`);
63
64 onUpload?.(publicUrl);
65 } catch (error) {
66 console.error("Error uploading file:", error);
67 }
68 };
69
70 const handleDrop = (e: DragEvent<HTMLButtonElement>) => {
71 e.preventDefault();

Callers 2

handleDropFunction · 0.85
handleFileInputFunction · 0.85

Calls 1

createClientFunction · 0.90

Tested by

no test coverage detected