mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
* refactor(server): calculate asset type server-side * chore: open api * chore: remove comments * fix: linting * update * Revert "update" This reverts commit dc58702923250b9385d22468a7afe77dc9972a03. * fix: upload LivePhotos * chore: remove unused request fields for upload * remove unused method * mobile-fix: livePhoto filename * fix: revert check for livephotos filename and extension --------- Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import * as fs from 'fs';
|
|
import { basename } from 'node:path';
|
|
import crypto from 'crypto';
|
|
|
|
export class CrawledAsset {
|
|
public path: string;
|
|
|
|
public assetData?: fs.ReadStream;
|
|
public deviceAssetId?: string;
|
|
public fileCreatedAt?: string;
|
|
public fileModifiedAt?: string;
|
|
public sidecarData?: Buffer;
|
|
public sidecarPath?: string;
|
|
public fileSize!: number;
|
|
public skipped = false;
|
|
|
|
constructor(path: string) {
|
|
this.path = path;
|
|
}
|
|
|
|
async readData() {
|
|
this.assetData = fs.createReadStream(this.path);
|
|
}
|
|
|
|
async process() {
|
|
const stats = await fs.promises.stat(this.path);
|
|
this.deviceAssetId = `${basename(this.path)}-${stats.size}`.replace(/\s+/g, '');
|
|
this.fileCreatedAt = stats.ctime.toISOString();
|
|
this.fileModifiedAt = stats.mtime.toISOString();
|
|
this.fileSize = stats.size;
|
|
|
|
// 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 = await fs.promises.readFile(sideCarPath);
|
|
this.sidecarPath = sideCarPath;
|
|
} catch (error) {}
|
|
}
|
|
|
|
async delete(): Promise<void> {
|
|
return fs.promises.unlink(this.path);
|
|
}
|
|
|
|
public async hash(): Promise<string> {
|
|
const sha1 = (filePath: string) => {
|
|
const hash = crypto.createHash('sha1');
|
|
return new Promise<string>((resolve, reject) => {
|
|
const rs = fs.createReadStream(filePath);
|
|
rs.on('error', reject);
|
|
rs.on('data', (chunk) => hash.update(chunk));
|
|
rs.on('end', () => resolve(hash.digest('hex')));
|
|
});
|
|
};
|
|
|
|
return await sha1(this.path);
|
|
}
|
|
}
|