|
| 1 | +import React from "react"; |
| 2 | +import bytes from "bytes"; |
| 3 | +import minimatch from "minimatch"; |
| 4 | +import { readFileContent } from "./utils/readFileContent"; |
| 5 | +import { generateId } from "./utils/generateId"; |
| 6 | + |
| 7 | +export type SelectedFile = { |
| 8 | + id: string; |
| 9 | + name: string; |
| 10 | + type: string; |
| 11 | + size: number; |
| 12 | + src: { |
| 13 | + file: File; |
| 14 | + base64: string | null; |
| 15 | + }; |
| 16 | +}; |
| 17 | + |
| 18 | +export type FileError = { |
| 19 | + id: string; |
| 20 | + type: |
| 21 | + | "unsupportedFileType" |
| 22 | + | "maxSizeExceeded" |
| 23 | + | "multipleMaxSizeExceeded" |
| 24 | + | "multipleMaxCountExceeded" |
| 25 | + | "multipleNotAllowed"; |
| 26 | + index?: number; |
| 27 | + file?: SelectedFile | File; |
| 28 | + multipleFileSize?: number; |
| 29 | + multipleMaxSize?: number; |
| 30 | + multipleMaxCount?: number; |
| 31 | + multipleCount?: number; |
| 32 | +}; |
| 33 | + |
| 34 | +export type BrowseFilesParams = { |
| 35 | + onSuccess?: (files: SelectedFile[]) => void; |
| 36 | + onError?: (errors: FileError[], files: SelectedFile[]) => void; |
| 37 | +}; |
| 38 | + |
| 39 | +export type RenderPropParams = { |
| 40 | + browseFiles: (params: BrowseFilesParams) => void; |
| 41 | + getDropZoneProps: (additionalProps: any) => any; |
| 42 | + getLabelProps: (additionalProps: any) => any; |
| 43 | + validateFiles: (files: SelectedFile[] | File[]) => FileError[]; |
| 44 | +}; |
| 45 | + |
| 46 | +export type FilesRules = { |
| 47 | + accept: string[]; |
| 48 | + multiple: boolean; |
| 49 | + maxSize: string; |
| 50 | + multipleMaxSize: string; |
| 51 | + multipleMaxCount: number | null; |
| 52 | + convertToBase64: boolean; |
| 53 | + onSuccess?: (files: SelectedFile[]) => void; |
| 54 | + onError?: (errors: FileError[], files: SelectedFile[]) => void; |
| 55 | +}; |
| 56 | + |
| 57 | +export type Props = FilesRules & { |
| 58 | + children: (params: RenderPropParams) => React.ReactNode; |
| 59 | + id?: string; |
| 60 | +}; |
| 61 | + |
| 62 | +export class Files extends React.Component<Props> { |
| 63 | + static defaultProps = { |
| 64 | + accept: [], |
| 65 | + multiple: false, |
| 66 | + maxSize: "2mb", |
| 67 | + multipleMaxSize: "10mb", |
| 68 | + multipleMaxCount: null, |
| 69 | + convertToBase64: false |
| 70 | + }; |
| 71 | + |
| 72 | + input: HTMLInputElement | null = null; |
| 73 | + browseFilesPassedParams: BrowseFilesParams | null = null; |
| 74 | + id: string = generateId(); |
| 75 | + |
| 76 | + validateFiles = (files: SelectedFile[] | File[]): FileError[] => { |
| 77 | + const { multiple, multipleMaxSize, multipleMaxCount, accept, maxSize } = this.props; |
| 78 | + |
| 79 | + const errors: FileError[] = []; |
| 80 | + let multipleFileSize = 0; |
| 81 | + |
| 82 | + if (!multiple && files.length > 1) { |
| 83 | + errors.push({ |
| 84 | + id: generateId(), |
| 85 | + type: "multipleNotAllowed" |
| 86 | + }); |
| 87 | + |
| 88 | + return errors; |
| 89 | + } |
| 90 | + |
| 91 | + for (let index = 0; index < files.length; index++) { |
| 92 | + const file = files[index]; |
| 93 | + |
| 94 | + if ( |
| 95 | + Array.isArray(accept) && |
| 96 | + accept.length && |
| 97 | + !accept.some(type => minimatch(file.type, type)) |
| 98 | + ) { |
| 99 | + errors.push({ |
| 100 | + id: generateId(), |
| 101 | + index, |
| 102 | + file, |
| 103 | + type: "unsupportedFileType" |
| 104 | + }); |
| 105 | + } else if (maxSize) { |
| 106 | + if (file.size > bytes(maxSize)) { |
| 107 | + errors.push({ |
| 108 | + id: generateId(), |
| 109 | + index, |
| 110 | + file, |
| 111 | + type: "maxSizeExceeded" |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (multiple) { |
| 117 | + multipleFileSize += file.size; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (multiple) { |
| 122 | + if (multipleMaxSize && multipleFileSize > bytes(multipleMaxSize)) { |
| 123 | + errors.push({ |
| 124 | + id: generateId(), |
| 125 | + type: "multipleMaxSizeExceeded", |
| 126 | + multipleFileSize, |
| 127 | + multipleMaxSize: bytes(multipleMaxSize) |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + if (multipleMaxCount && files.length > multipleMaxCount) { |
| 132 | + errors.push({ |
| 133 | + id: generateId(), |
| 134 | + type: "multipleMaxCountExceeded", |
| 135 | + multipleCount: files.length, |
| 136 | + multipleMaxCount |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return errors; |
| 142 | + }; |
| 143 | + |
| 144 | + processSelectedFiles = async (eventFiles: Array<File>) => { |
| 145 | + if (eventFiles.length === 0) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + const { convertToBase64, onSuccess, onError } = this.props; |
| 150 | + const { browseFilesPassedParams } = this; |
| 151 | + const callbacks = { |
| 152 | + onSuccess, |
| 153 | + onError |
| 154 | + }; |
| 155 | + |
| 156 | + if (browseFilesPassedParams && browseFilesPassedParams.onSuccess) { |
| 157 | + callbacks.onSuccess = browseFilesPassedParams.onSuccess; |
| 158 | + } |
| 159 | + |
| 160 | + if (browseFilesPassedParams && browseFilesPassedParams.onError) { |
| 161 | + callbacks.onError = browseFilesPassedParams.onError; |
| 162 | + } |
| 163 | + |
| 164 | + const files: SelectedFile[] = [...eventFiles].map(file => { |
| 165 | + return { |
| 166 | + id: generateId(), |
| 167 | + name: file.name, |
| 168 | + type: file.type, |
| 169 | + size: file.size, |
| 170 | + src: { |
| 171 | + file, |
| 172 | + base64: null |
| 173 | + } |
| 174 | + }; |
| 175 | + }); |
| 176 | + |
| 177 | + const errors = this.validateFiles(files); |
| 178 | + |
| 179 | + if (errors.length) { |
| 180 | + callbacks.onError && callbacks.onError(errors, files); |
| 181 | + } else { |
| 182 | + if (convertToBase64) { |
| 183 | + for (let i = 0; i < files.length; i++) { |
| 184 | + const file = files[i].src.file; |
| 185 | + files[i].src.base64 = await readFileContent(file); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + callbacks.onSuccess && callbacks.onSuccess(files); |
| 190 | + } |
| 191 | + |
| 192 | + // Reset the browseFiles arguments. |
| 193 | + if (this.input) { |
| 194 | + this.input.value = ""; |
| 195 | + } |
| 196 | + this.browseFilesPassedParams = null; |
| 197 | + }; |
| 198 | + |
| 199 | + /** |
| 200 | + * Extracted into a separate method just for testing purposes. |
| 201 | + */ |
| 202 | + onDropFilesHandler = async ({ e, onSuccess, onError }: any) => { |
| 203 | + this.browseFilesPassedParams = { onSuccess, onError }; |
| 204 | + e.dataTransfer && |
| 205 | + e.dataTransfer.files && |
| 206 | + (await this.processSelectedFiles(e.dataTransfer.files)); |
| 207 | + }; |
| 208 | + |
| 209 | + /** |
| 210 | + * Extracted into a separate method just for testing purposes. |
| 211 | + */ |
| 212 | + browseFilesHandler = ({ onSuccess, onError }: any) => { |
| 213 | + this.browseFilesPassedParams = { onSuccess, onError }; |
| 214 | + this.input && this.input.click(); |
| 215 | + }; |
| 216 | + |
| 217 | + override render() { |
| 218 | + const { multiple, accept, id } = this.props; |
| 219 | + return ( |
| 220 | + <React.Fragment> |
| 221 | + {this.props.children({ |
| 222 | + getLabelProps: (props: any) => { |
| 223 | + return { |
| 224 | + ...props, |
| 225 | + htmlFor: id || this.id |
| 226 | + }; |
| 227 | + }, |
| 228 | + validateFiles: this.validateFiles, |
| 229 | + browseFiles: ({ onSuccess, onError }: BrowseFilesParams = {}) => { |
| 230 | + this.browseFilesHandler({ onSuccess, onError }); |
| 231 | + }, |
| 232 | + getDropZoneProps: ({ |
| 233 | + onSuccess, |
| 234 | + onError, |
| 235 | + onDragOver, |
| 236 | + onDrop, |
| 237 | + ...rest |
| 238 | + }: any = {}) => { |
| 239 | + return { |
| 240 | + ...rest, |
| 241 | + onDragOver: (e: DragEvent) => { |
| 242 | + e.preventDefault(); |
| 243 | + typeof onDragOver === "function" && onDragOver(); |
| 244 | + }, |
| 245 | + onDrop: async (e: DragEvent) => { |
| 246 | + e.preventDefault(); |
| 247 | + typeof onDrop === "function" && onDrop(); |
| 248 | + this.onDropFilesHandler({ e, onSuccess, onError }); |
| 249 | + } |
| 250 | + }; |
| 251 | + } |
| 252 | + })} |
| 253 | + |
| 254 | + <input |
| 255 | + id={id || this.id} |
| 256 | + ref={ref => { |
| 257 | + if (ref) { |
| 258 | + this.input = ref; |
| 259 | + } |
| 260 | + }} |
| 261 | + accept={accept.join(",")} |
| 262 | + style={{ display: "none" }} |
| 263 | + type="file" |
| 264 | + multiple={multiple} |
| 265 | + onChange={e => |
| 266 | + this.processSelectedFiles((e.target.files as any as Array<File>) ?? []) |
| 267 | + } |
| 268 | + /> |
| 269 | + </React.Fragment> |
| 270 | + ); |
| 271 | + } |
| 272 | +} |
0 commit comments