| 
									
										
										
										
											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 {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | 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"; | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  | import {fail} from "assert"; | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  | export class Mapillary extends ImageProvider { | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |     defaultKeyPrefixes = ["mapillary","image"] | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							|  |  |  |     public static readonly valuePrefixes = [Mapillary.valuePrefix, "http://mapillary.com","https://mapillary.com"] | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |     private static ExtractKeyFromURL(value: string, failIfNoMath = false): { | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         key: string, | 
					
						
							|  |  |  |         isApiv4?: boolean | 
					
						
							|  |  |  |     } { | 
					
						
							| 
									
										
										
										
											2021-10-18 22:17:41 +02:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |         if (value.startsWith(Mapillary.valuePrefix)) { | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |             const key = value.substring(0, value.lastIndexOf("?")).substring(value.lastIndexOf("/") + 1) | 
					
						
							|  |  |  |             return {key: key, isApiv4: !isNaN(Number(key))}; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         const newApiFormat = value.match(/https?:\/\/www.mapillary.com\/app\/\?pKey=([0-9]*)/) | 
					
						
							|  |  |  |         if (newApiFormat !== null) { | 
					
						
							|  |  |  |             return {key: newApiFormat[1], isApiv4: true} | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const mapview = value.match(/https?:\/\/www.mapillary.com\/map\/im\/(.*)/) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         if (mapview !== null) { | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |             const key = mapview[1] | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |             return {key: key, isApiv4: !isNaN(Number(key))}; | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (value.toLowerCase().startsWith("https://www.mapillary.com/map/im/")) { | 
					
						
							|  |  |  |             // Extract the key of the image
 | 
					
						
							|  |  |  |             value = value.substring("https://www.mapillary.com/map/im/".length); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const matchApi = value.match(/https?:\/\/images.mapillary.com\/([^/]*)(&.*)?/) | 
					
						
							|  |  |  |         if (matchApi !== null) { | 
					
						
							|  |  |  |             return {key: matchApi[1]}; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         if(failIfNoMath){ | 
					
						
							|  |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         return {key: value, isApiv4: !isNaN(Number(value))}; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private async PrepareUrlAsync(key: string, value: string): Promise<ProvidedImage> { | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |         const failIfNoMatch = key.indexOf("mapillary") < 0 | 
					
						
							|  |  |  |         const keyV = Mapillary.ExtractKeyFromURL(value, failIfNoMatch) | 
					
						
							|  |  |  |         if(keyV === undefined){ | 
					
						
							|  |  |  |             return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         if (!keyV.isApiv4) { | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             const url = `https://images.mapillary.com/${keyV.key}/thumb-640.jpg?client_id=${Constants.mapillary_client_token_v3}` | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |                 url: url, | 
					
						
							|  |  |  |                 provider: this, | 
					
						
							|  |  |  |                 key: key | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-10-01 02:57:41 +02:00
										 |  |  |             const mapillaryId = keyV.key; | 
					
						
							|  |  |  |             const metadataUrl = 'https://graph.mapillary.com/' + mapillaryId + '?fields=thumb_1024_url&&access_token=' + Constants.mapillary_client_token_v4; | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |             const response = await Utils.downloadJson(metadataUrl) | 
					
						
							|  |  |  |             const url = <string> response["thumb_1024_url"]; | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |                 url: url, | 
					
						
							|  |  |  |                 provider: this, | 
					
						
							|  |  |  |                 key: key | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-18 01:25:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-29 23:56:59 +02:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |     protected async DownloadAttribution(url: string): Promise<LicenseInfo> { | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         const keyV = Mapillary.ExtractKeyFromURL(url) | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         if (keyV.isApiv4) { | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02: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";
 | 
					
						
							|  |  |  |             license.attributionRequired = true; | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |             return license | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 01:33:52 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         const key = keyV.key | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 17:36:39 +02:00
										 |  |  |         const metadataURL = `https://a.mapillary.com/v3/images/${key}?client_id=TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2` | 
					
						
							|  |  |  |         const data = await Utils.downloadJson(metadataURL) | 
					
						
							|  |  |  |         const license = new LicenseInfo(); | 
					
						
							|  |  |  |         license.artist = data.properties?.username; | 
					
						
							|  |  |  |         license.licenseShortName = "CC BY-SA 4.0"; | 
					
						
							|  |  |  |         license.license = "Creative Commons Attribution-ShareAlike 4.0 International License"; | 
					
						
							|  |  |  |         license.attributionRequired = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return license | 
					
						
							| 
									
										
										
										
											2020-10-17 00:37:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |