Skip to content

Commit

Permalink
Workaround for new danbooru bot protection
Browse files Browse the repository at this point in the history
  • Loading branch information
neobooru committed Jan 1, 2024
1 parent b4e63c8 commit 1fa42c9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 1fa42c9

Please sign in to comment.