From 1fa42c94a6fd7326caec6a844f506fe62370d7b9 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:24:01 +0100 Subject: [PATCH] Workaround for new danbooru bot protection --- package.json | 2 +- src/api/index.ts | 31 +++++++++++++++++++++++++++++++ src/background/main.ts | 6 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1be9418..add8f8f 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "deepmerge": "^4.3.1", "lodash": "^4.17.21", "markdown-it": "^14.0.0", - "neo-scraper": "^0.7.3", + "neo-scraper": "^0.7.4", "normalize.css": "^8.0.1", "pinia": "^2.1.7", "primeflex": "^3.3.1", diff --git a/src/api/index.ts b/src/api/index.ts index 9a63c02..ad219a7 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -148,10 +148,41 @@ export default class SzurubooruApi { } async uploadTempFile(contentUrl: string): Promise { + // HACK: For some sources we need to download the image to the client and then upload it to szurubooru. + // We can't just pass the contentUrl because that would trigger the bot/hotlink protection. + + if (contentUrl.indexOf("donmai.us") != -1) { + console.log("Upload from content"); + return this.uploadTempFileFromContent(contentUrl); + } else { + console.log("Upload from URL"); + return this.uploadTempFileFromUrl(contentUrl); + } + } + + async uploadTempFileFromUrl(contentUrl: string): Promise { const obj = { contentUrl }; return (await this.apiPost("uploads", obj)).data; } + async uploadTempFileFromContent(contentUrl: string): Promise { + const content = await (await fetch(contentUrl)).blob(); + + const fullUrl = this.apiUrl + "uploads"; + + const formData = new FormData(); + formData.append("content", content); + + const config: AxiosRequestConfig = { + method: "POST", + url: fullUrl, + data: formData, + }; + + config.headers = { ...this.baseHeaders, "Content-Type": "multipart/form-data" }; + return (await this.execute(config)).data; + } + static createFromConfig(siteConfig: SzuruSiteConfig): SzurubooruApi { return new SzurubooruApi(siteConfig.domain, siteConfig.username, siteConfig.authToken); } diff --git a/src/background/main.ts b/src/background/main.ts index e044d17..003678d 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -36,6 +36,12 @@ async function uploadPost(data: PostUploadCommandData) { pushInfo(); const contentToken = data.post.instanceSpecificData[data.selectedSite.id]?.contentToken; + + if (!contentToken) { + // TODO: If donmai URL is the source then we need to do the uploadTempFileFromContent here! + console.log("contentToken is undefined. Is could be a problem for some sources!"); + } + const createdPost = await szuru.createPost(data.post, contentToken); info.state = "uploaded";