2023-03-03 15:54:28 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2024-03-14 19:25:27 +00:00
|
|
|
import { column, hasMany, belongsTo, SnakeCaseNamingStrategy, computed } from '@adonisjs/lucid/orm';
|
2024-04-30 09:50:50 +00:00
|
|
|
import HashValue from './hash_value.js';
|
|
|
|
import Dataset from './dataset.js';
|
|
|
|
import BaseModel from './base_model.js';
|
2023-11-22 16:06:55 +00:00
|
|
|
// import { Buffer } from 'buffer';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import crypto from 'crypto';
|
2024-03-14 19:25:27 +00:00
|
|
|
// import Drive from '@ioc:Adonis/Core/Drive';
|
2024-04-23 17:36:45 +00:00
|
|
|
// import Drive from '@adonisjs/drive';
|
|
|
|
import drive from '#services/drive';
|
2024-03-14 19:25:27 +00:00
|
|
|
|
|
|
|
import type { HasMany } from "@adonisjs/lucid/types/relations";
|
|
|
|
import type { BelongsTo } from "@adonisjs/lucid/types/relations";
|
|
|
|
// import { TransactionClientContract } from "@adonisjs/lucid/database";
|
|
|
|
import { TransactionClientContract } from '@adonisjs/lucid/types/database';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
export default class File extends BaseModel {
|
2023-11-22 16:06:55 +00:00
|
|
|
// private readonly _data: Uint8Array;
|
|
|
|
// private readonly _type: string;
|
|
|
|
// private readonly _size: number;
|
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
public static namingStrategy = new SnakeCaseNamingStrategy();
|
|
|
|
public static primaryKey = 'id';
|
|
|
|
public static table = 'document_files';
|
|
|
|
public static selfAssignPrimaryKey = false;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({
|
|
|
|
isPrimary: true,
|
|
|
|
})
|
|
|
|
public id: number;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-27 16:23:18 +00:00
|
|
|
@column({})
|
|
|
|
public document_id: number;
|
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({})
|
|
|
|
public pathName: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column()
|
|
|
|
public label: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column()
|
2023-06-22 15:20:04 +00:00
|
|
|
public comment: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column()
|
2023-09-04 11:24:58 +00:00
|
|
|
public mimeType: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column()
|
2023-06-22 15:20:04 +00:00
|
|
|
public language: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column()
|
2023-09-04 11:24:58 +00:00
|
|
|
public fileSize: number;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column()
|
2023-06-22 15:20:04 +00:00
|
|
|
public visibleInOai: boolean;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-09-04 11:24:58 +00:00
|
|
|
@column()
|
|
|
|
public visibleInFrontdoor: boolean;
|
|
|
|
|
2023-03-03 15:54:28 +00:00
|
|
|
@column()
|
2023-06-22 15:20:04 +00:00
|
|
|
public sortOrder: number;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@column.dateTime({ autoCreate: true })
|
2023-06-22 15:20:04 +00:00
|
|
|
public createdAt: DateTime;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
|
|
|
public updatedAt: DateTime;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-27 16:23:18 +00:00
|
|
|
// public function dataset()
|
2023-03-03 15:54:28 +00:00
|
|
|
// {
|
2023-06-27 16:23:18 +00:00
|
|
|
// return $this->belongsTo(Dataset::class, 'document_id', 'id');
|
2023-03-03 15:54:28 +00:00
|
|
|
// }
|
2023-06-27 16:23:18 +00:00
|
|
|
@belongsTo(() => Dataset, {
|
|
|
|
foreignKey: 'document_id',
|
|
|
|
})
|
|
|
|
public dataset: BelongsTo<typeof Dataset>;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
@hasMany(() => HashValue, {
|
|
|
|
foreignKey: 'file_id',
|
|
|
|
})
|
2023-06-22 15:20:04 +00:00
|
|
|
public hashvalues: HasMany<typeof HashValue>;
|
2023-11-22 16:06:55 +00:00
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'filePath',
|
|
|
|
})
|
|
|
|
public get filePath() {
|
|
|
|
return `/storage/app/public/${this.pathName}`;
|
|
|
|
// const mainTitle = this.titles?.find((title) => title.type === 'Main');
|
|
|
|
// return mainTitle ? mainTitle.value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'size',
|
|
|
|
})
|
|
|
|
public get size() {
|
|
|
|
return this.fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'type',
|
|
|
|
})
|
|
|
|
public get type() {
|
|
|
|
return this.mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'name',
|
|
|
|
})
|
|
|
|
get name(): string {
|
|
|
|
return this.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'lastModified',
|
|
|
|
})
|
|
|
|
get lastModified(): number {
|
|
|
|
return this.updatedAt.toUnixInteger(); //.toFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly webkitRelativePath: string = '';
|
|
|
|
|
|
|
|
@computed({
|
|
|
|
serializeAs: 'fileData',
|
|
|
|
})
|
|
|
|
public get fileData(): string {
|
2023-11-27 16:17:22 +00:00
|
|
|
try {
|
|
|
|
const fileContent: Buffer = fs.readFileSync(this.filePath);
|
|
|
|
// Create a Blob from the file content
|
|
|
|
// const blob = new Blob([fileContent], { type: this.type }); // Adjust
|
|
|
|
// let fileSrc = URL.createObjectURL(blob);
|
|
|
|
// return fileSrc;
|
|
|
|
|
|
|
|
// create a JSON string that contains the data in the property "blob"
|
|
|
|
const json = JSON.stringify({ blob: fileContent.toString('base64') });
|
|
|
|
return json;
|
|
|
|
} catch (err) {
|
|
|
|
// console.error(`Error reading file: ${err}`);
|
|
|
|
return '';
|
|
|
|
}
|
2023-11-22 16:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async createHashValues(trx?: TransactionClientContract) {
|
|
|
|
const hashtypes: string[] = ['md5', 'sha512'];
|
|
|
|
|
|
|
|
for (const type of hashtypes) {
|
|
|
|
const hash = new HashValue();
|
|
|
|
hash.type = type;
|
2023-11-27 16:17:22 +00:00
|
|
|
const hashString = await this._checksumFile(this.filePath, type); // Assuming getRealHash is a method in the same model
|
2023-11-22 16:06:55 +00:00
|
|
|
hash.value = hashString;
|
|
|
|
|
|
|
|
// https://github.com/adonisjs/core/discussions/1872#discussioncomment-132289
|
|
|
|
const file: File = this;
|
|
|
|
if (trx) {
|
|
|
|
await file.useTransaction(trx).related('hashvalues').save(hash); // Save the hash value to the database
|
|
|
|
} else {
|
|
|
|
await file.related('hashvalues').save(hash); // Save the hash value to the database
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:17:22 +00:00
|
|
|
public async delete() {
|
|
|
|
if (this.pathName) {
|
|
|
|
// Delete file from additional storage
|
2024-04-23 17:36:45 +00:00
|
|
|
await drive.delete(this.pathName);
|
2023-11-27 16:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the original delete method of the BaseModel to remove the record from the database
|
|
|
|
await super.delete();
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
private async _checksumFile(path: string, hashName = 'md5'): Promise<string> {
|
2023-11-22 16:06:55 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const hash = crypto.createHash(hashName);
|
|
|
|
const stream = fs.createReadStream(path);
|
|
|
|
stream.on('error', (err) => reject(err));
|
|
|
|
stream.on('data', (chunk) => hash.update(chunk));
|
|
|
|
stream.on('end', () => resolve(hash.digest('hex')));
|
|
|
|
});
|
|
|
|
}
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|