978a9bbb3b
* Implement? HttpFetchService
* ✌️
* remove node-fetch
* fix
* refactor
* fix
* gateway timeout
* UndiciFetcherクラスを追加 (仮コミット, ビルドもstartもさせていない)
* fix
* add logger and fix url preview
* fix ip check
* enhance logger and error handling
* fix
* fix
* clean up
* Use custom fetcher for ApRequest / ApResolver
* bypassProxyはproxyBypassHostsに判断を委譲するように
* set maxRedirections (default 3, ApRequest/ApResolver: 0)
* fix comment
* handle error s3 upload
* add debug message
* no return await
* Revert "no return await"
This reverts commit b5b0dc58a342393d260492e3a6f58304372f53b2.
* reduce maxSockets
* apResolverのUndiciFetcherを廃止しapRequestのものを使う、 add ap logger
* Revert "apResolverのUndiciFetcherを廃止しapRequestのものを使う、 add ap logger"
This reverts commit 997243915c8e1f8472da64f607f88c36cb1d5cb4.
* add logger
* fix
* change logger name
* safe
* デフォルトでUser-Agentを設定
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import summaly from 'summaly';
|
|
import { DI } from '@/di-symbols.js';
|
|
import type { UsersRepository } from '@/models/index.js';
|
|
import type { Config } from '@/config.js';
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
|
import type Logger from '@/logger.js';
|
|
import { query } from '@/misc/prelude/url.js';
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
|
|
|
@Injectable()
|
|
export class UrlPreviewService {
|
|
private logger: Logger;
|
|
|
|
constructor(
|
|
@Inject(DI.config)
|
|
private config: Config,
|
|
|
|
@Inject(DI.usersRepository)
|
|
private usersRepository: UsersRepository,
|
|
|
|
private metaService: MetaService,
|
|
private httpRequestService: HttpRequestService,
|
|
private loggerService: LoggerService,
|
|
) {
|
|
this.logger = this.loggerService.getLogger('url-preview');
|
|
}
|
|
|
|
@bindThis
|
|
private wrap(url?: string): string | null {
|
|
return url != null
|
|
? url.match(/^https?:\/\//)
|
|
? `${this.config.url}/proxy/preview.webp?${query({
|
|
url,
|
|
preview: '1',
|
|
})}`
|
|
: url
|
|
: null;
|
|
}
|
|
|
|
@bindThis
|
|
public async handle(
|
|
request: FastifyRequest<{ Querystring: { url: string; lang: string; } }>,
|
|
reply: FastifyReply,
|
|
) {
|
|
const url = request.query.url;
|
|
if (typeof url !== 'string') {
|
|
reply.code(400);
|
|
return;
|
|
}
|
|
|
|
const lang = request.query.lang;
|
|
if (Array.isArray(lang)) {
|
|
reply.code(400);
|
|
return;
|
|
}
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
this.logger.info(meta.summalyProxy
|
|
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
|
: `Getting preview of ${url}@${lang} ...`);
|
|
try {
|
|
const summary = meta.summalyProxy ? await this.httpRequestService.getJson<ReturnType<typeof summaly.default>>(`${meta.summalyProxy}?${query({
|
|
url: url,
|
|
lang: lang ?? 'ja-JP',
|
|
})}`) : await summaly.default(url, {
|
|
followRedirects: false,
|
|
lang: lang ?? 'ja-JP',
|
|
});
|
|
|
|
this.logger.succ(`Got preview of ${url}: ${summary.title}`);
|
|
|
|
summary.icon = this.wrap(summary.icon);
|
|
summary.thumbnail = this.wrap(summary.thumbnail);
|
|
|
|
// Cache 7days
|
|
reply.header('Cache-Control', 'max-age=604800, immutable');
|
|
|
|
return summary;
|
|
} catch (err) {
|
|
this.logger.warn(`Failed to get preview of ${url}: ${err}`);
|
|
reply.code(200);
|
|
reply.header('Cache-Control', 'max-age=86400, immutable');
|
|
return {};
|
|
}
|
|
}
|
|
}
|