1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00: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",
"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",

View file

@ -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": {

View file

@ -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) {

View file

@ -1,18 +1,17 @@
import * as fs from 'graceful-fs';
import { basename } from 'node:path';
import crypto from 'crypto';
import Os from 'os';
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 {
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);
// 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 {
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');
// 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 = {
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);
}