2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import summaly from 'summaly';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { UsersRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-09-18 16:07:41 +02:00
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { query } from '@/misc/prelude/url.js';
|
2022-09-18 16:07:41 +02:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-12-13 16:01:45 +01:00
|
|
|
import type { FastifyRequest, FastifyReply } from 'fastify';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UrlPreviewService {
|
2022-09-18 20:11:50 +02:00
|
|
|
private logger: Logger;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
2022-09-18 16:07:41 +02:00
|
|
|
private loggerService: LoggerService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger = this.loggerService.getLogger('url-preview');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-18 20:11:50 +02:00
|
|
|
private wrap(url?: string): string | null {
|
2022-09-17 20:27:08 +02:00
|
|
|
return url != null
|
|
|
|
? url.match(/^https?:\/\//)
|
2023-02-04 05:38:51 +01:00
|
|
|
? `${this.config.mediaProxy}/preview.webp?${query({
|
2022-09-17 20:27:08 +02:00
|
|
|
url,
|
|
|
|
preview: '1',
|
|
|
|
})}`
|
|
|
|
: url
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-12-03 11:42:05 +01:00
|
|
|
public async handle(
|
|
|
|
request: FastifyRequest<{ Querystring: { url: string; lang: string; } }>,
|
|
|
|
reply: FastifyReply,
|
|
|
|
) {
|
|
|
|
const url = request.query.url;
|
2022-09-17 20:27:08 +02:00
|
|
|
if (typeof url !== 'string') {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(400);
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
const lang = request.query.lang;
|
2022-09-17 20:27:08 +02:00
|
|
|
if (Array.isArray(lang)) {
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(400);
|
2022-09-17 20:27:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.info(meta.summalyProxy
|
2022-09-17 20:27:08 +02:00
|
|
|
? `(Proxy) Getting preview of ${url}@${lang} ...`
|
|
|
|
: `Getting preview of ${url}@${lang} ...`);
|
|
|
|
try {
|
2023-01-12 13:03:02 +01:00
|
|
|
const summary = meta.summalyProxy ? await this.httpRequestService.getJson<ReturnType<typeof summaly.default>>(`${meta.summalyProxy}?${query({
|
2022-09-17 20:27:08 +02:00
|
|
|
url: url,
|
|
|
|
lang: lang ?? 'ja-JP',
|
|
|
|
})}`) : await summaly.default(url, {
|
|
|
|
followRedirects: false,
|
|
|
|
lang: lang ?? 'ja-JP',
|
|
|
|
});
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.succ(`Got preview of ${url}: ${summary.title}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
summary.icon = this.wrap(summary.icon);
|
|
|
|
summary.thumbnail = this.wrap(summary.thumbnail);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
// Cache 7days
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.header('Cache-Control', 'max-age=604800, immutable');
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-03 11:42:05 +01:00
|
|
|
return summary;
|
2022-09-17 20:27:08 +02:00
|
|
|
} catch (err) {
|
2022-09-18 20:11:50 +02:00
|
|
|
this.logger.warn(`Failed to get preview of ${url}: ${err}`);
|
2022-12-03 11:42:05 +01:00
|
|
|
reply.code(200);
|
|
|
|
reply.header('Cache-Control', 'max-age=86400, immutable');
|
|
|
|
return {};
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|