mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
* feat(cli): use immich-well-known * chore: e2e test --------- Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
import { existsSync } from 'node:fs';
|
|
import { access, constants, mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
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);
|
|
}
|
|
}
|
|
|
|
export class SessionService {
|
|
private get authPath() {
|
|
return path.join(this.configDirectory, '/auth.yml');
|
|
}
|
|
|
|
constructor(private configDirectory: string) {}
|
|
|
|
async connect(): Promise<ImmichApi> {
|
|
let instanceUrl = process.env.IMMICH_INSTANCE_URL;
|
|
let apiKey = process.env.IMMICH_API_KEY;
|
|
|
|
if (!instanceUrl || !apiKey) {
|
|
await access(this.authPath, constants.F_OK).catch((error) => {
|
|
if (error.code === 'ENOENT') {
|
|
throw new LoginError('No auth file exist. Please login first');
|
|
}
|
|
});
|
|
|
|
const data: string = await readFile(this.authPath, 'utf8');
|
|
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}`);
|
|
}
|
|
|
|
if (!apiKey) {
|
|
throw new LoginError(`API key missing in auth config file ${this.authPath}`);
|
|
}
|
|
}
|
|
|
|
instanceUrl = await this.resolveApiEndpoint(instanceUrl);
|
|
|
|
const api = new ImmichApi(instanceUrl, apiKey);
|
|
|
|
const pingResponse = await api.pingServer().catch((error) => {
|
|
throw new Error(`Failed to connect to server ${instanceUrl}: ${error.message}`, error);
|
|
});
|
|
|
|
if (pingResponse.res !== 'pong') {
|
|
throw new Error(`Could not parse response. Is Immich listening on ${instanceUrl}?`);
|
|
}
|
|
|
|
return api;
|
|
}
|
|
|
|
async login(instanceUrl: string, apiKey: string): Promise<ImmichApi> {
|
|
console.log(`Logging in to ${instanceUrl}`);
|
|
|
|
instanceUrl = await this.resolveApiEndpoint(instanceUrl);
|
|
|
|
const api = new ImmichApi(instanceUrl, apiKey);
|
|
|
|
// Check if server and api key are valid
|
|
const userInfo = await api.getMyUserInfo().catch((error) => {
|
|
throw new LoginError(`Failed to connect to server ${instanceUrl}: ${error.message}`);
|
|
});
|
|
|
|
console.log(`Logged in as ${userInfo.email}`);
|
|
|
|
if (!existsSync(this.configDirectory)) {
|
|
// Create config folder if it doesn't exist
|
|
const created = await mkdir(this.configDirectory, { recursive: true });
|
|
if (!created) {
|
|
throw new Error(`Failed to create config folder ${this.configDirectory}`);
|
|
}
|
|
}
|
|
|
|
await writeFile(this.authPath, yaml.stringify({ instanceUrl, apiKey }), { mode: 0o600 });
|
|
|
|
console.log(`Wrote auth info to ${this.authPath}`);
|
|
|
|
return api;
|
|
}
|
|
|
|
async logout(): Promise<void> {
|
|
console.log('Logging out...');
|
|
|
|
if (existsSync(this.authPath)) {
|
|
await unlink(this.authPath);
|
|
console.log('Removed auth file ' + this.authPath);
|
|
}
|
|
|
|
console.log('Successfully logged out');
|
|
}
|
|
|
|
private async resolveApiEndpoint(instanceUrl: string): Promise<string> {
|
|
const wellKnownUrl = new URL('.well-known/immich', instanceUrl);
|
|
try {
|
|
const wellKnown = await fetch(wellKnownUrl).then((response) => response.json());
|
|
const endpoint = new URL(wellKnown.api.endpoint, instanceUrl).toString();
|
|
if (endpoint !== instanceUrl) {
|
|
console.debug(`Discovered API at ${endpoint}`);
|
|
}
|
|
return endpoint;
|
|
} catch {
|
|
return instanceUrl;
|
|
}
|
|
}
|
|
}
|