2023-11-19 23:16:24 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import { basename } from 'node:path';
|
|
|
|
|
import crypto from 'crypto';
|
|
|
|
|
import Os from 'os';
|
2023-11-21 15:52:12 -06:00
|
|
|
import FormData from 'form-data';
|
2023-11-19 23:16:24 +01:00
|
|
|
|
|
|
|
|
export class Asset {
|
|
|
|
|
readonly path: string;
|
|
|
|
|
readonly deviceId!: string;
|
|
|
|
|
|
2023-11-21 15:09:19 -06:00
|
|
|
assetData?: fs.ReadStream;
|
2023-11-19 23:16:24 +01:00
|
|
|
deviceAssetId?: string;
|
|
|
|
|
fileCreatedAt?: string;
|
|
|
|
|
fileModifiedAt?: string;
|
2023-11-21 15:09:19 -06:00
|
|
|
sidecarData?: fs.ReadStream;
|
2023-11-19 23:16:24 +01:00
|
|
|
sidecarPath?: string;
|
|
|
|
|
fileSize!: number;
|
|
|
|
|
albumName?: string;
|
|
|
|
|
|
2023-12-19 03:29:26 +01:00
|
|
|
constructor(path: string) {
|
2023-11-19 23:16:24 +01:00
|
|
|
this.path = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async process() {
|
|
|
|
|
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();
|
|
|
|
|
|
2023-11-21 15:09:19 -06:00
|
|
|
this.assetData = this.getReadStream(this.path);
|
2023-11-19 23:16:24 +01:00
|
|
|
|
|
|
|
|
// TODO: doesn't xmp replace the file extension? Will need investigation
|
|
|
|
|
const sideCarPath = `${this.path}.xmp`;
|
|
|
|
|
try {
|
|
|
|
|
fs.accessSync(sideCarPath, fs.constants.R_OK);
|
2023-11-21 15:09:19 -06:00
|
|
|
this.sidecarData = this.getReadStream(sideCarPath);
|
2023-11-19 23:16:24 +01:00
|
|
|
} catch (error) {}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 15:52:12 -06:00
|
|
|
getUploadFormData(): FormData {
|
2023-11-19 23:16:24 +01:00
|
|
|
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');
|
|
|
|
|
|
2023-11-21 15:52:12 -06:00
|
|
|
const data: any = {
|
2023-11-21 15:09:19 -06:00
|
|
|
assetData: this.assetData as any,
|
2023-11-19 23:16:24 +01:00
|
|
|
deviceAssetId: this.deviceAssetId,
|
2023-12-19 03:29:26 +01:00
|
|
|
deviceId: 'CLI',
|
2023-11-19 23:16:24 +01:00
|
|
|
fileCreatedAt: this.fileCreatedAt,
|
|
|
|
|
fileModifiedAt: this.fileModifiedAt,
|
2023-11-21 15:52:12 -06:00
|
|
|
isFavorite: String(false),
|
2023-11-19 23:16:24 +01:00
|
|
|
};
|
2023-11-21 15:52:12 -06:00
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
for (const prop in data) {
|
|
|
|
|
formData.append(prop, data[prop]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.sidecarData) {
|
|
|
|
|
formData.append('sidecarData', this.sidecarData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formData;
|
2023-11-19 23:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 15:09:19 -06:00
|
|
|
private getReadStream(path: string): fs.ReadStream {
|
|
|
|
|
return fs.createReadStream(path);
|
2023-11-19 23:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extractAlbumName(): string {
|
|
|
|
|
if (Os.platform() === 'win32') {
|
|
|
|
|
return this.path.split('\\').slice(-2)[0];
|
|
|
|
|
} else {
|
|
|
|
|
return this.path.split('/').slice(-2)[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|