2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-14 05:17:07 +00:00
|
|
|
import { summaly } from 'summaly';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-09-18 14:07:41 +00:00
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { query } from '@/misc/prelude/url.js';
|
2022-09-18 14:07:41 +00:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-19 11:27:17 +00:00
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2022-12-13 15:01:45 +00:00
|
|
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UrlPreviewService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
2022-09-18 14:07:41 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger = this.loggerService.getLogger('url-preview');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-02-14 05:17:07 +00:00
|
|
|
private wrap(url?: string | null): string | null {
|
2022-09-17 18:27:08 +00:00
|
|
|
return url != null
|
|
|
|
? url.match(/^https?:\/\//)
|
2023-03-12 08:31:52 +00:00
|
|
|
? `${this.config.mediaProxy}/preview.webp?${query({
|
2022-09-17 18:27:08 +00:00
|
|
|
url,
|
|
|
|
preview: '1',
|
|
|
|
})}`
|
|
|
|
: url
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-12-03 10:42:05 +00:00
|
|
|
public async handle(
|
2023-03-19 11:27:17 +00:00
|
|
|
request: FastifyRequest<{ Querystring: { url: string; lang?: string; } }>,
|
2022-12-03 10:42:05 +00:00
|
|
|
reply: FastifyReply,
|
2023-03-19 11:27:17 +00:00
|
|
|
): Promise<object | undefined> {
|
2022-12-03 10:42:05 +00:00
|
|
|
const url = request.query.url;
|
2022-09-17 18:27:08 +00:00
|
|
|
if (typeof url !== 'string') {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
const lang = request.query.lang;
|
2022-09-17 18:27:08 +00:00
|
|
|
if (Array.isArray(lang)) {
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.code(400);
|
2022-09-17 18:27:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const meta = await this.metaService.fetch();
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.info(meta.summalyProxy
|
2022-09-17 18:27:08 +00:00
|
|
|
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
|
|
|
: `Getting preview of ${url}@${lang} ...`);
|
|
|
|
try {
|
2023-02-14 05:17:07 +00:00
|
|
|
const summary = meta.summalyProxy ?
|
|
|
|
await this.httpRequestService.getJson<ReturnType<typeof summaly>>(`${meta.summalyProxy}?${query({
|
|
|
|
url: url,
|
|
|
|
lang: lang ?? 'ja-JP',
|
|
|
|
})}`)
|
|
|
|
:
|
|
|
|
await summaly(url, {
|
|
|
|
followRedirects: false,
|
|
|
|
lang: lang ?? 'ja-JP',
|
2023-05-07 11:58:08 +00:00
|
|
|
agent: this.config.proxy ? {
|
2023-02-14 05:17:07 +00:00
|
|
|
http: this.httpRequestService.httpAgent,
|
|
|
|
https: this.httpRequestService.httpsAgent,
|
2023-05-07 11:58:08 +00:00
|
|
|
} : undefined,
|
2023-02-14 05:17:07 +00:00
|
|
|
});
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.succ(`Got preview of ${url}: ${summary.title}`);
|
2023-02-04 05:20:07 +00:00
|
|
|
|
2023-03-19 11:27:17 +00:00
|
|
|
if (!(summary.url.startsWith('http://') || summary.url.startsWith('https://'))) {
|
2023-02-04 05:20:07 +00:00
|
|
|
throw new Error('unsupported schema included');
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:59:31 +00:00
|
|
|
if (summary.player.url && !(summary.player.url.startsWith('http://') || summary.player.url.startsWith('https://'))) {
|
2023-02-04 05:20:07 +00:00
|
|
|
throw new Error('unsupported schema included');
|
|
|
|
}
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
summary.icon = this.wrap(summary.icon);
|
|
|
|
summary.thumbnail = this.wrap(summary.thumbnail);
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Cache 7days
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.header('Cache-Control', 'max-age=604800, immutable');
|
2023-03-19 07:59:31 +00:00
|
|
|
|
2022-12-03 10:42:05 +00:00
|
|
|
return summary;
|
2022-09-17 18:27:08 +00:00
|
|
|
} catch (err) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.warn(`Failed to get preview of ${url}: ${err}`);
|
2023-03-19 11:27:17 +00:00
|
|
|
reply.code(422);
|
2022-12-03 10:42:05 +00:00
|
|
|
reply.header('Cache-Control', 'max-age=86400, immutable');
|
2023-03-19 11:27:17 +00:00
|
|
|
return {
|
|
|
|
error: new ApiError({
|
|
|
|
message: 'Failed to get preview',
|
|
|
|
code: 'URL_PREVIEW_FAILED',
|
|
|
|
id: '09d01cb5-53b9-4856-82e5-38a50c290a3b',
|
|
|
|
}),
|
|
|
|
};
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|