Load nearby image and image:* images from server for the nearby images

This commit is contained in:
Pieter Vander Vennet 2024-07-27 12:59:38 +02:00
parent 9a4572cdfa
commit caa2e18a03
10 changed files with 206 additions and 72 deletions

View file

@ -12,19 +12,23 @@ import { WikidataImageProvider } from "./WikidataImageProvider"
export default class AllImageProviders {
private static dontLoadFromPrefixes = ["https://photos.app.goo.gl/"]
public static ImageAttributionSource: ImageProvider[] = [
/**
* 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,
"Category:",
])
private static ImageAttributionSource: ImageProvider[] = [
Imgur.singleton,
Mapillary.singleton,
WikidataImageProvider.singleton,
WikimediaImageProvider.singleton,
// The 'genericImageProvider' is a fallback that scans various other tags for tags, unless the URL starts with one of the given prefixes
new GenericImageProvider([
...Imgur.defaultValuePrefix,
...WikimediaImageProvider.commonsPrefixes,
...Mapillary.valuePrefixes,
...AllImageProviders.dontLoadFromPrefixes,
"Category:",
]),
AllImageProviders.genericImageProvider
]
public static apiUrls: string[] = [].concat(
...AllImageProviders.ImageAttributionSource.map((src) => src.apiUrls())
@ -47,6 +51,23 @@ export default class AllImageProviders {
return AllImageProviders.providersByName[name.toLowerCase()]
}
public static async selectBestProvider(key: string, value: string): Promise<ImageProvider> {
for (const imageProvider of AllImageProviders.ImageAttributionSource) {
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
}
public static LoadImagesFor(
tags: Store<Record<string, string>>,
tagKey?: string[]
@ -63,7 +84,7 @@ export default class AllImageProviders {
const source = new UIEventSource([])
this._cache.set(cacheKey, source)
const allSources = []
const allSources: Store<ProvidedImage[]>[] = []
for (const imageProvider of AllImageProviders.ImageAttributionSource) {
let prefixes = imageProvider.defaultKeyPrefixes
if (tagKey !== undefined) {

View file

@ -2,6 +2,7 @@ import ImageProvider, { ProvidedImage } from "./ImageProvider"
export default class GenericImageProvider extends ImageProvider {
public defaultKeyPrefixes: string[] = ["image"]
public readonly name = "Generic"
public apiUrls(): string[] {
return []

View file

@ -15,6 +15,8 @@ export interface ProvidedImage {
export default abstract class ImageProvider {
public abstract readonly defaultKeyPrefixes: string[]
public abstract readonly name: string
public abstract SourceIcon(id?: string, location?: { lon: number; lat: number }): BaseUIElement
/**

View file

@ -8,6 +8,7 @@ import { ImageUploader } from "./ImageUploader"
export class Imgur extends ImageProvider implements ImageUploader {
public static readonly defaultValuePrefix = ["https://i.imgur.com"]
public static readonly singleton = new Imgur()
public readonly name = "Imgur"
public readonly defaultKeyPrefixes: string[] = ["image"]
public readonly maxFileSizeInMegabytes = 10
public static readonly apiUrl = "https://api.imgur.com/3/image"

View file

@ -8,6 +8,8 @@ import MapillaryIcon from "./MapillaryIcon.svelte"
export class Mapillary extends ImageProvider {
public static readonly singleton = new Mapillary()
public readonly name = "Mapillary"
private static readonly valuePrefix = "https://a.mapillary.com"
public static readonly valuePrefixes = [
Mapillary.valuePrefix,

View file

@ -8,6 +8,7 @@ import * as Wikidata_icon from "../../assets/svg/Wikidata.svelte"
export class WikidataImageProvider extends ImageProvider {
public static readonly singleton = new WikidataImageProvider()
public readonly defaultKeyPrefixes = ["wikidata"]
public readonly name = "Wikidata"
private constructor() {
super()

View file

@ -18,6 +18,7 @@ export class WikimediaImageProvider extends ImageProvider {
public static readonly commonsPrefixes = [...WikimediaImageProvider.apiUrls, "File:"]
private readonly commons_key = "wikimedia_commons"
public readonly defaultKeyPrefixes = [this.commons_key, "image"]
public readonly name = "Wikimedia"
private constructor() {
super()
@ -133,9 +134,9 @@ export class WikimediaImageProvider extends ImageProvider {
"titles=" +
filename +
"&format=json&origin=*"
const data = await Utils.downloadJsonCached(url, 365 * 24 * 60 * 60)
const data = await Utils.downloadJsonCached<{query: {pages: {title: string, imageinfo: { extmetadata} []}[]}}>(url, 365 * 24 * 60 * 60)
const licenseInfo = new LicenseInfo()
const pageInfo = data.query.pages[-1]
const pageInfo = data.query.pages.at(-1)
if (pageInfo === undefined) {
return undefined
}