Download src/components/UploadArea.tsx from mozolla/telev: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/mozolla/telev/resolve/main/src/components/UploadArea.tsx
- Command line
-
hf download hf://spaces/mozolla/telev/src/components/UploadArea.tsx
-
curl -L -o UploadArea.tsx https://huggingface.co/spaces/mozolla/telev/resolve/main/src/components/UploadArea.tsx
5.29 kB
| import React from "react"; | |
| // βββ Directory drop helper ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async function* getFilesFromDataTransferItems(items: DataTransferItemList, prefix = ""): AsyncGenerator<File & { __folderPath?: string }> { | |
| for (let i = 0; i < items.length; i++) { | |
| const item = items[i]; | |
| if (item.kind === "file") { | |
| const entry = item.webkitGetAsEntry?.(); | |
| if (entry?.isFile) { | |
| const file = item.getAsFile(); | |
| if (file) { | |
| if (prefix) { | |
| (file as any).__folderPath = prefix; | |
| } | |
| yield file as File & { __folderPath?: string }; | |
| } | |
| } else if (entry?.isDirectory) { | |
| yield* readDirectoryRecursive(entry as any, entry.name); | |
| } | |
| } | |
| } | |
| } | |
| async function* readDirectoryRecursive(dir: any, prefix = ""): AsyncGenerator<File & { __folderPath?: string }> { | |
| const reader = dir.createReader(); | |
| const entries = await new Promise<any[]>((resolve, reject) => { | |
| reader.readEntries(resolve, reject); | |
| }); | |
| for (const entry of entries) { | |
| const newPrefix = prefix ? `${prefix}/${entry.name}` : entry.name; | |
| if (entry.isFile) { | |
| const file = await new Promise<File>((resolve, reject) => { | |
| (entry as any).file(resolve, reject); | |
| }); | |
| (file as any).__folderPath = prefix; | |
| yield file as File & { __folderPath?: string }; | |
| } else if (entry.isDirectory) { | |
| yield* readDirectoryRecursive(entry, newPrefix); | |
| } | |
| } | |
| } | |
| // βββ UploadArea βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export function UploadArea({ | |
| onFilesSelected, | |
| busy, | |
| dragOver, | |
| onDragOver, | |
| onDragLeave, | |
| uploadInputRef, | |
| uploadFolderRef, | |
| }: { | |
| onFilesSelected: (files: FileList | (File & { __folderPath?: string })[]) => void; | |
| busy: boolean; | |
| dragOver: boolean; | |
| onDragOver: (e: React.DragEvent) => void; | |
| onDragLeave: (e: React.DragEvent) => void; | |
| uploadInputRef: React.RefObject<HTMLInputElement | null>; | |
| uploadFolderRef: React.RefObject<HTMLInputElement | null>; | |
| }) { | |
| const handleDrop = async (e: React.DragEvent) => { | |
| e.preventDefault(); | |
| onDragLeave(e); | |
| if (e.dataTransfer.items?.length) { | |
| try { | |
| const allFiles: (File & { __folderPath?: string })[] = []; | |
| for await (const file of getFilesFromDataTransferItems(e.dataTransfer.items)) { | |
| allFiles.push(file); | |
| } | |
| if (allFiles.length > 0) { | |
| onFilesSelected(allFiles); | |
| } else if (e.dataTransfer.files) { | |
| onFilesSelected(e.dataTransfer.files); | |
| } | |
| } catch (err) { | |
| console.error("Error reading drag-and-drop items:", err); | |
| if (e.dataTransfer.files) { | |
| onFilesSelected(e.dataTransfer.files); | |
| } | |
| } | |
| } else if (e.dataTransfer.files) { | |
| onFilesSelected(e.dataTransfer.files); | |
| } | |
| }; | |
| return ( | |
| <div | |
| onDragOver={onDragOver} | |
| onDragLeave={onDragLeave} | |
| onDrop={handleDrop} | |
| className={`rounded-lg border-2 border-dashed p-4 text-center transition-colors ${ | |
| dragOver | |
| ? "border-violet-400 bg-violet-50" | |
| : "border-slate-300 bg-slate-50 hover:border-slate-400" | |
| } ${busy ? "opacity-60 cursor-not-allowed" : "cursor-pointer"}`} | |
| > | |
| <div className="text-sm font-semibold text-slate-600 mb-2"> | |
| {busy ? "Uploading..." : "Drop files or folders here"} | |
| </div> | |
| <div className="flex gap-2 justify-center flex-wrap"> | |
| <button | |
| type="button" | |
| onClick={() => uploadInputRef.current?.click()} | |
| disabled={busy} | |
| className="rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors" | |
| > | |
| Select Files | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => uploadFolderRef.current?.click()} | |
| disabled={busy} | |
| className="rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed transition-colors" | |
| > | |
| Select Folder | |
| </button> | |
| </div> | |
| <input | |
| ref={uploadInputRef} | |
| type="file" | |
| multiple | |
| onChange={(e) => e.target.files && onFilesSelected(e.target.files)} | |
| className="hidden" | |
| disabled={busy} | |
| /> | |
| <input | |
| {...({ | |
| ref: uploadFolderRef, | |
| type: "file", | |
| webkitdirectory: "true", | |
| mozdirectory: "true", | |
| multiple: true, | |
| onChange: (e) => e.target.files && onFilesSelected(e.target.files), | |
| className: "hidden", | |
| disabled: busy, | |
| } as React.InputHTMLAttributes<HTMLInputElement> & { | |
| webkitdirectory?: string; | |
| mozdirectory?: string; | |
| })} | |
| /> | |
| </div> | |
| ); | |
| } |