sharkey/packages/backend/src/server/web/UrlPreviewService.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-09-18 03:27:08 +09:00
import { Inject, Injectable } from '@nestjs/common';
import summaly from 'summaly';
import { DI } from '@/di-symbols.js';
import { UsersRepository } from '@/models/index.js';
import { Config } from '@/config.js';
import { MetaService } from '@/core/MetaService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
2022-09-18 23:07:41 +09:00
import type Logger from '@/logger.js';
2022-09-18 03:27:08 +09:00
import { query } from '@/misc/prelude/url.js';
2022-09-18 23:07:41 +09:00
import { LoggerService } from '@/core/LoggerService.js';
2022-09-18 03:27:08 +09:00
import type Koa from 'koa';
@Injectable()
export class UrlPreviewService {
#logger: Logger;
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
private metaService: MetaService,
private httpRequestService: HttpRequestService,
2022-09-18 23:07:41 +09:00
private loggerService: LoggerService,
2022-09-18 03:27:08 +09:00
) {
2022-09-18 23:07:41 +09:00
this.#logger = this.loggerService.getLogger('url-preview');
2022-09-18 03:27:08 +09:00
}
#wrap(url?: string): string | null {
return url != null
? url.match(/^https?:\/\//)
? `${this.config.url}/proxy/preview.webp?${query({
url,
preview: '1',
})}`
: url
: null;
}
public async handle(ctx: Koa.Context) {
const url = ctx.query.url;
if (typeof url !== 'string') {
ctx.status = 400;
return;
}
const lang = ctx.query.lang;
if (Array.isArray(lang)) {
ctx.status = 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(`${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
ctx.set('Cache-Control', 'max-age=604800, immutable');
ctx.body = summary;
} catch (err) {
this.#logger.warn(`Failed to get preview of ${url}: ${err}`);
ctx.status = 200;
ctx.set('Cache-Control', 'max-age=86400, immutable');
ctx.body = '{}';
}
}
}