2024-02-02 04:18:00 +01:00
|
|
|
import { existsSync } from 'node:fs';
|
2024-01-30 07:55:34 -05:00
|
|
|
import { access, constants, mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
2023-07-06 16:37:47 +02:00
|
|
|
import path from 'node:path';
|
2024-01-30 07:55:34 -05:00
|
|
|
import yaml from 'yaml';
|
|
|
|
|
import { ImmichApi } from './api.service';
|
|
|
|
|
|
|
|
|
|
class LoginError extends Error {
|
|
|
|
|
constructor(message: string) {
|
|
|
|
|
super(message);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-06 16:37:47 +02:00
|
|
|
|
|
|
|
|
export class SessionService {
|
2024-02-02 04:18:00 +01:00
|
|
|
readonly configDirectory!: string;
|
2023-07-06 16:37:47 +02:00
|
|
|
readonly authPath!: string;
|
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
constructor(configDirectory: string) {
|
|
|
|
|
this.configDirectory = configDirectory;
|
|
|
|
|
this.authPath = path.join(configDirectory, '/auth.yml');
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
async connect(): Promise<ImmichApi> {
|
2023-12-19 03:29:26 +01:00
|
|
|
let instanceUrl = process.env.IMMICH_INSTANCE_URL;
|
|
|
|
|
let apiKey = process.env.IMMICH_API_KEY;
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2023-12-19 03:29:26 +01:00
|
|
|
if (!instanceUrl || !apiKey) {
|
2024-01-30 07:55:34 -05:00
|
|
|
await access(this.authPath, constants.F_OK).catch((error) => {
|
2023-12-19 03:29:26 +01:00
|
|
|
if (error.code === 'ENOENT') {
|
|
|
|
|
throw new LoginError('No auth file exist. Please login first');
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
const data: string = await readFile(this.authPath, 'utf8');
|
2023-12-19 03:29:26 +01:00
|
|
|
const parsedConfig = yaml.parse(data);
|
|
|
|
|
|
|
|
|
|
instanceUrl = parsedConfig.instanceUrl;
|
|
|
|
|
apiKey = parsedConfig.apiKey;
|
|
|
|
|
|
|
|
|
|
if (!instanceUrl) {
|
|
|
|
|
throw new LoginError(`Instance URL missing in auth config file ${this.authPath}`);
|
|
|
|
|
}
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2023-12-19 03:29:26 +01:00
|
|
|
if (!apiKey) {
|
|
|
|
|
throw new LoginError(`API key missing in auth config file ${this.authPath}`);
|
|
|
|
|
}
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
const api = new ImmichApi(instanceUrl, apiKey);
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2024-02-05 11:29:00 -08:00
|
|
|
const pingResponse = await api.serverInfoApi.pingServer().catch((error) => {
|
2024-01-30 07:55:34 -05:00
|
|
|
throw new Error(`Failed to connect to server ${api.instanceUrl}: ${error.message}`);
|
|
|
|
|
});
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
if (pingResponse.res !== 'pong') {
|
|
|
|
|
throw new Error(`Could not parse response. Is Immich listening on ${api.instanceUrl}?`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return api;
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
async login(instanceUrl: string, apiKey: string): Promise<ImmichApi> {
|
|
|
|
|
console.log('Logging in...');
|
|
|
|
|
|
|
|
|
|
const api = new ImmichApi(instanceUrl, apiKey);
|
2023-07-06 16:37:47 +02:00
|
|
|
|
|
|
|
|
// Check if server and api key are valid
|
2024-02-05 11:29:00 -08:00
|
|
|
const userInfo = await api.userApi.getMyUserInfo().catch((error) => {
|
2023-11-19 23:16:24 +01:00
|
|
|
throw new LoginError(`Failed to connect to server ${instanceUrl}: ${error.message}`);
|
2023-07-06 16:37:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`Logged in as ${userInfo.email}`);
|
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
if (!existsSync(this.configDirectory)) {
|
2023-07-06 16:37:47 +02:00
|
|
|
// Create config folder if it doesn't exist
|
2024-02-02 04:18:00 +01:00
|
|
|
const created = await mkdir(this.configDirectory, { recursive: true });
|
2023-10-13 07:22:40 +02:00
|
|
|
if (!created) {
|
2024-02-02 04:18:00 +01:00
|
|
|
throw new Error(`Failed to create config folder ${this.configDirectory}`);
|
2023-10-13 07:22:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 00:40:22 +01:00
|
|
|
await writeFile(this.authPath, yaml.stringify({ instanceUrl, apiKey }), { mode: 0o600 });
|
2023-07-06 16:37:47 +02:00
|
|
|
|
|
|
|
|
console.log('Wrote auth info to ' + this.authPath);
|
|
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
return api;
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
async logout(): Promise<void> {
|
|
|
|
|
console.log('Logging out...');
|
2023-07-06 16:37:47 +02:00
|
|
|
|
2024-01-30 07:55:34 -05:00
|
|
|
if (existsSync(this.authPath)) {
|
|
|
|
|
await unlink(this.authPath);
|
|
|
|
|
console.log('Removed auth file ' + this.authPath);
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
2024-01-30 07:55:34 -05:00
|
|
|
|
|
|
|
|
console.log('Successfully logged out');
|
2023-07-06 16:37:47 +02:00
|
|
|
}
|
|
|
|
|
}
|