Initial commit
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
/**
|
||||
* Class model for FileEntity Interface
|
||||
*/
|
||||
import type { FileEntity } from "@/types/node";
|
||||
|
||||
export class FileEntityObject implements FileEntity {
|
||||
|
||||
_data!: FileEntity;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'files.entity',
|
||||
in: null,
|
||||
id: '',
|
||||
createdBy: '',
|
||||
createdOn: '',
|
||||
modifiedBy: '',
|
||||
modifiedOn: '',
|
||||
owner: '',
|
||||
signature: '',
|
||||
label: '',
|
||||
size: 0,
|
||||
mime: '',
|
||||
format: '',
|
||||
encoding: '',
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: FileEntity): FileEntityObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): FileEntity {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): FileEntityObject {
|
||||
const cloned = new FileEntityObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get '@type'(): 'files.entity' {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get in(): string | null {
|
||||
return this._data.in;
|
||||
}
|
||||
|
||||
set in(value: string | null) {
|
||||
this._data.in = value;
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
set id(value: string) {
|
||||
this._data.id = value;
|
||||
}
|
||||
|
||||
get createdBy(): string {
|
||||
return this._data.createdBy;
|
||||
}
|
||||
|
||||
set createdBy(value: string) {
|
||||
this._data.createdBy = value;
|
||||
}
|
||||
|
||||
get createdOn(): string {
|
||||
return this._data.createdOn;
|
||||
}
|
||||
|
||||
set createdOn(value: string) {
|
||||
this._data.createdOn = value;
|
||||
}
|
||||
|
||||
get modifiedBy(): string {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string) {
|
||||
this._data.modifiedBy = value;
|
||||
}
|
||||
|
||||
get modifiedOn(): string {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
set modifiedOn(value: string) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
|
||||
get owner(): string {
|
||||
return this._data.owner;
|
||||
}
|
||||
|
||||
set owner(value: string) {
|
||||
this._data.owner = value;
|
||||
}
|
||||
|
||||
get signature(): string {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
set signature(value: string) {
|
||||
this._data.signature = value;
|
||||
}
|
||||
|
||||
get label(): string {
|
||||
return this._data.label;
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this._data.size;
|
||||
}
|
||||
|
||||
set size(value: number) {
|
||||
this._data.size = value;
|
||||
}
|
||||
|
||||
get mime(): string {
|
||||
return this._data.mime;
|
||||
}
|
||||
|
||||
set mime(value: string) {
|
||||
this._data.mime = value;
|
||||
}
|
||||
|
||||
get format(): string {
|
||||
return this._data.format;
|
||||
}
|
||||
|
||||
set format(value: string) {
|
||||
this._data.format = value;
|
||||
}
|
||||
|
||||
get encoding(): string {
|
||||
return this._data.encoding;
|
||||
}
|
||||
|
||||
set encoding(value: string) {
|
||||
this._data.encoding = value;
|
||||
}
|
||||
|
||||
/** Helper methods */
|
||||
|
||||
get createdOnDate(): Date | null {
|
||||
return this._data.createdOn ? new Date(this._data.createdOn) : null;
|
||||
}
|
||||
|
||||
get modifiedOnDate(): Date | null {
|
||||
return this._data.modifiedOn ? new Date(this._data.modifiedOn) : null;
|
||||
}
|
||||
|
||||
get extension(): string {
|
||||
const parts = this._data.label.split('.');
|
||||
return parts.length > 1 ? parts[parts.length - 1] : '';
|
||||
}
|
||||
|
||||
get sizeFormatted(): string {
|
||||
const bytes = this._data.size;
|
||||
if (bytes === 0) return '0 B';
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
get isImage(): boolean {
|
||||
return this._data.mime.startsWith('image/');
|
||||
}
|
||||
|
||||
get isVideo(): boolean {
|
||||
return this._data.mime.startsWith('video/');
|
||||
}
|
||||
|
||||
get isAudio(): boolean {
|
||||
return this._data.mime.startsWith('audio/');
|
||||
}
|
||||
|
||||
get isText(): boolean {
|
||||
return this._data.mime.startsWith('text/') ||
|
||||
this._data.mime === 'application/json' ||
|
||||
this._data.mime === 'application/xml';
|
||||
}
|
||||
|
||||
get isPdf(): boolean {
|
||||
return this._data.mime === 'application/pdf';
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user