1
0
mirror of https://github.com/immich-app/immich.git synced 2025-01-25 17:15:28 +02:00

fix(cli): file handles (#6110)

* fix: file handles

* chore: bump for patch release
This commit is contained in:
Jason Rasmussen 2024-01-01 17:37:52 -05:00 committed by GitHub
parent cc7ba3c21a
commit 014adf175a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 27 deletions

4
cli/package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@immich/cli", "name": "@immich/cli",
"version": "2.0.5", "version": "2.0.6",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@immich/cli", "name": "@immich/cli",
"version": "2.0.5", "version": "2.0.6",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.6.2", "axios": "^1.6.2",

View File

@ -1,6 +1,6 @@
{ {
"name": "@immich/cli", "name": "@immich/cli",
"version": "2.0.5", "version": "2.0.6",
"description": "Command Line Interface (CLI) for Immich", "description": "Command Line Interface (CLI) for Immich",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {

View File

@ -60,7 +60,7 @@ export default class Upload extends BaseCommand {
for (const asset of assetsToUpload) { for (const asset of assetsToUpload) {
// Compute total size first // Compute total size first
await asset.process(); await asset.prepare();
totalSize += asset.fileSize; totalSize += asset.fileSize;
if (options.albumName) { if (options.albumName) {

View File

@ -1,18 +1,17 @@
import * as fs from 'graceful-fs';
import { basename } from 'node:path';
import crypto from 'crypto'; import crypto from 'crypto';
import Os from 'os';
import FormData from 'form-data'; import FormData from 'form-data';
import * as fs from 'graceful-fs';
import { createReadStream } from 'node:fs';
import { basename } from 'node:path';
import Os from 'os';
export class Asset { export class Asset {
readonly path: string; readonly path: string;
readonly deviceId!: string; readonly deviceId!: string;
assetData?: fs.ReadStream;
deviceAssetId?: string; deviceAssetId?: string;
fileCreatedAt?: string; fileCreatedAt?: string;
fileModifiedAt?: string; fileModifiedAt?: string;
sidecarData?: fs.ReadStream;
sidecarPath?: string; sidecarPath?: string;
fileSize!: number; fileSize!: number;
albumName?: string; albumName?: string;
@ -21,32 +20,30 @@ export class Asset {
this.path = path; this.path = path;
} }
async process() { async prepare() {
const stats = await fs.promises.stat(this.path); const stats = await fs.promises.stat(this.path);
this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replace(/\s+/g, ''); this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replace(/\s+/g, '');
this.fileCreatedAt = stats.mtime.toISOString(); this.fileCreatedAt = stats.mtime.toISOString();
this.fileModifiedAt = stats.mtime.toISOString(); this.fileModifiedAt = stats.mtime.toISOString();
this.fileSize = stats.size; this.fileSize = stats.size;
this.albumName = this.extractAlbumName(); this.albumName = this.extractAlbumName();
this.assetData = this.getReadStream(this.path);
// TODO: doesn't xmp replace the file extension? Will need investigation
const sideCarPath = `${this.path}.xmp`;
try {
fs.accessSync(sideCarPath, fs.constants.R_OK);
this.sidecarData = this.getReadStream(sideCarPath);
} catch (error) {}
} }
getUploadFormData(): FormData { 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.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');
// 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);
sidecarData = createReadStream(sideCarPath);
} catch (error) {}
const data: any = { const data: any = {
assetData: this.assetData as any, assetData: createReadStream(this.path),
deviceAssetId: this.deviceAssetId, deviceAssetId: this.deviceAssetId,
deviceId: 'CLI', deviceId: 'CLI',
fileCreatedAt: this.fileCreatedAt, fileCreatedAt: this.fileCreatedAt,
@ -59,17 +56,13 @@ export class Asset {
formData.append(prop, data[prop]); formData.append(prop, data[prop]);
} }
if (this.sidecarData) { if (sidecarData) {
formData.append('sidecarData', this.sidecarData); formData.append('sidecarData', sidecarData);
} }
return formData; return formData;
} }
private getReadStream(path: string): fs.ReadStream {
return fs.createReadStream(path);
}
async delete(): Promise<void> { async delete(): Promise<void> {
return fs.promises.unlink(this.path); return fs.promises.unlink(this.path);
} }