Merge master

This commit is contained in:
Pieter Vander Vennet 2024-11-24 23:55:58 +01:00
commit 1d87151228
185 changed files with 4687 additions and 2421 deletions

View file

@ -68,7 +68,7 @@ export default class AllImageProviders {
private static readonly _cachedImageStores: Record<string, Store<ProvidedImage[]>> = {}
/**
* Tries to extract all image data for this image. Cachedon tags?.data?.id
* Tries to extract all image data for this image. Cached on tags?.data?.id
*/
public static LoadImagesFor(
tags: Store<Record<string, string>>,
@ -78,8 +78,9 @@ export default class AllImageProviders {
return undefined
}
const id = tags?.data?.id
if (this._cachedImageStores[id]) {
return this._cachedImageStores[id]
const cachekey = id + (tagKey?.join(";") ?? "")
if (this._cachedImageStores[cachekey]) {
return this._cachedImageStores[cachekey]
}
const source = new UIEventSource([])
@ -90,6 +91,7 @@ export default class AllImageProviders {
However, we override them if a custom image tag is set, e.g. 'image:menu'
*/
const prefixes = tagKey ?? imageProvider.defaultKeyPrefixes
console.log("Prefixes are", tagKey, prefixes)
const singleSource = tags.bindD((tags) => imageProvider.getRelevantUrls(tags, prefixes))
allSources.push(singleSource)
singleSource.addCallbackAndRunD((_) => {
@ -98,21 +100,19 @@ export default class AllImageProviders {
source.set(dedup)
})
}
this._cachedImageStores[id] = source
this._cachedImageStores[cachekey] = source
return source
}
/**
* Given a list of URLs, tries to detect the images. Used in e.g. the comments
* @param url
*/
public static loadImagesFrom(urls: string[]): Store<ProvidedImage[]> {
const tags = {
id: "na",
id: urls.join(";"),
}
for (let i = 0; i < urls.length; i++) {
const url = urls[i]
tags["image:" + i] = url
tags["image:" + i] = urls[i]
}
return this.LoadImagesFor(new ImmutableStore(tags))
}

View file

@ -11,6 +11,7 @@ export class Imgur extends ImageProvider {
public readonly defaultKeyPrefixes: string[] = ["image"]
public static readonly apiUrl = "https://api.imgur.com/3/image"
public static readonly supportingUrls = ["https://i.imgur.com"]
private constructor() {
super()
}
@ -37,6 +38,37 @@ export class Imgur extends ImageProvider {
return undefined
}
public static parseLicense(descr: string) {
const data: Record<string, string> = {}
if (!descr) {
return undefined
}
if (descr.toLowerCase() === "cc0") {
data.author = "Unknown"
data.license = "CC0"
} else {
for (const tag of descr.split("\n")) {
const kv = tag.split(":")
if (kv.length < 2) {
continue
}
const k = kv[0]
data[k] = kv[1]?.replace(/\r/g, "")
}
}
if (Object.keys(data).length === 0) {
return undefined
}
const licenseInfo = new LicenseInfo()
licenseInfo.licenseShortName = data.license
licenseInfo.artist = data.author
return licenseInfo
}
/**
* Download the attribution and license info for the picture at the given URL
*
@ -56,9 +88,14 @@ export class Imgur extends ImageProvider {
*
*
*/
public async DownloadAttribution(providedImage: { url: string }): Promise<LicenseInfo> {
public async DownloadAttribution(
providedImage: {
url: string
},
withResponse?: (obj) => void
): Promise<LicenseInfo> {
const url = providedImage.url
const hash = url.substr("https://i.imgur.com/".length).split(/\.jpe?g/i)[0]
const hash = url.substr("https://i.imgur.com/".length).split(/(\.jpe?g)|(\.png)/i)[0]
const apiUrl = "https://api.imgur.com/3/image/" + hash
const response = await Utils.downloadJsonCached<{
@ -66,24 +103,17 @@ export class Imgur extends ImageProvider {
}>(apiUrl, 365 * 24 * 60 * 60, {
Authorization: "Client-ID " + Constants.ImgurApiKey,
})
const descr = response.data.description ?? ""
const data: any = {}
const imgurData = response.data
for (const tag of descr.split("\n")) {
const kv = tag.split(":")
const k = kv[0]
data[k] = kv[1]?.replace(/\r/g, "")
if (withResponse) {
withResponse(response)
}
const licenseInfo = new LicenseInfo()
const imgurData = response.data
const license = Imgur.parseLicense(imgurData.description ?? "")
if (license) {
license.views = imgurData.views
license.date = new Date(Number(imgurData.datetime) * 1000)
}
licenseInfo.licenseShortName = data.license
licenseInfo.artist = data.author
licenseInfo.date = new Date(Number(imgurData.datetime) * 1000)
licenseInfo.views = imgurData.views
return licenseInfo
return license
}
}

View file

@ -145,12 +145,7 @@ export default class PanoramaxImageProvider extends ImageProvider {
)
}
Stores.Chronic(1500, () => hasLoading(source.data)).addCallback((_) => {
console.log(
"Testing panoramax URLS again as some were loading",
source.data,
hasLoading(source.data)
)
Stores.Chronic(1500, () => hasLoading(source.data)).addCallback(() => {
super.getRelevantUrlsFor(tags, prefixes).then((data) => {
source.set(data)
return !hasLoading(data)
@ -204,7 +199,8 @@ export class PanoramaxUploader implements ImageUploader {
currentGps: [number, number],
author: string,
noblur: boolean = false,
sequenceId?: string
sequenceId?: string,
datetime?: string
): Promise<{
key: string
value: string
@ -213,7 +209,7 @@ export class PanoramaxUploader implements ImageUploader {
// https://panoramax.openstreetmap.fr/api/docs/swagger#/
let [lon, lat] = currentGps ?? [undefined, undefined]
let datetime = new Date().toISOString()
datetime ??= new Date().toISOString()
try {
const tags = await ExifReader.load(blob)
const [[latD], [latM], [latS, latSDenom]] = <