| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import ImageProvider, { ProvidedImage } from "./ImageProvider" | 
					
						
							|  |  |  | import BaseUIElement from "../../UI/BaseUIElement" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							|  |  |  | import Constants from "../../Models/Constants" | 
					
						
							|  |  |  | import { LicenseInfo } from "./LicenseInfo" | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | export class Imgur extends ImageProvider { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public static readonly defaultValuePrefix = ["https://i.imgur.com"] | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public static readonly singleton = new Imgur() | 
					
						
							|  |  |  |     public readonly defaultKeyPrefixes: string[] = ["image"] | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     private constructor() { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super() | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static uploadMultiple( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         title: string, | 
					
						
							|  |  |  |         description: string, | 
					
						
							|  |  |  |         blobs: FileList, | 
					
						
							|  |  |  |         handleSuccessfullUpload: (imageURL: string) => Promise<void>, | 
					
						
							|  |  |  |         allDone: () => void, | 
					
						
							|  |  |  |         onFail: (reason: string) => void, | 
					
						
							|  |  |  |         offset: number = 0 | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         if (blobs.length == offset) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             allDone() | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const blob = blobs.item(offset) | 
					
						
							|  |  |  |         const self = this | 
					
						
							|  |  |  |         this.uploadImage( | 
					
						
							|  |  |  |             title, | 
					
						
							|  |  |  |             description, | 
					
						
							|  |  |  |             blob, | 
					
						
							| 
									
										
										
										
											2022-09-02 12:39:40 +02:00
										 |  |  |             async (imageUrl) => { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 await handleSuccessfullUpload(imageUrl) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |                 self.uploadMultiple( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     title, | 
					
						
							|  |  |  |                     description, | 
					
						
							|  |  |  |                     blobs, | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |                     handleSuccessfullUpload, | 
					
						
							|  |  |  |                     allDone, | 
					
						
							| 
									
										
										
										
											2020-07-25 12:32:30 +02:00
										 |  |  |                     onFail, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     offset + 1 | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2020-07-21 22:50:54 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             onFail | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     static uploadImage( | 
					
						
							|  |  |  |         title: string, | 
					
						
							|  |  |  |         description: string, | 
					
						
							|  |  |  |         blob: File, | 
					
						
							|  |  |  |         handleSuccessfullUpload: (imageURL: string) => Promise<void>, | 
					
						
							|  |  |  |         onFail: (reason: string) => void | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         const apiUrl = "https://api.imgur.com/3/image" | 
					
						
							|  |  |  |         const apiKey = Constants.ImgurApiKey | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const formData = new FormData() | 
					
						
							|  |  |  |         formData.append("image", blob) | 
					
						
							|  |  |  |         formData.append("title", title) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |         formData.append("description", description) | 
					
						
							| 
									
										
										
										
											2022-09-03 20:53:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const settings: RequestInit = { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             method: "POST", | 
					
						
							| 
									
										
										
										
											2022-09-03 20:53:06 +02:00
										 |  |  |             body: formData, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             redirect: "follow", | 
					
						
							| 
									
										
										
										
											2022-09-03 20:53:06 +02:00
										 |  |  |             headers: new Headers({ | 
					
						
							|  |  |  |                 Authorization: `Client-ID ${apiKey}`, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 Accept: "application/json", | 
					
						
							| 
									
										
										
										
											2022-09-03 20:53:06 +02:00
										 |  |  |             }), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Response contains stringified JSON
 | 
					
						
							|  |  |  |         // Image URL available at response.data.link
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         fetch(apiUrl, settings) | 
					
						
							|  |  |  |             .then(async function (response) { | 
					
						
							|  |  |  |                 const content = await response.json() | 
					
						
							|  |  |  |                 await handleSuccessfullUpload(content.data.link) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             .catch((reason) => { | 
					
						
							|  |  |  |                 console.log("Uploading to IMGUR failed", reason) | 
					
						
							|  |  |  |                 // @ts-ignore
 | 
					
						
							|  |  |  |                 onFail(reason) | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2020-06-25 03:39:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     SourceIcon(): BaseUIElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return undefined | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public async ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         if (Imgur.defaultValuePrefix.some((prefix) => value.startsWith(prefix))) { | 
					
						
							|  |  |  |             return [ | 
					
						
							|  |  |  |                 Promise.resolve({ | 
					
						
							|  |  |  |                     url: value, | 
					
						
							|  |  |  |                     key: key, | 
					
						
							|  |  |  |                     provider: this, | 
					
						
							|  |  |  |                 }), | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 00:51:53 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Download the attribution from attribution | 
					
						
							| 
									
										
										
										
											2022-09-03 20:53:06 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-06-13 00:51:53 +02:00
										 |  |  |      * const data = {"data":{"id":"I9t6B7B","title":"Station Knokke","description":"author:Pieter Vander Vennet\r\nlicense:CC-BY 4.0\r\nosmid:node\/9812712386","datetime":1655052078,"type":"image\/jpeg","animated":false,"width":2400,"height":1795,"size":910872,"views":2,"bandwidth":1821744,"vote":null,"favorite":false,"nsfw":false,"section":null,"account_url":null,"account_id":null,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"link":"https:\/\/i.imgur.com\/I9t6B7B.jpg","ad_config":{"safeFlags":["not_in_gallery","share"],"highRiskFlags":[],"unsafeFlags":["sixth_mod_unsafe"],"wallUnsafeFlags":[],"showsAds":false,"showAdLevel":1}},"success":true,"status":200} | 
					
						
							|  |  |  |      * Utils.injectJsonDownloadForTests("https://api.imgur.com/3/image/E0RuAK3", data) | 
					
						
							|  |  |  |      * const licenseInfo = await Imgur.singleton.DownloadAttribution("https://i.imgur.com/E0RuAK3.jpg") | 
					
						
							|  |  |  |      * const expected = new LicenseInfo() | 
					
						
							|  |  |  |      * expected.licenseShortName = "CC-BY 4.0" | 
					
						
							|  |  |  |      * expected.artist = "Pieter Vander Vennet" | 
					
						
							|  |  |  |      * licenseInfo // => expected
 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public async DownloadAttribution(url: string): Promise<LicenseInfo> { | 
					
						
							|  |  |  |         const hash = url.substr("https://i.imgur.com/".length).split(".jpg")[0] | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const apiUrl = "https://api.imgur.com/3/image/" + hash | 
					
						
							|  |  |  |         const response = await Utils.downloadJsonCached(apiUrl, 365 * 24 * 60 * 60, { | 
					
						
							|  |  |  |             Authorization: "Client-ID " + Constants.ImgurApiKey, | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const descr: string = response.data.description ?? "" | 
					
						
							|  |  |  |         const data: any = {} | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         for (const tag of descr.split("\n")) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const kv = tag.split(":") | 
					
						
							|  |  |  |             const k = kv[0] | 
					
						
							|  |  |  |             data[k] = kv[1]?.replace(/\r/g, "") | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const licenseInfo = new LicenseInfo() | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         licenseInfo.licenseShortName = data.license | 
					
						
							|  |  |  |         licenseInfo.artist = data.author | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         return licenseInfo | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |