| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | import { UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import { Imgur } from "./Imgur" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class ImgurUploader { | 
					
						
							| 
									
										
										
										
											2021-06-14 19:21:33 +02:00
										 |  |  |     public readonly queue: UIEventSource<string[]> = new UIEventSource<string[]>([]) | 
					
						
							|  |  |  |     public readonly failed: UIEventSource<string[]> = new UIEventSource<string[]>([]) | 
					
						
							|  |  |  |     public readonly success: UIEventSource<string[]> = new UIEventSource<string[]>([]) | 
					
						
							| 
									
										
										
										
											2021-10-09 23:51:04 +02:00
										 |  |  |     public maxFileSizeInMegabytes = 10 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:39:40 +02:00
										 |  |  |     private readonly _handleSuccessUrl: (string) => Promise<void> | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:39:40 +02:00
										 |  |  |     constructor(handleSuccessUrl: (string) => Promise<void>) { | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |         this._handleSuccessUrl = handleSuccessUrl | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 23:51:04 +02:00
										 |  |  |     public uploadMany(title: string, description: string, files: FileList): void { | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |         for (let i = 0; i < files.length; i++) { | 
					
						
							|  |  |  |             this.queue.data.push(files.item(i).name) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.queue.ping() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  |         this.queue.setData([...self.queue.data]) | 
					
						
							|  |  |  |         Imgur.uploadMultiple( | 
					
						
							|  |  |  |             title, | 
					
						
							|  |  |  |             description, | 
					
						
							|  |  |  |             files, | 
					
						
							| 
									
										
										
										
											2022-09-02 12:39:40 +02:00
										 |  |  |             async function (url) { | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |                 console.log("File saved at", url) | 
					
						
							| 
									
										
										
										
											2021-10-20 00:09:40 +02:00
										 |  |  |                 self.success.data.push(url) | 
					
						
							|  |  |  |                 self.success.ping() | 
					
						
							| 
									
										
										
										
											2022-09-02 12:39:40 +02:00
										 |  |  |                 await self._handleSuccessUrl(url) | 
					
						
							| 
									
										
										
										
											2021-06-11 22:51:45 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             function () { | 
					
						
							|  |  |  |                 console.log("All uploads completed") | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             function (failReason) { | 
					
						
							|  |  |  |                 console.log("Upload failed due to ", failReason) | 
					
						
							|  |  |  |                 self.failed.setData([...self.failed.data, failReason]) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |