MapComplete/src/Logic/ImageProviders/AllImageProviders.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

161 lines
5.8 KiB
TypeScript
Raw Normal View History

import { Mapillary } from "./Mapillary"
import { WikimediaImageProvider } from "./WikimediaImageProvider"
import { Imgur } from "./Imgur"
import GenericImageProvider from "./GenericImageProvider"
2025-04-09 17:09:11 +02:00
import { ImmutableStore, Store, Stores } from "../UIEventSource"
import ImageProvider, { ProvidedImage } from "./ImageProvider"
import { WikidataImageProvider } from "./WikidataImageProvider"
import Panoramax from "./Panoramax"
import { Utils } from "../../Utils"
/**
* A generic 'from the interwebz' image picker, without attribution
*/
export default class AllImageProviders {
private static dontLoadFromPrefixes = ["https://photos.app.goo.gl/"]
/**
* The 'genericImageProvider' is a fallback that scans various other tags for tags, unless the URL starts with one of the given prefixes
*/
public static genericImageProvider = new GenericImageProvider([
...Imgur.defaultValuePrefix,
...WikimediaImageProvider.commonsPrefixes,
...Mapillary.valuePrefixes,
...AllImageProviders.dontLoadFromPrefixes,
2025-01-28 15:42:34 +01:00
"Category:",
])
private static imageAttributionSources: ImageProvider[] = [
Imgur.singleton,
Mapillary.singleton,
WikidataImageProvider.singleton,
WikimediaImageProvider.singleton,
Panoramax.singleton,
2025-01-28 15:42:34 +01:00
AllImageProviders.genericImageProvider,
]
2023-09-27 22:21:35 +02:00
public static apiUrls: string[] = [].concat(
...AllImageProviders.imageAttributionSources.map((src) => src.apiUrls())
2023-09-27 22:21:35 +02:00
)
2025-04-09 17:09:11 +02:00
public static defaultKeys: string[] = [].concat(
...AllImageProviders.imageAttributionSources.map((provider) => provider.defaultKeyPrefixes)
2023-09-27 22:21:35 +02:00
)
private static providersByName = {
imgur: Imgur.singleton,
mapillary: Mapillary.singleton,
wikidata: WikidataImageProvider.singleton,
wikimedia: WikimediaImageProvider.singleton,
2025-01-28 15:42:34 +01:00
panoramax: Panoramax.singleton,
}
2023-09-27 22:21:35 +02:00
public static byName(name: string) {
return AllImageProviders.providersByName[name.toLowerCase()]
}
public static async selectBestProvider(key: string, value: string): Promise<ImageProvider> {
for (const imageProvider of AllImageProviders.imageAttributionSources) {
2024-08-09 16:55:08 +02:00
try {
const extracted = await Promise.all(await imageProvider.ExtractUrls(key, value))
if (extracted?.length > 0) {
return imageProvider
}
} catch (e) {
console.warn("Provider gave an error while trying to determine a match:", e)
}
}
return AllImageProviders.genericImageProvider
}
private static readonly _cachedImageStores: Record<string, Store<ProvidedImage[]>> = {}
/**
* Does a guess on the number of images that are probably there.
* Will simply count all image tags
*
2025-01-17 16:47:56 +01:00
* AllImageProviders.estimateNumberOfImages({image:"abc", "mapillary": "123", "panoramax:0": "xyz"}) // => 3
* AllImageProviders.estimateNumberOfImages({wikidata:"Q123", "wikipedia": "nl:xyz"}) // => 0
*
* AllImageProviders.estimateNumberOfImages({image:"https://photos.app.goo.gl/tjt5FsQZtpkQRVw38"}) // => 0
*
*/
2025-01-28 15:42:34 +01:00
public static estimateNumberOfImages(
tags: Record<string, string>,
prefixes: string[] = undefined
): number {
let count = 0
2025-02-10 02:04:58 +01:00
const sources = [
Imgur.singleton,
Mapillary.singleton,
Panoramax.singleton,
2025-02-10 02:04:58 +01:00
AllImageProviders.genericImageProvider,
]
const allPrefixes = Utils.Dedup(
prefixes ?? [].concat(...sources.map((s) => s.defaultKeyPrefixes))
)
2025-01-17 16:47:56 +01:00
for (const prefix of allPrefixes) {
for (const k in tags) {
const v = tags[k]
if (!v) {
continue
}
if (AllImageProviders.dontLoadFromPrefixes.some(prefix => v.startsWith(prefix))) {
continue
}
if (k === prefix || k.startsWith(prefix + ":")) {
count++
2025-01-17 16:47:56 +01:00
continue
}
}
}
return count
}
/**
* Tries to extract all image data for this image. Cached on tags?.data?.id
*/
2025-01-17 14:25:30 +01:00
public static loadImagesFor(
tags: Store<Record<string, string>>,
2024-10-19 14:44:55 +02:00
tagKey?: string[]
): Store<ProvidedImage[]> {
if (tags?.data?.id === undefined) {
return undefined
}
const id = tags?.data?.id
const cachekey = id + (tagKey?.join(";") ?? "")
if (this._cachedImageStores[cachekey]) {
return this._cachedImageStores[cachekey]
}
const allSources: Store<ProvidedImage[]>[] = []
for (const imageProvider of AllImageProviders.imageAttributionSources) {
/*
By default, 'GetRelevantUrls' uses the defaultKeyPrefixes.
However, we override them if a custom image tag is set, e.g. 'image:menu'
*/
const prefixes = tagKey ?? imageProvider.defaultKeyPrefixes
2024-10-19 14:44:55 +02:00
const singleSource = tags.bindD((tags) => imageProvider.getRelevantUrls(tags, prefixes))
allSources.push(singleSource)
}
2025-04-15 18:18:44 +02:00
const source = Stores.fromStoresArray(allSources).map((result) => {
2025-04-09 17:09:11 +02:00
const all = [].concat(...result)
return Utils.DedupOnId(all, (i) => i?.id ?? i?.url)
})
this._cachedImageStores[cachekey] = source
return source
}
/**
* Given a list of URLs, tries to detect the images. Used in e.g. the comments
*/
public static loadImagesFrom(urls: string[]): Store<ProvidedImage[]> {
const tags = {
2025-01-28 15:42:34 +01:00
id: urls.join(";"),
}
for (let i = 0; i < urls.length; i++) {
2024-11-24 23:54:13 +01:00
tags["image:" + i] = urls[i]
}
2025-01-17 14:25:30 +01:00
return this.loadImagesFor(new ImmutableStore(tags))
}
}