2023-10-24 17:05:42 +02:00
|
|
|
import { Inject, Injectable, Logger } from '@nestjs/common';
|
|
|
|
|
import { DateTime } from 'luxon';
|
|
|
|
|
import { ServerVersion, isDev, mimeTypes, serverVersion } from '../domain.constant';
|
2023-03-21 22:49:19 -04:00
|
|
|
import { asHumanReadable } from '../domain.util';
|
2023-10-24 17:05:42 +02:00
|
|
|
import {
|
|
|
|
|
CommunicationEvent,
|
|
|
|
|
ICommunicationRepository,
|
|
|
|
|
IServerInfoRepository,
|
|
|
|
|
IStorageRepository,
|
|
|
|
|
ISystemConfigRepository,
|
|
|
|
|
IUserRepository,
|
|
|
|
|
UserStatsQueryResponse,
|
|
|
|
|
} from '../repositories';
|
2023-10-09 10:25:03 -04:00
|
|
|
import { StorageCore, StorageFolder } from '../storage';
|
|
|
|
|
import { SystemConfigCore } from '../system-config';
|
2023-07-15 20:24:46 -05:00
|
|
|
import {
|
2023-09-08 22:51:46 -04:00
|
|
|
ServerConfigDto,
|
2023-08-18 00:55:26 -04:00
|
|
|
ServerFeaturesDto,
|
2023-07-15 20:24:46 -05:00
|
|
|
ServerInfoResponseDto,
|
|
|
|
|
ServerMediaTypesResponseDto,
|
|
|
|
|
ServerPingResponse,
|
|
|
|
|
ServerStatsResponseDto,
|
|
|
|
|
UsageByUserDto,
|
2023-08-18 00:55:26 -04:00
|
|
|
} from './server-info.dto';
|
2023-03-21 22:49:19 -04:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ServerInfoService {
|
2023-10-24 17:05:42 +02:00
|
|
|
private logger = new Logger(ServerInfoService.name);
|
2023-08-18 00:55:26 -04:00
|
|
|
private configCore: SystemConfigCore;
|
2023-10-24 17:05:42 +02:00
|
|
|
private releaseVersion = serverVersion;
|
|
|
|
|
private releaseVersionCheckedAt: DateTime | null = null;
|
2023-05-24 22:05:31 -04:00
|
|
|
|
2023-03-21 22:49:19 -04:00
|
|
|
constructor(
|
2023-10-24 17:05:42 +02:00
|
|
|
@Inject(ICommunicationRepository) private communicationRepository: ICommunicationRepository,
|
2023-08-18 00:55:26 -04:00
|
|
|
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
|
2023-03-21 22:49:19 -04:00
|
|
|
@Inject(IUserRepository) private userRepository: IUserRepository,
|
2023-10-24 17:05:42 +02:00
|
|
|
@Inject(IServerInfoRepository) private repository: IServerInfoRepository,
|
2023-03-21 22:49:19 -04:00
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
2023-08-18 00:55:26 -04:00
|
|
|
) {
|
2023-10-09 02:51:03 +02:00
|
|
|
this.configCore = SystemConfigCore.create(configRepository);
|
2023-10-24 17:05:42 +02:00
|
|
|
this.communicationRepository.addEventListener('connect', (userId) => this.handleConnect(userId));
|
2023-08-18 00:55:26 -04:00
|
|
|
}
|
2023-03-21 22:49:19 -04:00
|
|
|
|
|
|
|
|
async getInfo(): Promise<ServerInfoResponseDto> {
|
2023-10-14 13:12:59 -04:00
|
|
|
const libraryBase = StorageCore.getBaseFolder(StorageFolder.LIBRARY);
|
2023-05-24 22:05:31 -04:00
|
|
|
const diskInfo = await this.storageRepository.checkDiskUsage(libraryBase);
|
2023-03-21 22:49:19 -04:00
|
|
|
|
|
|
|
|
const usagePercentage = (((diskInfo.total - diskInfo.free) / diskInfo.total) * 100).toFixed(2);
|
|
|
|
|
|
|
|
|
|
const serverInfo = new ServerInfoResponseDto();
|
|
|
|
|
serverInfo.diskAvailable = asHumanReadable(diskInfo.available);
|
|
|
|
|
serverInfo.diskSize = asHumanReadable(diskInfo.total);
|
|
|
|
|
serverInfo.diskUse = asHumanReadable(diskInfo.total - diskInfo.free);
|
|
|
|
|
serverInfo.diskAvailableRaw = diskInfo.available;
|
|
|
|
|
serverInfo.diskSizeRaw = diskInfo.total;
|
|
|
|
|
serverInfo.diskUseRaw = diskInfo.total - diskInfo.free;
|
|
|
|
|
serverInfo.diskUsagePercentage = parseFloat(usagePercentage);
|
|
|
|
|
return serverInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ping(): ServerPingResponse {
|
2023-08-18 00:55:26 -04:00
|
|
|
return { res: 'pong' };
|
2023-03-21 22:49:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getVersion() {
|
|
|
|
|
return serverVersion;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 00:15:03 -04:00
|
|
|
getFeatures(): Promise<ServerFeaturesDto> {
|
|
|
|
|
return this.configCore.getFeatures();
|
2023-08-18 00:55:26 -04:00
|
|
|
}
|
|
|
|
|
|
2023-10-25 15:13:05 -07:00
|
|
|
async getTheme() {
|
|
|
|
|
const { theme } = await this.configCore.getConfig();
|
2023-10-26 19:32:33 -07:00
|
|
|
return theme;
|
2023-10-25 15:13:05 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-08 22:51:46 -04:00
|
|
|
async getConfig(): Promise<ServerConfigDto> {
|
|
|
|
|
const config = await this.configCore.getConfig();
|
|
|
|
|
|
|
|
|
|
// TODO move to system config
|
|
|
|
|
const loginPageMessage = process.env.PUBLIC_LOGIN_PAGE_MESSAGE || '';
|
|
|
|
|
|
2023-10-11 04:37:13 +02:00
|
|
|
const isInitialized = await this.userRepository.hasAdmin();
|
|
|
|
|
|
2023-09-08 22:51:46 -04:00
|
|
|
return {
|
|
|
|
|
loginPageMessage,
|
|
|
|
|
mapTileUrl: config.map.tileUrl,
|
2023-10-06 07:01:14 +00:00
|
|
|
trashDays: config.trash.days,
|
2023-09-08 22:51:46 -04:00
|
|
|
oauthButtonText: config.oauth.buttonText,
|
2023-10-11 04:37:13 +02:00
|
|
|
isInitialized,
|
2023-09-08 22:51:46 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 22:49:19 -04:00
|
|
|
async getStats(): Promise<ServerStatsResponseDto> {
|
|
|
|
|
const userStats: UserStatsQueryResponse[] = await this.userRepository.getUserStats();
|
|
|
|
|
const serverStats = new ServerStatsResponseDto();
|
|
|
|
|
|
|
|
|
|
for (const user of userStats) {
|
|
|
|
|
const usage = new UsageByUserDto();
|
|
|
|
|
usage.userId = user.userId;
|
|
|
|
|
usage.userFirstName = user.userFirstName;
|
|
|
|
|
usage.userLastName = user.userLastName;
|
|
|
|
|
usage.photos = user.photos;
|
|
|
|
|
usage.videos = user.videos;
|
|
|
|
|
usage.usage = user.usage;
|
|
|
|
|
|
|
|
|
|
serverStats.photos += usage.photos;
|
|
|
|
|
serverStats.videos += usage.videos;
|
|
|
|
|
serverStats.usage += usage.usage;
|
|
|
|
|
serverStats.usageByUser.push(usage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serverStats;
|
|
|
|
|
}
|
2023-07-15 20:24:46 -05:00
|
|
|
|
|
|
|
|
getSupportedMediaTypes(): ServerMediaTypesResponseDto {
|
|
|
|
|
return {
|
2023-08-15 12:05:32 -04:00
|
|
|
video: Object.keys(mimeTypes.video),
|
|
|
|
|
image: Object.keys(mimeTypes.image),
|
|
|
|
|
sidecar: Object.keys(mimeTypes.sidecar),
|
2023-07-15 20:24:46 -05:00
|
|
|
};
|
|
|
|
|
}
|
2023-10-24 17:05:42 +02:00
|
|
|
|
|
|
|
|
async handleVersionCheck(): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
if (isDev) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { newVersionCheck } = await this.configCore.getConfig();
|
|
|
|
|
if (!newVersionCheck.enabled) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check once per hour (max)
|
|
|
|
|
if (this.releaseVersionCheckedAt && this.releaseVersionCheckedAt.diffNow().as('minutes') < 60) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const githubRelease = await this.repository.getGitHubRelease();
|
|
|
|
|
const githubVersion = ServerVersion.fromString(githubRelease.tag_name);
|
|
|
|
|
const publishedAt = new Date(githubRelease.published_at);
|
|
|
|
|
this.releaseVersion = githubVersion;
|
|
|
|
|
this.releaseVersionCheckedAt = DateTime.now();
|
|
|
|
|
|
|
|
|
|
if (githubVersion.isNewerThan(serverVersion)) {
|
|
|
|
|
this.logger.log(`Found ${githubVersion.toString()}, released at ${publishedAt.toLocaleString()}`);
|
|
|
|
|
this.newReleaseNotification();
|
|
|
|
|
}
|
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
|
this.logger.warn(`Unable to run version check: ${error}`, error?.stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async handleConnect(userId: string) {
|
|
|
|
|
this.communicationRepository.send(CommunicationEvent.SERVER_VERSION, userId, serverVersion);
|
|
|
|
|
this.newReleaseNotification(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private newReleaseNotification(userId?: string) {
|
|
|
|
|
const event = CommunicationEvent.NEW_RELEASE;
|
|
|
|
|
const payload = {
|
|
|
|
|
isAvailable: this.releaseVersion.isNewerThan(serverVersion),
|
|
|
|
|
checkedAt: this.releaseVersionCheckedAt,
|
|
|
|
|
serverVersion,
|
|
|
|
|
releaseVersion: this.releaseVersion,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
userId
|
|
|
|
|
? this.communicationRepository.send(event, userId, payload)
|
|
|
|
|
: this.communicationRepository.broadcast(event, payload);
|
|
|
|
|
}
|
2023-03-21 22:49:19 -04:00
|
|
|
}
|