Workaround for new danbooru bot protection

pull/55/head
neobooru 2024-01-01 20:24:01 +01:00
parent b4e63c8c5e
commit 1fa42c94a6
3 changed files with 38 additions and 1 deletions

View File

@ -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",

View File

@ -148,10 +148,41 @@ export default class SzurubooruApi {
}
async uploadTempFile(contentUrl: string): Promise<TemporaryFileUploadResult> {
// 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<TemporaryFileUploadResult> {
const obj = { contentUrl };
return (await this.apiPost("uploads", obj)).data;
}
async uploadTempFileFromContent(contentUrl: string): Promise<TemporaryFileUploadResult> {
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);
}

View File

@ -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";