Reformat all files with prettier

This commit is contained in:
Pieter Vander Vennet 2022-09-08 21:40:48 +02:00
parent e22d189376
commit b541d3eab4
382 changed files with 50893 additions and 35566 deletions

View file

@ -1,93 +1,105 @@
import ImageProvider, { ProvidedImage } from "./ImageProvider";
import BaseUIElement from "../../UI/BaseUIElement";
import {Utils} from "../../Utils";
import Constants from "../../Models/Constants";
import {LicenseInfo} from "./LicenseInfo";
import ImageProvider, { ProvidedImage } from "./ImageProvider"
import BaseUIElement from "../../UI/BaseUIElement"
import { Utils } from "../../Utils"
import Constants from "../../Models/Constants"
import { LicenseInfo } from "./LicenseInfo"
export class Imgur extends ImageProvider {
public static readonly defaultValuePrefix = ["https://i.imgur.com"]
public static readonly singleton = new Imgur();
public readonly defaultKeyPrefixes: string[] = ["image"];
public static readonly singleton = new Imgur()
public readonly defaultKeyPrefixes: string[] = ["image"]
private constructor() {
super();
super()
}
static uploadMultiple(
title: string, description: string, blobs: FileList,
handleSuccessfullUpload: ((imageURL: string) => Promise<void>),
allDone: (() => void),
onFail: ((reason: string) => void),
offset: number = 0) {
title: string,
description: string,
blobs: FileList,
handleSuccessfullUpload: (imageURL: string) => Promise<void>,
allDone: () => void,
onFail: (reason: string) => void,
offset: number = 0
) {
if (blobs.length == offset) {
allDone();
return;
allDone()
return
}
const blob = blobs.item(offset);
const self = this;
this.uploadImage(title, description, blob,
const blob = blobs.item(offset)
const self = this
this.uploadImage(
title,
description,
blob,
async (imageUrl) => {
await handleSuccessfullUpload(imageUrl);
await handleSuccessfullUpload(imageUrl)
self.uploadMultiple(
title, description, blobs,
title,
description,
blobs,
handleSuccessfullUpload,
allDone,
onFail,
offset + 1);
offset + 1
)
},
onFail
);
)
}
static uploadImage(title: string, description: string, blob: File,
handleSuccessfullUpload: ((imageURL: string) => Promise<void>),
onFail: (reason: string) => void) {
static uploadImage(
title: string,
description: string,
blob: File,
handleSuccessfullUpload: (imageURL: string) => Promise<void>,
onFail: (reason: string) => void
) {
const apiUrl = "https://api.imgur.com/3/image"
const apiKey = Constants.ImgurApiKey
const apiUrl = 'https://api.imgur.com/3/image';
const apiKey = Constants.ImgurApiKey;
const formData = new FormData();
formData.append('image', blob);
formData.append("title", title);
const formData = new FormData()
formData.append("image", blob)
formData.append("title", title)
formData.append("description", description)
const settings: RequestInit = {
method: 'POST',
method: "POST",
body: formData,
redirect: 'follow',
redirect: "follow",
headers: new Headers({
Authorization: `Client-ID ${apiKey}`,
Accept: 'application/json',
Accept: "application/json",
}),
};
}
// Response contains stringified JSON
// Image URL available at response.data.link
fetch(apiUrl, settings).then(async function (response) {
const content = await response.json()
await handleSuccessfullUpload(content.data.link);
}).catch((reason) => {
console.log("Uploading to IMGUR failed", reason);
// @ts-ignore
onFail(reason);
});
fetch(apiUrl, settings)
.then(async function (response) {
const content = await response.json()
await handleSuccessfullUpload(content.data.link)
})
.catch((reason) => {
console.log("Uploading to IMGUR failed", reason)
// @ts-ignore
onFail(reason)
})
}
SourceIcon(): BaseUIElement {
return undefined;
return undefined
}
public async ExtractUrls(key: string, value: string): Promise<Promise<ProvidedImage>[]> {
if (Imgur.defaultValuePrefix.some(prefix => value.startsWith(prefix))) {
return [Promise.resolve({
url: value,
key: key,
provider: this
})]
if (Imgur.defaultValuePrefix.some((prefix) => value.startsWith(prefix))) {
return [
Promise.resolve({
url: value,
key: key,
provider: this,
}),
]
}
return []
}
@ -103,29 +115,27 @@ export class Imgur extends ImageProvider {
* expected.artist = "Pieter Vander Vennet"
* licenseInfo // => expected
*/
public async DownloadAttribution (url: string) : Promise<LicenseInfo> {
const hash = url.substr("https://i.imgur.com/".length).split(".jpg")[0];
public async DownloadAttribution(url: string): Promise<LicenseInfo> {
const hash = url.substr("https://i.imgur.com/".length).split(".jpg")[0]
const apiUrl = 'https://api.imgur.com/3/image/' + hash;
const response = await Utils.downloadJsonCached(apiUrl, 365*24*60*60,
{Authorization: 'Client-ID ' + Constants.ImgurApiKey})
const apiUrl = "https://api.imgur.com/3/image/" + hash
const response = await Utils.downloadJsonCached(apiUrl, 365 * 24 * 60 * 60, {
Authorization: "Client-ID " + Constants.ImgurApiKey,
})
const descr: string = response.data.description ?? "";
const data: any = {};
const descr: string = response.data.description ?? ""
const data: any = {}
for (const tag of descr.split("\n")) {
const kv = tag.split(":");
const k = kv[0];
data[k] = kv[1]?.replace(/\r/g, "");
const kv = tag.split(":")
const k = kv[0]
data[k] = kv[1]?.replace(/\r/g, "")
}
const licenseInfo = new LicenseInfo()
const licenseInfo = new LicenseInfo();
licenseInfo.licenseShortName = data.license;
licenseInfo.artist = data.author;
licenseInfo.licenseShortName = data.license
licenseInfo.artist = data.author
return licenseInfo
}
}
}