mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
chore: remove axios dependency from CLI (#6888)
This commit is contained in:
parent
c29976cd6f
commit
6ed33da2a4
218 changed files with 22789 additions and 237 deletions
|
|
@ -3,9 +3,9 @@ import { BaseCommand } from './base-command';
|
|||
export class ServerInfoCommand extends BaseCommand {
|
||||
public async run() {
|
||||
await this.connect();
|
||||
const { data: versionInfo } = await this.immichApi.serverInfoApi.getServerVersion();
|
||||
const { data: mediaTypes } = await this.immichApi.serverInfoApi.getSupportedMediaTypes();
|
||||
const { data: statistics } = await this.immichApi.assetApi.getAssetStatistics();
|
||||
const versionInfo = await this.immichApi.serverInfoApi.getServerVersion();
|
||||
const mediaTypes = await this.immichApi.serverInfoApi.getSupportedMediaTypes();
|
||||
const statistics = await this.immichApi.assetApi.getAssetStatistics();
|
||||
|
||||
console.log(`Server Version: ${versionInfo.major}.${versionInfo.minor}.${versionInfo.patch}`);
|
||||
console.log(`Image Types: ${mediaTypes.image.map((extension) => extension.replace('.', ''))}`);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
import byteSize from 'byte-size';
|
||||
import cliProgress from 'cli-progress';
|
||||
import fs, { createReadStream } from 'node:fs';
|
||||
|
|
@ -114,7 +113,7 @@ export class UploadCommand extends BaseCommand {
|
|||
await this.connect();
|
||||
|
||||
const formatResponse = await this.immichApi.serverInfoApi.getSupportedMediaTypes();
|
||||
const crawlService = new CrawlService(formatResponse.data.image, formatResponse.data.video);
|
||||
const crawlService = new CrawlService(formatResponse.image, formatResponse.video);
|
||||
|
||||
const inputFiles: string[] = [];
|
||||
for (const pathArgument of paths) {
|
||||
|
|
@ -163,7 +162,7 @@ export class UploadCommand extends BaseCommand {
|
|||
}
|
||||
}
|
||||
|
||||
const { data: existingAlbums } = await this.immichApi.albumApi.getAllAlbums();
|
||||
const existingAlbums = await this.immichApi.albumApi.getAllAlbums();
|
||||
|
||||
uploadProgress.start(totalSize, 0);
|
||||
uploadProgress.update({ value_formatted: 0, total_formatted: byteSize(totalSize) });
|
||||
|
|
@ -186,11 +185,11 @@ export class UploadCommand extends BaseCommand {
|
|||
assetBulkUploadCheckDto,
|
||||
});
|
||||
|
||||
skipUpload = checkResponse.data.results[0].action === 'reject';
|
||||
skipUpload = checkResponse.results[0].action === 'reject';
|
||||
|
||||
const isDuplicate = checkResponse.data.results[0].reason === 'duplicate';
|
||||
const isDuplicate = checkResponse.results[0].reason === 'duplicate';
|
||||
if (isDuplicate) {
|
||||
existingAssetId = checkResponse.data.results[0].assetId;
|
||||
existingAssetId = checkResponse.results[0].assetId;
|
||||
}
|
||||
|
||||
skipAsset = skipUpload && !isDuplicate;
|
||||
|
|
@ -199,8 +198,9 @@ export class UploadCommand extends BaseCommand {
|
|||
if (!skipAsset && !options.dryRun) {
|
||||
if (!skipUpload) {
|
||||
const formData = await asset.getUploadFormData();
|
||||
const { data } = await this.uploadAsset(formData);
|
||||
existingAssetId = data.id;
|
||||
const response = await this.uploadAsset(formData);
|
||||
const json = await response.json();
|
||||
existingAssetId = json.id;
|
||||
uploadCounter++;
|
||||
totalSizeUploaded += asset.fileSize;
|
||||
}
|
||||
|
|
@ -208,10 +208,10 @@ export class UploadCommand extends BaseCommand {
|
|||
if ((options.album || options.albumName) && asset.albumName !== undefined) {
|
||||
let album = existingAlbums.find((album) => album.albumName === asset.albumName);
|
||||
if (!album) {
|
||||
const { data } = await this.immichApi.albumApi.createAlbum({
|
||||
const response = await this.immichApi.albumApi.createAlbum({
|
||||
createAlbumDto: { albumName: asset.albumName },
|
||||
});
|
||||
album = data;
|
||||
album = response;
|
||||
existingAlbums.push(album);
|
||||
}
|
||||
|
||||
|
|
@ -259,21 +259,20 @@ export class UploadCommand extends BaseCommand {
|
|||
}
|
||||
}
|
||||
|
||||
private async uploadAsset(data: FormData): Promise<AxiosResponse> {
|
||||
private async uploadAsset(data: FormData): Promise<Response> {
|
||||
const url = this.immichApi.instanceUrl + '/asset/upload';
|
||||
|
||||
const config: AxiosRequestConfig = {
|
||||
const response = await fetch(url, {
|
||||
method: 'post',
|
||||
maxRedirects: 0,
|
||||
url,
|
||||
redirect: 'error',
|
||||
headers: {
|
||||
'x-api-key': this.immichApi.apiKey,
|
||||
},
|
||||
maxContentLength: Number.POSITIVE_INFINITY,
|
||||
maxBodyLength: Number.POSITIVE_INFINITY,
|
||||
data,
|
||||
};
|
||||
|
||||
return axios(config);
|
||||
body: data,
|
||||
});
|
||||
if (response.status !== 200 && response.status !== 201) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import {
|
|||
SystemConfigApi,
|
||||
UserApi,
|
||||
} from '@immich/sdk';
|
||||
import FormData from 'form-data';
|
||||
|
||||
export class ImmichApi {
|
||||
public userApi: UserApi;
|
||||
|
|
@ -31,12 +30,9 @@ export class ImmichApi {
|
|||
) {
|
||||
this.config = new Configuration({
|
||||
basePath: instanceUrl,
|
||||
baseOptions: {
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
},
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
},
|
||||
formDataCtor: FormData,
|
||||
});
|
||||
|
||||
this.userApi = new UserApi(this.config);
|
||||
|
|
@ -52,6 +48,9 @@ export class ImmichApi {
|
|||
|
||||
setApiKey(apiKey: string) {
|
||||
this.apiKey = apiKey;
|
||||
this.config.baseOptions.headers['x-api-key'] = apiKey;
|
||||
if (!this.config.headers) {
|
||||
throw new Error('missing headers');
|
||||
}
|
||||
this.config.headers['x-api-key'] = apiKey;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import {
|
|||
spyOnConsole,
|
||||
} from '../../test/cli-test-utils';
|
||||
|
||||
const mockPingServer = vi.fn(() => Promise.resolve({ data: { res: 'pong' } }));
|
||||
const mockUserInfo = vi.fn(() => Promise.resolve({ data: { email: 'admin@example.com' } }));
|
||||
const mockPingServer = vi.fn(() => Promise.resolve({ res: 'pong' }));
|
||||
const mockUserInfo = vi.fn(() => Promise.resolve({ email: 'admin@example.com' }));
|
||||
|
||||
vi.mock('@immich/sdk', async () => ({
|
||||
...(await vi.importActual('@immich/sdk')),
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ export class SessionService {
|
|||
|
||||
const api = new ImmichApi(instanceUrl, apiKey);
|
||||
|
||||
const { data: pingResponse } = await api.serverInfoApi.pingServer().catch((error) => {
|
||||
const pingResponse = await api.serverInfoApi.pingServer().catch((error) => {
|
||||
throw new Error(`Failed to connect to server ${api.instanceUrl}: ${error.message}`);
|
||||
});
|
||||
|
||||
|
|
@ -68,7 +68,7 @@ export class SessionService {
|
|||
const api = new ImmichApi(instanceUrl, apiKey);
|
||||
|
||||
// Check if server and api key are valid
|
||||
const { data: userInfo } = await api.userApi.getMyUserInfo().catch((error) => {
|
||||
const userInfo = await api.userApi.getMyUserInfo().catch((error) => {
|
||||
throw new LoginError(`Failed to connect to server ${instanceUrl}: ${error.message}`);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue