Skip to content

Commit 83234a3

Browse files
committed
fix(gelbooru): image broken due to missing referer
fix #73
1 parent 7892e6d commit 83234a3

7 files changed

Lines changed: 39 additions & 44 deletions

File tree

src/sites/base/downloadConfig.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type TemplateData = {
2525

2626
export interface OptionBase extends DownloadSettingState {
2727
setProgress?: (progress: number) => void;
28+
headers?: Record<string, string>;
2829
}
2930

3031
export abstract class MediaDownloadConfig<T extends string | string[] = string> {
@@ -222,10 +223,6 @@ export abstract class MayBeMultiIllustsConfig extends MediaDownloadConfig<string
222223
abstract createBundle(option: OptionBase): DownloadConfig[];
223224
}
224225

225-
interface BooruOption extends OptionBase {
226-
cfClearance?: string;
227-
}
228-
229226
export class BooruDownloadConfig extends MediaDownloadConfig {
230227
protected character: string;
231228
protected score: number;
@@ -247,12 +244,6 @@ export class BooruDownloadConfig extends MediaDownloadConfig {
247244
};
248245
}
249246

250-
protected getHeaders(cfClearance: string): Record<string, string> {
251-
return {
252-
cookie: `cf_clearance=${cfClearance}`
253-
};
254-
}
255-
256247
protected getTemplateData(): Partial<TemplateData> {
257248
return {
258249
id: this.id,
@@ -265,18 +256,18 @@ export class BooruDownloadConfig extends MediaDownloadConfig {
265256
};
266257
}
267258

268-
create(option: BooruOption): DownloadConfig {
259+
create(option: OptionBase): DownloadConfig {
269260
const {
270261
filenameTemplate,
271262
filenameConflictAction,
272263
directoryTemplate,
273264
useFileSystemAccessApi,
274265
setProgress,
275-
cfClearance
266+
headers
276267
} = option;
277268

278269
return {
279-
headers: cfClearance ? this.getHeaders(cfClearance) : undefined,
270+
headers,
280271
taskId: this.getTaskId(),
281272
src: this.getSrc(),
282273
path: this.getSavePath(

src/sites/base/gelbooru/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export abstract class GelbooruV020 extends SiteInject {
3838
return '/favicon.ico';
3939
}
4040

41+
protected getHeaders(): Record<string, string> | undefined {
42+
if (userAuthentication.cf_clearance) {
43+
return {
44+
cookie: `cf_clearance=${userAuthentication.cf_clearance}`
45+
};
46+
}
47+
48+
return undefined;
49+
}
50+
4151
#validityCheckFactory(
4252
checkValidity: (meta: Partial<GelbooruMeta>) => Promise<boolean>
4353
): (postData: GelbooruHtmlPostDataV020) => Promise<PostValidState> {
@@ -58,7 +68,7 @@ export abstract class GelbooruV020 extends SiteInject {
5868
downloadArtworkByMeta: async (meta, signal) => {
5969
const downloadConfigs = new BooruDownloadConfig(meta).create({
6070
...downloadSetting,
61-
cfClearance: userAuthentication.cf_clearance || undefined
71+
headers: this.getHeaders()
6272
});
6373

6474
await downloader.download(downloadConfigs, { signal });
@@ -203,7 +213,7 @@ export abstract class GelbooruV020 extends SiteInject {
203213
const mediaMeta = this.parser.buildMeta(id, doc);
204214
const downloadConfig = new BooruDownloadConfig(mediaMeta).create({
205215
...downloadSetting,
206-
cfClearance: userAuthentication.cf_clearance || undefined,
216+
headers: this.getHeaders(),
207217
setProgress: (progress: number) => {
208218
btn.setProgress(progress);
209219
}

src/sites/base/moebooru/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ export abstract class Moebooru extends SiteInject {
4343
return BooruDownloadConfig.supportedTemplate;
4444
}
4545

46+
protected getHeaders(): Record<string, string> | undefined {
47+
if (userAuthentication.cf_clearance) {
48+
return {
49+
cookie: `cf_clearance=${userAuthentication.cf_clearance}`
50+
};
51+
}
52+
53+
return undefined;
54+
}
55+
4656
/**
4757
* register
4858
* https://github.com/moebooru/moebooru/blob/master/app/javascript/src/legacy/post.coffee#L286
@@ -89,7 +99,7 @@ export abstract class Moebooru extends SiteInject {
8999
downloadArtworkByMeta: async (meta, signal) => {
90100
const downloadConfig = new BooruDownloadConfig(meta).create({
91101
...downloadSetting,
92-
cfClearance: userAuthentication.cf_clearance || undefined
102+
headers: this.getHeaders()
93103
});
94104

95105
await downloader.download(downloadConfig, { signal });
@@ -325,7 +335,7 @@ export abstract class Moebooru extends SiteInject {
325335

326336
const downloadConfig = new BooruDownloadConfig(mediaMeta).create({
327337
...downloadSetting,
328-
cfClearance: userAuthentication.cf_clearance || undefined,
338+
headers: this.getHeaders(),
329339
setProgress: (progress: number) => {
330340
btn.setProgress(progress);
331341
}

src/sites/gelbooru/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class Gelbooru extends GelbooruV020 {
3535
return '/user_avatars/honkonymous.png';
3636
}
3737

38+
protected getHeaders() {
39+
return {
40+
referer: 'https://gelbooru.com/'
41+
};
42+
}
43+
3844
protected getThumbnailSelector(): string {
3945
// favorite
4046
// post list

src/sites/rule34vault/downloadConfig.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import type { DownloadConfig } from '@/lib/downloader';
2-
import {
3-
BooruDownloadConfig,
4-
SupportedTemplate,
5-
type OptionBase,
6-
type TemplateData
7-
} from '../base/downloadConfig';
1+
import { BooruDownloadConfig, SupportedTemplate, type TemplateData } from '../base/downloadConfig';
82

93
export class Rule34VaultDownloadConfig extends BooruDownloadConfig {
104
static get supportedTemplate(): Partial<TemplateData> {
@@ -26,8 +20,4 @@ export class Rule34VaultDownloadConfig extends BooruDownloadConfig {
2620
score: String(this.score)
2721
};
2822
}
29-
30-
create(option: OptionBase): DownloadConfig {
31-
return super.create({ ...option, cfClearance: undefined });
32-
}
3323
}

src/sites/sankakuComplex/downloadConfig.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/sites/sankakuComplex/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { SiteInject } from '../base';
33
import { logger } from '@/lib/logger';
44
import { SankakuApi, type SankakuPool } from './api';
55
import { SankakuParser } from './parser';
6-
import { SankakuDownloadConfig } from './downloadConfig';
76
import { downloadSetting } from '@/lib/store/downloadSetting.svelte';
87
import { downloader } from '@/lib/downloader';
98
import { historyDb } from '@/lib/db';
@@ -14,6 +13,7 @@ import { ArtworkButton } from '@/lib/components/Button/artworkButton';
1413
import { regexp } from '@/lib/regExp';
1514
import { t } from '@/lib/i18n.svelte';
1615
import { PostValidState } from '../base/parser';
16+
import { BooruDownloadConfig } from '../base/downloadConfig';
1717

1818
/** @default query */
1919
type SelectorMatchStrategy = 'query' | 'match' | 'both';
@@ -103,7 +103,7 @@ export class SankakuApp extends SiteInject {
103103
}
104104

105105
protected getSupportedTemplate() {
106-
return SankakuDownloadConfig.supportedTemplate;
106+
return BooruDownloadConfig.supportedTemplate;
107107
}
108108

109109
static get hostname(): string[] {
@@ -140,7 +140,7 @@ export class SankakuApp extends SiteInject {
140140

141141
const mediaMeta = this.parser.buildMeta(postData, postUrl, tagDetail);
142142

143-
const downloadConfig = new SankakuDownloadConfig(mediaMeta).create({
143+
const downloadConfig = new BooruDownloadConfig(mediaMeta).create({
144144
...downloadSetting,
145145
setProgress: (progress: number) => {
146146
btn.setProgress(progress);
@@ -334,7 +334,7 @@ export class SankakuApp extends SiteInject {
334334
downloadArtworkByMeta: async (meta, signal) => {
335335
if (this.parser.isURLExpired(meta.src)) throw new Error('URL is expired.');
336336

337-
const downloadConfig = new SankakuDownloadConfig(meta).create({
337+
const downloadConfig = new BooruDownloadConfig(meta).create({
338338
...downloadSetting
339339
});
340340

0 commit comments

Comments
 (0)