mirror of
https://github.com/immich-app/immich.git
synced 2025-01-04 02:46:47 +01:00
fix(cli): upload large file (#5242)
* fix(cli): upload large file * fix: use known configuration * chore: version bump * chore: fix repo url * fix conflict --------- Co-authored-by: Jonathan Jogenfors <jonathan@jogenfors.se>
This commit is contained in:
parent
8ff4a08a2c
commit
c8aa782fef
2 changed files with 39 additions and 6 deletions
|
@ -6,6 +6,8 @@ import { CrawlOptionsDto } from '../cores/dto/crawl-options-dto';
|
||||||
import cliProgress from 'cli-progress';
|
import cliProgress from 'cli-progress';
|
||||||
import byteSize from 'byte-size';
|
import byteSize from 'byte-size';
|
||||||
import { BaseCommand } from '../cli/base-command';
|
import { BaseCommand } from '../cli/base-command';
|
||||||
|
import axios, { AxiosRequestConfig } from 'axios';
|
||||||
|
import FormData from 'form-data';
|
||||||
|
|
||||||
export default class Upload extends BaseCommand {
|
export default class Upload extends BaseCommand {
|
||||||
uploadLength!: number;
|
uploadLength!: number;
|
||||||
|
@ -75,7 +77,8 @@ export default class Upload extends BaseCommand {
|
||||||
|
|
||||||
if (!skipUpload) {
|
if (!skipUpload) {
|
||||||
if (!options.dryRun) {
|
if (!options.dryRun) {
|
||||||
const res = await this.immichApi.assetApi.uploadFile(asset.getUploadFileRequest());
|
const formData = asset.getUploadFormData();
|
||||||
|
const res = await this.uploadAsset(formData);
|
||||||
|
|
||||||
if (options.album && asset.albumName) {
|
if (options.album && asset.albumName) {
|
||||||
let album = existingAlbums.find((album) => album.albumName === asset.albumName);
|
let album = existingAlbums.find((album) => album.albumName === asset.albumName);
|
||||||
|
@ -134,4 +137,24 @@ export default class Upload extends BaseCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async uploadAsset(data: FormData): Promise<axios.AxiosResponse> {
|
||||||
|
const url = this.immichApi.apiConfiguration.instanceUrl + '/asset/upload';
|
||||||
|
|
||||||
|
const config: AxiosRequestConfig = {
|
||||||
|
method: 'post',
|
||||||
|
maxRedirects: 0,
|
||||||
|
url,
|
||||||
|
headers: {
|
||||||
|
'x-api-key': this.immichApi.apiConfiguration.apiKey,
|
||||||
|
...data.getHeaders(),
|
||||||
|
},
|
||||||
|
maxContentLength: Infinity,
|
||||||
|
maxBodyLength: Infinity,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await axios(config);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { basename } from 'node:path';
|
import { basename } from 'node:path';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { AssetApiUploadFileRequest } from 'src/api/open-api';
|
|
||||||
import Os from 'os';
|
import Os from 'os';
|
||||||
|
import FormData from 'form-data';
|
||||||
|
|
||||||
export class Asset {
|
export class Asset {
|
||||||
readonly path: string;
|
readonly path: string;
|
||||||
|
@ -40,22 +40,32 @@ export class Asset {
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
getUploadFileRequest(): AssetApiUploadFileRequest {
|
getUploadFormData(): FormData {
|
||||||
if (!this.assetData) throw new Error('Asset data not set');
|
if (!this.assetData) throw new Error('Asset data not set');
|
||||||
if (!this.deviceAssetId) throw new Error('Device asset id not set');
|
if (!this.deviceAssetId) throw new Error('Device asset id not set');
|
||||||
if (!this.fileCreatedAt) throw new Error('File created at not set');
|
if (!this.fileCreatedAt) throw new Error('File created at not set');
|
||||||
if (!this.fileModifiedAt) throw new Error('File modified at not set');
|
if (!this.fileModifiedAt) throw new Error('File modified at not set');
|
||||||
if (!this.deviceId) throw new Error('Device id not set');
|
if (!this.deviceId) throw new Error('Device id not set');
|
||||||
|
|
||||||
return {
|
const data: any = {
|
||||||
assetData: this.assetData as any,
|
assetData: this.assetData as any,
|
||||||
deviceAssetId: this.deviceAssetId,
|
deviceAssetId: this.deviceAssetId,
|
||||||
deviceId: this.deviceId,
|
deviceId: this.deviceId,
|
||||||
fileCreatedAt: this.fileCreatedAt,
|
fileCreatedAt: this.fileCreatedAt,
|
||||||
fileModifiedAt: this.fileModifiedAt,
|
fileModifiedAt: this.fileModifiedAt,
|
||||||
isFavorite: false,
|
isFavorite: String(false),
|
||||||
sidecarData: this.sidecarData as any,
|
|
||||||
};
|
};
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
for (const prop in data) {
|
||||||
|
formData.append(prop, data[prop]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.sidecarData) {
|
||||||
|
formData.append('sidecarData', this.sidecarData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return formData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getReadStream(path: string): fs.ReadStream {
|
private getReadStream(path: string): fs.ReadStream {
|
||||||
|
|
Loading…
Reference in a new issue