2023-12-11 20:37:47 +01:00
|
|
|
import {
|
|
|
|
AuthService,
|
|
|
|
JobService,
|
|
|
|
ONE_HOUR,
|
|
|
|
OpenGraphTags,
|
|
|
|
ServerInfoService,
|
|
|
|
SharedLinkService,
|
|
|
|
StorageService,
|
2023-12-14 17:55:40 +01:00
|
|
|
SystemConfigService,
|
2023-12-16 18:15:30 +01:00
|
|
|
WEB_ROOT_PATH,
|
2023-12-11 20:37:47 +01:00
|
|
|
} from '@app/domain';
|
2023-12-14 17:55:40 +01:00
|
|
|
import { ImmichLogger } from '@app/infra/logger';
|
|
|
|
import { Injectable } from '@nestjs/common';
|
2023-10-24 17:05:42 +02:00
|
|
|
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
|
2023-12-11 20:37:47 +01:00
|
|
|
import { NextFunction, Request, Response } from 'express';
|
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
|
|
|
|
const render = (index: string, meta: OpenGraphTags) => {
|
|
|
|
const tags = `
|
|
|
|
<meta name="description" content="${meta.description}" />
|
|
|
|
|
|
|
|
<!-- Facebook Meta Tags -->
|
|
|
|
<meta property="og:type" content="website" />
|
|
|
|
<meta property="og:title" content="${meta.title}" />
|
|
|
|
<meta property="og:description" content="${meta.description}" />
|
|
|
|
${meta.imageUrl ? `<meta property="og:image" content="${meta.imageUrl}" />` : ''}
|
|
|
|
|
|
|
|
<!-- Twitter Meta Tags -->
|
|
|
|
<meta name="twitter:card" content="summary_large_image" />
|
|
|
|
<meta name="twitter:title" content="${meta.title}" />
|
|
|
|
<meta name="twitter:description" content="${meta.description}" />
|
|
|
|
|
|
|
|
${meta.imageUrl ? `<meta name="twitter:image" content="${meta.imageUrl}" />` : ''}`;
|
|
|
|
|
|
|
|
return index.replace('<!-- metadata:tags -->', tags);
|
|
|
|
};
|
2023-06-02 03:54:16 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AppService {
|
2023-12-14 17:55:40 +01:00
|
|
|
private logger = new ImmichLogger(AppService.name);
|
2023-06-07 16:56:08 +02:00
|
|
|
|
2023-06-02 03:54:16 +02:00
|
|
|
constructor(
|
2023-12-11 20:37:47 +01:00
|
|
|
private authService: AuthService,
|
2023-12-14 17:55:40 +01:00
|
|
|
private configService: SystemConfigService,
|
2023-06-02 03:54:16 +02:00
|
|
|
private jobService: JobService,
|
2023-08-25 06:15:03 +02:00
|
|
|
private serverService: ServerInfoService,
|
2023-12-11 20:37:47 +01:00
|
|
|
private sharedLinkService: SharedLinkService,
|
|
|
|
private storageService: StorageService,
|
2023-06-02 03:54:16 +02:00
|
|
|
) {}
|
|
|
|
|
2023-10-24 17:05:42 +02:00
|
|
|
@Interval(ONE_HOUR.as('milliseconds'))
|
|
|
|
async onVersionCheck() {
|
|
|
|
await this.serverService.handleVersionCheck();
|
|
|
|
}
|
|
|
|
|
2023-06-02 03:54:16 +02:00
|
|
|
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
|
|
|
|
async onNightlyJob() {
|
|
|
|
await this.jobService.handleNightlyJobs();
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2023-12-14 17:55:40 +01:00
|
|
|
await this.configService.init();
|
2023-06-02 03:54:16 +02:00
|
|
|
this.storageService.init();
|
2023-10-24 17:05:42 +02:00
|
|
|
await this.serverService.handleVersionCheck();
|
2023-08-25 06:15:03 +02:00
|
|
|
this.logger.log(`Feature Flags: ${JSON.stringify(await this.serverService.getFeatures(), null, 2)}`);
|
2023-09-02 04:01:54 +02:00
|
|
|
}
|
2023-12-11 20:37:47 +01:00
|
|
|
|
|
|
|
ssr(excludePaths: string[]) {
|
2023-12-11 21:19:27 +01:00
|
|
|
let index = '';
|
|
|
|
try {
|
2023-12-16 18:15:30 +01:00
|
|
|
index = readFileSync(WEB_ROOT_PATH).toString();
|
2023-12-11 21:19:27 +01:00
|
|
|
} catch (error: Error | any) {
|
|
|
|
this.logger.warn('Unable to open `www/index.html, skipping SSR.');
|
|
|
|
}
|
2023-12-11 20:37:47 +01:00
|
|
|
|
|
|
|
return async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
if (
|
|
|
|
req.url.startsWith('/api') ||
|
|
|
|
req.method.toLowerCase() !== 'get' ||
|
|
|
|
excludePaths.find((item) => req.url.startsWith(item))
|
|
|
|
) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const targets = [
|
|
|
|
{
|
|
|
|
regex: /^\/share\/(.+)$/,
|
|
|
|
onMatch: async (matches: RegExpMatchArray) => {
|
|
|
|
const key = matches[1];
|
|
|
|
const auth = await this.authService.validateSharedLink(key);
|
|
|
|
return this.sharedLinkService.getMetadataTags(auth);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
let html = index;
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (const { regex, onMatch } of targets) {
|
|
|
|
const matches = req.url.match(regex);
|
|
|
|
if (matches) {
|
|
|
|
const meta = await onMatch(matches);
|
|
|
|
if (meta) {
|
|
|
|
html = render(index, meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
res.type('text/html').header('Cache-Control', 'no-store').send(html);
|
|
|
|
};
|
|
|
|
}
|
2023-06-02 03:54:16 +02:00
|
|
|
}
|