| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | import ImageProvider, { ProvidedImage } from "./ImageProvider" | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | import BaseUIElement from "../../UI/BaseUIElement" | 
					
						
							|  |  |  | import Svg from "../../Svg" | 
					
						
							| 
									
										
										
										
											2021-07-03 22:24:12 +02:00
										 |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | import { LicenseInfo } from "./LicenseInfo" | 
					
						
							|  |  |  | import Constants from "../../Models/Constants" | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | export class Mapillary extends ImageProvider { | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     public static readonly singleton = new Mapillary() | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |     private static readonly valuePrefix = "https://a.mapillary.com" | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |     public static readonly valuePrefixes = [ | 
					
						
							|  |  |  |         Mapillary.valuePrefix, | 
					
						
							|  |  |  |         "http://mapillary.com", | 
					
						
							|  |  |  |         "https://mapillary.com", | 
					
						
							|  |  |  |         "http://www.mapillary.com", | 
					
						
							|  |  |  |         "https://www.mapillary.com", | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     defaultKeyPrefixes = ["mapillary", "image"] | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-06 12:41:24 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Indicates that this is the same URL | 
					
						
							|  |  |  |      * Ignores 'stp' parameter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * const a = "https://scontent-bru2-1.xx.fbcdn.net/m1/v/t6/An8xm5SGLt20ETziNqzhhBd8b8S5GHLiIu8N6BbyqHFohFAQoaJJPG8i5yQiSwjYmEqXSfVeoCmpiyBJICEkQK98JOB21kkJoBS8VdhYa-Ty93lBnznQyesJBtKcb32foGut2Hgt10hEMWJbE3dDgA?stp=s1024x768&ccb=10-5&oh=00_AT-ZGTXHzihoaQYBILmEiAEKR64z_IWiTlcAYq_D7Ka0-Q&oe=6278C456&_nc_sid=122ab1" | 
					
						
							|  |  |  |      * const b = "https://scontent-bru2-1.xx.fbcdn.net/m1/v/t6/An8xm5SGLt20ETziNqzhhBd8b8S5GHLiIu8N6BbyqHFohFAQoaJJPG8i5yQiSwjYmEqXSfVeoCmpiyBJICEkQK98JOB21kkJoBS8VdhYa-Ty93lBnznQyesJBtKcb32foGut2Hgt10hEMWJbE3dDgA?stp=s256x192&ccb=10-5&oh=00_AT9BZ1Rpc9zbY_uNu92A_4gj1joiy1b6VtgtLIu_7wh9Bg&oe=6278C456&_nc_sid=122ab1" | 
					
						
							|  |  |  |      * Mapillary.sameUrl(a, b) => true | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     static sameUrl(a: string, b: string): boolean { | 
					
						
							|  |  |  |         if (a === b) { | 
					
						
							|  |  |  |             return true | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             const aUrl = new URL(a) | 
					
						
							|  |  |  |             const bUrl = new URL(b) | 
					
						
							|  |  |  |             if (aUrl.host !== bUrl.host || aUrl.pathname !== bUrl.pathname) { | 
					
						
							|  |  |  |                 return false | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             let allSame = true | 
					
						
							|  |  |  |             aUrl.searchParams.forEach((value, key) => { | 
					
						
							|  |  |  |                 if (key === "stp") { | 
					
						
							|  |  |  |                     // This is the key indicating the image size on mapillary; we ignore it
 | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (value !== bUrl.searchParams.get(key)) { | 
					
						
							|  |  |  |                     allSame = false | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             return allSame | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-06-03 01:33:41 +02:00
										 |  |  |             console.debug("Could not compare ", a, "and", b, "due to", e) | 
					
						
							| 
									
										
										
										
											2022-05-06 12:41:24 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the correct key for API v4.0 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static ExtractKeyFromURL(value: string): number { | 
					
						
							|  |  |  |         let key: string | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         const newApiFormat = value.match(/https?:\/\/www.mapillary.com\/app\/\?pKey=([0-9]*)/) | 
					
						
							|  |  |  |         if (newApiFormat !== null) { | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |             key = newApiFormat[1] | 
					
						
							|  |  |  |         } else if (value.startsWith(Mapillary.valuePrefix)) { | 
					
						
							|  |  |  |             key = value.substring(0, value.lastIndexOf("?")).substring(value.lastIndexOf("/") + 1) | 
					
						
							| 
									
										
										
										
											2022-05-06 12:41:24 +02:00
										 |  |  |         } else if (value.match("[0-9]*")) { | 
					
						
							|  |  |  |             key = value | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         const keyAsNumber = Number(key) | 
					
						
							|  |  |  |         if (!isNaN(keyAsNumber)) { | 
					
						
							|  |  |  |             return keyAsNumber | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         return undefined | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SourceIcon(backlinkSource?: string): BaseUIElement { | 
					
						
							|  |  |  |         return Svg.mapillary_svg() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |     async ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> { | 
					
						
							| 
									
										
										
										
											2021-10-18 22:17:41 +02:00
										 |  |  |         return [this.PrepareUrlAsync(key, value)] | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 00:51:53 +02:00
										 |  |  |     public async DownloadAttribution(url: string): Promise<LicenseInfo> { | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         const license = new LicenseInfo() | 
					
						
							|  |  |  |         license.artist = "Contributor name unavailable" | 
					
						
							|  |  |  |         license.license = "CC BY-SA 4.0" | 
					
						
							|  |  |  |         // license.license = "Creative Commons Attribution-ShareAlike 4.0 International License";
 | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         license.attributionRequired = true | 
					
						
							|  |  |  |         return license | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private async PrepareUrlAsync(key: string, value: string): Promise<ProvidedImage> { | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         const mapillaryId = Mapillary.ExtractKeyFromURL(value) | 
					
						
							|  |  |  |         if (mapillaryId === undefined) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         const metadataUrl = | 
					
						
							|  |  |  |             "https://graph.mapillary.com/" + | 
					
						
							|  |  |  |             mapillaryId + | 
					
						
							|  |  |  |             "?fields=thumb_1024_url&&access_token=" + | 
					
						
							|  |  |  |             Constants.mapillary_client_token_v4 | 
					
						
							| 
									
										
										
										
											2022-06-13 00:51:53 +02:00
										 |  |  |         const response = await Utils.downloadJsonCached(metadataUrl, 60 * 60) | 
					
						
							| 
									
										
										
										
											2021-11-11 17:35:24 +01:00
										 |  |  |         const url = <string>response["thumb_1024_url"] | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             url: url, | 
					
						
							|  |  |  |             provider: this, | 
					
						
							|  |  |  |             key: key, | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | } |