| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | import { upload } from '@/service/base' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ImageUploadParams = { | 
					
						
							|  |  |  |   file: File | 
					
						
							|  |  |  |   onProgressCallback: (progress: number) => void | 
					
						
							|  |  |  |   onSuccessCallback: (res: { id: string }) => void | 
					
						
							|  |  |  |   onErrorCallback: () => void | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | type ImageUpload = (v: ImageUploadParams, isPublic?: boolean, url?: string) => void | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | export const imageUpload: ImageUpload = ({ | 
					
						
							|  |  |  |   file, | 
					
						
							|  |  |  |   onProgressCallback, | 
					
						
							|  |  |  |   onSuccessCallback, | 
					
						
							|  |  |  |   onErrorCallback, | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | }, isPublic, url) => { | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |   const formData = new FormData() | 
					
						
							|  |  |  |   formData.append('file', file) | 
					
						
							|  |  |  |   const onProgress = (e: ProgressEvent) => { | 
					
						
							|  |  |  |     if (e.lengthComputable) { | 
					
						
							|  |  |  |       const percent = Math.floor(e.loaded / e.total * 100) | 
					
						
							|  |  |  |       onProgressCallback(percent) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   upload({ | 
					
						
							|  |  |  |     xhr: new XMLHttpRequest(), | 
					
						
							|  |  |  |     data: formData, | 
					
						
							|  |  |  |     onprogress: onProgress, | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  |   }, isPublic, url) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |     .then((res: { id: string }) => { | 
					
						
							|  |  |  |       onSuccessCallback(res) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .catch(() => { | 
					
						
							|  |  |  |       onErrorCallback() | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | } |