mirror of
https://github.com/immich-app/immich.git
synced 2026-02-07 12:29:01 -05:00
* Allow building and installing cli * feat: add format fix * docs: remove cli folder * feat: use immich scoped package * feat: rewrite cli readme * docs: add info on running without building * cleanup * chore: remove import functionality from cli * feat: add logout to cli * docs: add todo for file format from server * docs: add compilation step to cli * fix: success message spacing * feat: can create albums * fix: add check step to cli * fix: typos * feat: pull file formats from server * chore: use crawl service from server * chore: fix lint * docs: add cli documentation * chore: rename ignore pattern * chore: add version number to cli * feat: use sdk * fix: cleanup * feat: album name on windows * chore: remove skipped asset field * feat: add more info to server-info command * chore: cleanup * chore: remove unneeded packages * chore: fix docs links * feat: add cli v2 milestone * fix: set correct cli date --------- Co-authored-by: Alex <alex.tran1502@gmail.com>
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import fs from 'node:fs';
|
|
import yaml from 'yaml';
|
|
import path from 'node:path';
|
|
import { ImmichApi } from '../api/client';
|
|
import { LoginError } from '../cores/errors/login-error';
|
|
|
|
export class SessionService {
|
|
readonly configDir: string;
|
|
readonly authPath!: string;
|
|
private api!: ImmichApi;
|
|
|
|
constructor(configDir: string) {
|
|
this.configDir = configDir;
|
|
this.authPath = path.join(this.configDir, 'auth.yml');
|
|
}
|
|
|
|
public async connect(): Promise<ImmichApi> {
|
|
await fs.promises.access(this.authPath, fs.constants.F_OK).catch((error) => {
|
|
if (error.code === 'ENOENT') {
|
|
throw new LoginError('No auth file exist. Please login first');
|
|
}
|
|
});
|
|
|
|
const data: string = await fs.promises.readFile(this.authPath, 'utf8');
|
|
const parsedConfig = yaml.parse(data);
|
|
const instanceUrl: string = parsedConfig.instanceUrl;
|
|
const apiKey: string = 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);
|
|
}
|
|
|
|
this.api = new ImmichApi(instanceUrl, apiKey);
|
|
|
|
await this.ping();
|
|
|
|
return this.api;
|
|
}
|
|
|
|
public async keyLogin(instanceUrl: string, apiKey: string): Promise<ImmichApi> {
|
|
this.api = new ImmichApi(instanceUrl, apiKey);
|
|
|
|
// Check if server and api key are valid
|
|
const { data: userInfo } = await this.api.userApi.getMyUserInfo().catch((error) => {
|
|
throw new LoginError(`Failed to connect to server ${instanceUrl}: ${error.message}`);
|
|
});
|
|
|
|
console.log(`Logged in as ${userInfo.email}`);
|
|
|
|
if (!fs.existsSync(this.configDir)) {
|
|
// Create config folder if it doesn't exist
|
|
const created = await fs.promises.mkdir(this.configDir, { recursive: true });
|
|
if (!created) {
|
|
throw new Error(`Failed to create config folder ${this.configDir}`);
|
|
}
|
|
}
|
|
|
|
if (!fs.existsSync(this.configDir)) {
|
|
console.error('waah');
|
|
}
|
|
|
|
fs.writeFileSync(this.authPath, yaml.stringify({ instanceUrl, apiKey }));
|
|
|
|
console.log('Wrote auth info to ' + this.authPath);
|
|
return this.api;
|
|
}
|
|
|
|
public async logout(): Promise<void> {
|
|
if (fs.existsSync(this.authPath)) {
|
|
fs.unlinkSync(this.authPath);
|
|
console.log('Removed auth file ' + this.authPath);
|
|
}
|
|
}
|
|
|
|
private async ping(): Promise<void> {
|
|
const { data: pingResponse } = await this.api.serverInfoApi.pingServer().catch((error) => {
|
|
throw new Error(`Failed to connect to server ${this.api.apiConfiguration.instanceUrl}: ${error.message}`);
|
|
});
|
|
|
|
if (pingResponse.res !== 'pong') {
|
|
throw new Error('Unexpected ping reply');
|
|
}
|
|
}
|
|
}
|