diff --git a/mobile/openapi/README.md b/mobile/openapi/README.md index 9fa1b4858f..3f3ae2e6a2 100644 --- a/mobile/openapi/README.md +++ b/mobile/openapi/README.md @@ -160,6 +160,7 @@ Class | Method | HTTP request | Description *LibrariesApi* | [**updateLibrary**](doc//LibrariesApi.md#updatelibrary) | **PUT** /libraries/{id} | Update a library *LibrariesApi* | [**validate**](doc//LibrariesApi.md#validate) | **POST** /libraries/{id}/validate | Validate library settings *MaintenanceAdminApi* | [**maintenanceLogin**](doc//MaintenanceAdminApi.md#maintenancelogin) | **POST** /admin/maintenance/login | Log into maintenance mode +*MaintenanceAdminApi* | [**maintenanceStatus**](doc//MaintenanceAdminApi.md#maintenancestatus) | **GET** /admin/maintenance/admin/maintenance/status | Get maintenance mode status *MaintenanceAdminApi* | [**setMaintenanceMode**](doc//MaintenanceAdminApi.md#setmaintenancemode) | **POST** /admin/maintenance | Set maintenance mode *MapApi* | [**getMapMarkers**](doc//MapApi.md#getmapmarkers) | **GET** /map/markers | Retrieve map markers *MapApi* | [**reverseGeocode**](doc//MapApi.md#reversegeocode) | **GET** /map/reverse-geocode | Reverse geocode coordinates @@ -409,6 +410,7 @@ Class | Method | HTTP request | Description - [MaintenanceAction](doc//MaintenanceAction.md) - [MaintenanceAuthDto](doc//MaintenanceAuthDto.md) - [MaintenanceLoginDto](doc//MaintenanceLoginDto.md) + - [MaintenanceStatusResponseDto](doc//MaintenanceStatusResponseDto.md) - [ManualJobName](doc//ManualJobName.md) - [MapMarkerResponseDto](doc//MapMarkerResponseDto.md) - [MapReverseGeocodeResponseDto](doc//MapReverseGeocodeResponseDto.md) diff --git a/mobile/openapi/lib/api.dart b/mobile/openapi/lib/api.dart index a47d9ddf92..db8f3c36bc 100644 --- a/mobile/openapi/lib/api.dart +++ b/mobile/openapi/lib/api.dart @@ -167,6 +167,7 @@ part 'model/machine_learning_availability_checks_dto.dart'; part 'model/maintenance_action.dart'; part 'model/maintenance_auth_dto.dart'; part 'model/maintenance_login_dto.dart'; +part 'model/maintenance_status_response_dto.dart'; part 'model/manual_job_name.dart'; part 'model/map_marker_response_dto.dart'; part 'model/map_reverse_geocode_response_dto.dart'; diff --git a/mobile/openapi/lib/api/maintenance_admin_api.dart b/mobile/openapi/lib/api/maintenance_admin_api.dart index 7e46f96c6e..c5ca7788a7 100644 --- a/mobile/openapi/lib/api/maintenance_admin_api.dart +++ b/mobile/openapi/lib/api/maintenance_admin_api.dart @@ -72,6 +72,54 @@ class MaintenanceAdminApi { return null; } + /// Get maintenance mode status + /// + /// Fetch information about the currently running maintenance action. + /// + /// Note: This method returns the HTTP [Response]. + Future maintenanceStatusWithHttpInfo() async { + // ignore: prefer_const_declarations + final apiPath = r'/admin/maintenance/admin/maintenance/status'; + + // ignore: prefer_final_locals + Object? postBody; + + final queryParams = []; + final headerParams = {}; + final formParams = {}; + + const contentTypes = []; + + + return apiClient.invokeAPI( + apiPath, + 'GET', + queryParams, + postBody, + headerParams, + formParams, + contentTypes.isEmpty ? null : contentTypes.first, + ); + } + + /// Get maintenance mode status + /// + /// Fetch information about the currently running maintenance action. + Future maintenanceStatus() async { + final response = await maintenanceStatusWithHttpInfo(); + if (response.statusCode >= HttpStatus.badRequest) { + throw ApiException(response.statusCode, await _decodeBodyBytes(response)); + } + // When a remote server returns no body with a status of 204, we shall not decode it. + // At the time of writing this, `dart:convert` will throw an "Unexpected end of input" + // FormatException when trying to decode an empty string. + if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) { + return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'MaintenanceStatusResponseDto',) as MaintenanceStatusResponseDto; + + } + return null; + } + /// Set maintenance mode /// /// Put Immich into or take it out of maintenance mode diff --git a/mobile/openapi/lib/api_client.dart b/mobile/openapi/lib/api_client.dart index c0dcf542ef..ed4c8e68cc 100644 --- a/mobile/openapi/lib/api_client.dart +++ b/mobile/openapi/lib/api_client.dart @@ -384,6 +384,8 @@ class ApiClient { return MaintenanceAuthDto.fromJson(value); case 'MaintenanceLoginDto': return MaintenanceLoginDto.fromJson(value); + case 'MaintenanceStatusResponseDto': + return MaintenanceStatusResponseDto.fromJson(value); case 'ManualJobName': return ManualJobNameTypeTransformer().decode(value); case 'MapMarkerResponseDto': diff --git a/mobile/openapi/lib/model/maintenance_status_response_dto.dart b/mobile/openapi/lib/model/maintenance_status_response_dto.dart new file mode 100644 index 0000000000..95954bef4e --- /dev/null +++ b/mobile/openapi/lib/model/maintenance_status_response_dto.dart @@ -0,0 +1,230 @@ +// +// AUTO-GENERATED FILE, DO NOT MODIFY! +// +// @dart=2.18 + +// ignore_for_file: unused_element, unused_import +// ignore_for_file: always_put_required_named_parameters_first +// ignore_for_file: constant_identifier_names +// ignore_for_file: lines_longer_than_80_chars + +part of openapi.api; + +class MaintenanceStatusResponseDto { + /// Returns a new [MaintenanceStatusResponseDto] instance. + MaintenanceStatusResponseDto({ + this.action, + this.error, + this.progress, + this.task, + }); + + MaintenanceStatusResponseDtoActionEnum? action; + + /// + /// Please note: This property should have been non-nullable! Since the specification file + /// does not include a default value (using the "default:" property), however, the generated + /// source code must fall back to having a nullable type. + /// Consider adding a "default:" property in the specification file to hide this note. + /// + String? error; + + /// + /// Please note: This property should have been non-nullable! Since the specification file + /// does not include a default value (using the "default:" property), however, the generated + /// source code must fall back to having a nullable type. + /// Consider adding a "default:" property in the specification file to hide this note. + /// + num? progress; + + /// + /// Please note: This property should have been non-nullable! Since the specification file + /// does not include a default value (using the "default:" property), however, the generated + /// source code must fall back to having a nullable type. + /// Consider adding a "default:" property in the specification file to hide this note. + /// + String? task; + + @override + bool operator ==(Object other) => identical(this, other) || other is MaintenanceStatusResponseDto && + other.action == action && + other.error == error && + other.progress == progress && + other.task == task; + + @override + int get hashCode => + // ignore: unnecessary_parenthesis + (action == null ? 0 : action!.hashCode) + + (error == null ? 0 : error!.hashCode) + + (progress == null ? 0 : progress!.hashCode) + + (task == null ? 0 : task!.hashCode); + + @override + String toString() => 'MaintenanceStatusResponseDto[action=$action, error=$error, progress=$progress, task=$task]'; + + Map toJson() { + final json = {}; + if (this.action != null) { + json[r'action'] = this.action; + } else { + // json[r'action'] = null; + } + if (this.error != null) { + json[r'error'] = this.error; + } else { + // json[r'error'] = null; + } + if (this.progress != null) { + json[r'progress'] = this.progress; + } else { + // json[r'progress'] = null; + } + if (this.task != null) { + json[r'task'] = this.task; + } else { + // json[r'task'] = null; + } + return json; + } + + /// Returns a new [MaintenanceStatusResponseDto] instance and imports its values from + /// [value] if it's a [Map], null otherwise. + // ignore: prefer_constructors_over_static_methods + static MaintenanceStatusResponseDto? fromJson(dynamic value) { + upgradeDto(value, "MaintenanceStatusResponseDto"); + if (value is Map) { + final json = value.cast(); + + return MaintenanceStatusResponseDto( + action: MaintenanceStatusResponseDtoActionEnum.fromJson(json[r'action']), + error: mapValueOfType(json, r'error'), + progress: num.parse('${json[r'progress']}'), + task: mapValueOfType(json, r'task'), + ); + } + return null; + } + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = MaintenanceStatusResponseDto.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } + + static Map mapFromJson(dynamic json) { + final map = {}; + if (json is Map && json.isNotEmpty) { + json = json.cast(); // ignore: parameter_assignments + for (final entry in json.entries) { + final value = MaintenanceStatusResponseDto.fromJson(entry.value); + if (value != null) { + map[entry.key] = value; + } + } + } + return map; + } + + // maps a json object with a list of MaintenanceStatusResponseDto-objects as value to a dart map + static Map> mapListFromJson(dynamic json, {bool growable = false,}) { + final map = >{}; + if (json is Map && json.isNotEmpty) { + // ignore: parameter_assignments + json = json.cast(); + for (final entry in json.entries) { + map[entry.key] = MaintenanceStatusResponseDto.listFromJson(entry.value, growable: growable,); + } + } + return map; + } + + /// The list of required keys that must be present in a JSON. + static const requiredKeys = { + }; +} + + +class MaintenanceStatusResponseDtoActionEnum { + /// Instantiate a new enum with the provided [value]. + const MaintenanceStatusResponseDtoActionEnum._(this.value); + + /// The underlying value of this enum member. + final String value; + + @override + String toString() => value; + + String toJson() => value; + + static const start = MaintenanceStatusResponseDtoActionEnum._(r'start'); + static const end = MaintenanceStatusResponseDtoActionEnum._(r'end'); + static const restoreDatabase = MaintenanceStatusResponseDtoActionEnum._(r'restore_database'); + + /// List of all possible values in this [enum][MaintenanceStatusResponseDtoActionEnum]. + static const values = [ + start, + end, + restoreDatabase, + ]; + + static MaintenanceStatusResponseDtoActionEnum? fromJson(dynamic value) => MaintenanceStatusResponseDtoActionEnumTypeTransformer().decode(value); + + static List listFromJson(dynamic json, {bool growable = false,}) { + final result = []; + if (json is List && json.isNotEmpty) { + for (final row in json) { + final value = MaintenanceStatusResponseDtoActionEnum.fromJson(row); + if (value != null) { + result.add(value); + } + } + } + return result.toList(growable: growable); + } +} + +/// Transformation class that can [encode] an instance of [MaintenanceStatusResponseDtoActionEnum] to String, +/// and [decode] dynamic data back to [MaintenanceStatusResponseDtoActionEnum]. +class MaintenanceStatusResponseDtoActionEnumTypeTransformer { + factory MaintenanceStatusResponseDtoActionEnumTypeTransformer() => _instance ??= const MaintenanceStatusResponseDtoActionEnumTypeTransformer._(); + + const MaintenanceStatusResponseDtoActionEnumTypeTransformer._(); + + String encode(MaintenanceStatusResponseDtoActionEnum data) => data.value; + + /// Decodes a [dynamic value][data] to a MaintenanceStatusResponseDtoActionEnum. + /// + /// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully, + /// then null is returned. However, if [allowNull] is false and the [dynamic value][data] + /// cannot be decoded successfully, then an [UnimplementedError] is thrown. + /// + /// The [allowNull] is very handy when an API changes and a new enum value is added or removed, + /// and users are still using an old app with the old code. + MaintenanceStatusResponseDtoActionEnum? decode(dynamic data, {bool allowNull = true}) { + if (data != null) { + switch (data) { + case r'start': return MaintenanceStatusResponseDtoActionEnum.start; + case r'end': return MaintenanceStatusResponseDtoActionEnum.end; + case r'restore_database': return MaintenanceStatusResponseDtoActionEnum.restoreDatabase; + default: + if (!allowNull) { + throw ArgumentError('Unknown enum value to decode: $data'); + } + } + } + return null; + } + + /// Singleton [MaintenanceStatusResponseDtoActionEnumTypeTransformer] instance. + static MaintenanceStatusResponseDtoActionEnumTypeTransformer? _instance; +} + + diff --git a/open-api/immich-openapi-specs.json b/open-api/immich-openapi-specs.json index eca7d2ab61..c125b3bfb9 100644 --- a/open-api/immich-openapi-specs.json +++ b/open-api/immich-openapi-specs.json @@ -372,6 +372,40 @@ "x-immich-state": "Alpha" } }, + "/admin/maintenance/admin/maintenance/status": { + "get": { + "description": "Fetch information about the currently running maintenance action.", + "operationId": "maintenanceStatus", + "parameters": [], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MaintenanceStatusResponseDto" + } + } + }, + "description": "" + } + }, + "summary": "Get maintenance mode status", + "tags": [ + "Maintenance (admin)" + ], + "x-immich-history": [ + { + "version": "v9.9.9", + "state": "Added" + }, + { + "version": "v9.9.9", + "state": "Alpha" + } + ], + "x-immich-state": "Alpha" + } + }, "/admin/maintenance/login": { "post": { "description": "Login with maintenance token or cookie to receive current information and perform further actions.", @@ -16550,6 +16584,28 @@ }, "type": "object" }, + "MaintenanceStatusResponseDto": { + "properties": { + "action": { + "enum": [ + "start", + "end", + "restore_database" + ], + "type": "string" + }, + "error": { + "type": "string" + }, + "progress": { + "type": "number" + }, + "task": { + "type": "string" + } + }, + "type": "object" + }, "ManualJobName": { "enum": [ "person-cleanup", diff --git a/open-api/typescript-sdk/src/fetch-client.ts b/open-api/typescript-sdk/src/fetch-client.ts index df55263031..8b5afaa904 100644 --- a/open-api/typescript-sdk/src/fetch-client.ts +++ b/open-api/typescript-sdk/src/fetch-client.ts @@ -44,6 +44,12 @@ export type SetMaintenanceModeDto = { action: MaintenanceAction; restoreBackupFilename?: string; }; +export type MaintenanceStatusResponseDto = { + action?: Action; + error?: string; + progress?: number; + task?: string; +}; export type MaintenanceLoginDto = { token?: string; }; @@ -515,7 +521,7 @@ export type AssetBulkUploadCheckDto = { assets: AssetBulkUploadCheckItem[]; }; export type AssetBulkUploadCheckResult = { - action: Action; + action: Action2; assetId?: string; id: string; isTrashed?: boolean; @@ -1845,6 +1851,17 @@ export function setMaintenanceMode({ setMaintenanceModeDto }: { body: setMaintenanceModeDto }))); } +/** + * Get maintenance mode status + */ +export function maintenanceStatus(opts?: Oazapfts.RequestOpts) { + return oazapfts.ok(oazapfts.fetchJson<{ + status: 200; + data: MaintenanceStatusResponseDto; + }>("/admin/maintenance/admin/maintenance/status", { + ...opts + })); +} /** * Log into maintenance mode */ @@ -5057,6 +5074,11 @@ export enum MaintenanceAction { End = "end", RestoreDatabase = "restore_database" } +export enum Action { + Start = "start", + End = "end", + RestoreDatabase = "restore_database" +} export enum NotificationLevel { Success = "success", Error = "error", @@ -5262,7 +5284,7 @@ export enum AssetMediaStatus { Replaced = "replaced", Duplicate = "duplicate" } -export enum Action { +export enum Action2 { Accept = "accept", Reject = "reject" } diff --git a/server/src/controllers/maintenance.controller.ts b/server/src/controllers/maintenance.controller.ts index e7f1dd9a86..9abd5341b2 100644 --- a/server/src/controllers/maintenance.controller.ts +++ b/server/src/controllers/maintenance.controller.ts @@ -1,9 +1,14 @@ -import { BadRequestException, Body, Controller, Post, Res } from '@nestjs/common'; +import { BadRequestException, Body, Controller, Get, Post, Res } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { Response } from 'express'; import { Endpoint, HistoryBuilder } from 'src/decorators'; import { AuthDto } from 'src/dtos/auth.dto'; -import { MaintenanceAuthDto, MaintenanceLoginDto, SetMaintenanceModeDto } from 'src/dtos/maintenance.dto'; +import { + MaintenanceAuthDto, + MaintenanceLoginDto, + MaintenanceStatusResponseDto, + SetMaintenanceModeDto, +} from 'src/dtos/maintenance.dto'; import { ApiTag, ImmichCookie, MaintenanceAction, Permission } from 'src/enum'; import { Auth, Authenticated, GetLoginDetails } from 'src/middleware/auth.guard'; import { LoginDetails } from 'src/services/auth.service'; @@ -15,6 +20,16 @@ import { respondWithCookie } from 'src/utils/response'; export class MaintenanceController { constructor(private service: MaintenanceService) {} + @Get('admin/maintenance/status') + @Endpoint({ + summary: 'Get maintenance mode status', + description: 'Fetch information about the currently running maintenance action.', + history: new HistoryBuilder().added('v9.9.9').alpha('v9.9.9'), + }) + maintenanceStatus(): MaintenanceStatusResponseDto { + return {}; + } + @Post('login') @Endpoint({ summary: 'Log into maintenance mode', diff --git a/server/src/dtos/maintenance.dto.ts b/server/src/dtos/maintenance.dto.ts index 59c4646439..ba40166d45 100644 --- a/server/src/dtos/maintenance.dto.ts +++ b/server/src/dtos/maintenance.dto.ts @@ -17,3 +17,11 @@ export class MaintenanceLoginDto { export class MaintenanceAuthDto { username!: string; } + +export class MaintenanceStatusResponseDto { + action?: MaintenanceAction; + + progress?: number; + task?: string; + error?: string; +} diff --git a/server/src/maintenance/maintenance-ephemeral-state.repository.ts b/server/src/maintenance/maintenance-ephemeral-state.repository.ts index c19ccca17e..56f8c318de 100644 --- a/server/src/maintenance/maintenance-ephemeral-state.repository.ts +++ b/server/src/maintenance/maintenance-ephemeral-state.repository.ts @@ -1,14 +1,34 @@ import { Injectable } from '@nestjs/common'; +import { MaintenanceStatusResponseDto } from 'src/dtos/maintenance.dto'; @Injectable() export class MaintenanceEphemeralStateRepository { #secret: string = null!; + #state: MaintenanceStatusResponseDto = {}; setSecret(secret: string) { this.#secret = secret; } - getSecret() { + getSecret(): string { return this.#secret; } + + setState(state: MaintenanceStatusResponseDto) { + this.#state = state; + } + + getState(): MaintenanceStatusResponseDto { + return this.#state; + } + + getPublicState(): MaintenanceStatusResponseDto { + const state = structuredClone(this.#state); + + if (state.error) { + state.error = 'Something went wrong, see logs!'; + } + + return state; + } } diff --git a/server/src/maintenance/maintenance-worker.controller.ts b/server/src/maintenance/maintenance-worker.controller.ts index e6143b771a..051ffebbac 100644 --- a/server/src/maintenance/maintenance-worker.controller.ts +++ b/server/src/maintenance/maintenance-worker.controller.ts @@ -1,6 +1,11 @@ import { Body, Controller, Get, Post, Req, Res } from '@nestjs/common'; import { Request, Response } from 'express'; -import { MaintenanceAuthDto, MaintenanceLoginDto, SetMaintenanceModeDto } from 'src/dtos/maintenance.dto'; +import { + MaintenanceAuthDto, + MaintenanceLoginDto, + MaintenanceStatusResponseDto, + SetMaintenanceModeDto, +} from 'src/dtos/maintenance.dto'; import { ServerConfigDto } from 'src/dtos/server.dto'; import { ImmichCookie, MaintenanceAction } from 'src/enum'; import { MaintenanceRoute } from 'src/maintenance/maintenance-auth.guard'; @@ -18,6 +23,11 @@ export class MaintenanceWorkerController { return this.service.getSystemConfig(); } + @Get('admin/maintenance/status') + maintenanceStatus(@Req() request: Request): Promise { + return this.service.status(request.cookies[ImmichCookie.MaintenanceToken]); + } + @Post('admin/maintenance/login') async maintenanceLogin( @Req() request: Request, diff --git a/server/src/maintenance/maintenance-worker.service.ts b/server/src/maintenance/maintenance-worker.service.ts index b63e7e5ec4..14089bdc4d 100644 --- a/server/src/maintenance/maintenance-worker.service.ts +++ b/server/src/maintenance/maintenance-worker.service.ts @@ -4,7 +4,7 @@ import { NextFunction, Request, Response } from 'express'; import { jwtVerify } from 'jose'; import { readFileSync } from 'node:fs'; import { IncomingHttpHeaders } from 'node:http'; -import { MaintenanceAuthDto } from 'src/dtos/maintenance.dto'; +import { MaintenanceAuthDto, MaintenanceStatusResponseDto } from 'src/dtos/maintenance.dto'; import { ServerConfigDto } from 'src/dtos/server.dto'; import { ImmichCookie, SystemMetadataKey } from 'src/enum'; import { MaintenanceEphemeralStateRepository } from 'src/maintenance/maintenance-ephemeral-state.repository'; @@ -150,6 +150,15 @@ export class MaintenanceWorkerService { return this.login(jwtToken); } + async status(potentiallyJwt?: string): Promise { + try { + await this.login(potentiallyJwt); + return this.maintenanceEphemeralStateRepository.getState(); + } catch { + return this.maintenanceEphemeralStateRepository.getPublicState(); + } + } + async login(jwt?: string): Promise { if (!jwt) { throw new UnauthorizedException('Missing JWT Token');