Skip to content

High performance self-hosted photo and video management solution.

fix(cli): file handles (#6110)

Jason Rasmussen014adf175ad5
diff --git a/cli/package-lock.json b/cli/package-lock.json
index 6b5db0ab26..273034ca77 100644
--- a/cli/package-lock.json
+++ b/cli/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@immich/cli",
- "version": "2.0.5",
+ "version": "2.0.6",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@immich/cli",
- "version": "2.0.5",
+ "version": "2.0.6",
"license": "MIT",
"dependencies": {
"axios": "^1.6.2",
diff --git a/cli/package.json b/cli/package.json
index 35c7340f37..b139950e65 100644
--- a/cli/package.json
+++ b/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@immich/cli",
- "version": "2.0.5",
+ "version": "2.0.6",
"description": "Command Line Interface (CLI) for Immich",
"main": "dist/index.js",
"bin": {
diff --git a/cli/src/commands/upload.ts b/cli/src/commands/upload.ts
index da3146f95e..c76c845f98 100644
--- a/cli/src/commands/upload.ts
+++ b/cli/src/commands/upload.ts
@@ -60,7 +60,7 @@ export default class Upload extends BaseCommand {
for (const asset of assetsToUpload) {
// Compute total size first
- await asset.process();
+ await asset.prepare();
totalSize += asset.fileSize;
if (options.albumName) {
diff --git a/cli/src/cores/models/asset.ts b/cli/src/cores/models/asset.ts
index 378945e6d3..a3e9b8f8e7 100644
--- a/cli/src/cores/models/asset.ts
+++ b/cli/src/cores/models/asset.ts
@@ -1,18 +1,17 @@
+import crypto from 'crypto';
+import FormData from 'form-data';
import * as fs from 'graceful-fs';
+import { createReadStream } from 'node:fs';
import { basename } from 'node:path';
-import crypto from 'crypto';
import Os from 'os';
-import FormData from 'form-data';
export class Asset {
readonly path: string;
readonly deviceId!: string;
- assetData?: fs.ReadStream;
deviceAssetId?: string;
fileCreatedAt?: string;
fileModifiedAt?: string;
- sidecarData?: fs.ReadStream;
sidecarPath?: string;
fileSize!: number;
albumName?: string;
@@ -21,32 +20,30 @@ export class Asset {
this.path = path;
}
- async process() {
+ async prepare() {
const stats = await fs.promises.stat(this.path);
this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replace(/\s+/g, '');
this.fileCreatedAt = stats.mtime.toISOString();
this.fileModifiedAt = stats.mtime.toISOString();
this.fileSize = stats.size;
this.albumName = this.extractAlbumName();
+ }
- this.assetData = this.getReadStream(this.path);
+ getUploadFormData(): FormData {
+ if (!this.deviceAssetId) throw new Error('Device asset id not set');
+ if (!this.fileCreatedAt) throw new Error('File created at not set');
+ if (!this.fileModifiedAt) throw new Error('File modified at not set');
// TODO: doesn't xmp replace the file extension? Will need investigation
const sideCarPath = `${this.path}.xmp`;
+ let sidecarData: fs.ReadStream | undefined = undefined;
try {
fs.accessSync(sideCarPath, fs.constants.R_OK);
- this.sidecarData = this.getReadStream(sideCarPath);
+ sidecarData = createReadStream(sideCarPath);
} catch (error) {}
- }
-
- getUploadFormData(): FormData {
- if (!this.assetData) throw new Error('Asset data 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.fileModifiedAt) throw new Error('File modified at not set');
const data: any = {
- assetData: this.assetData as any,
+ assetData: createReadStream(this.path),
deviceAssetId: this.deviceAssetId,
deviceId: 'CLI',
fileCreatedAt: this.fileCreatedAt,
@@ -59,17 +56,13 @@ export class Asset {
formData.append(prop, data[prop]);
}
- if (this.sidecarData) {
- formData.append('sidecarData', this.sidecarData);
+ if (sidecarData) {
+ formData.append('sidecarData', sidecarData);
}
return formData;
}
- private getReadStream(path: string): fs.ReadStream {
- return fs.createReadStream(path);
- }
-
async delete(): Promise<void> {
return fs.promises.unlink(this.path);
}