aboutsummaryrefslogtreecommitdiff
path: root/src/db/model/File.ts
blob: 193a7851868db4cc5cfa70ffb4b8ac5082853db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { default as Project } from './Project';
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, HasMany,
		HasOne, Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript';
import { default as User } from './User';

import { FileTypeName, mimes } from '../../files';

/*
 * attributes:
 * 	- nameInternal
 *  - fullPathInternal
 * 	- uuid
 * 	- type
 *  - size
 * 	- parentFolderId
 *  - containedFilesInternal
 * 	- creatorName
 *  - creator
 *  - rootFolderOf
 *  - uploadDate
 *  - modifyDate
 *  - status
 *  - metadataInternal
*/

@Table
export default class File extends Model<File> {
	@AllowNull
	@Column
	private nameInternal!: string;

	@AllowNull
	@Column
	private fullPathInternal!: string;

	@PrimaryKey
	@Column
	public uuid!: string;

	@Column(DataType.TEXT)
	public type!: FileTypeName;

	@Column
	public size!: number;

	@ForeignKey(() => File)
	@AllowNull
	@Column(DataType.TEXT)
	public parentFolderId!: string | null;

	@BelongsTo(() => File)
	public parentFolder?: File;

	@HasMany(() => File)
	public containedFilesInternal?: File[];

	@ForeignKey(() => User)
	@AllowNull
	@Column(DataType.TEXT)
	public creatorName!: string | null;

	@BelongsTo(() => User)
	public creator?: User;

	@HasOne(() => Project)
	public rootFolderOf?: Project;

	@CreatedAt
	@Column(DataType.DATE)
	public uploadDate!: Date;

	@UpdatedAt
	@Column(DataType.DATE)
	public modifyDate!: Date;

	@Column
	public status!: string;

	@Column
	private metadataInternal!: string;

	public get name(): string {
		return this.nameInternal;
	}

	public set name(name: string) {
		this.nameInternal = name;
		this.fullPathInternal = this.fullPathInternal.substring(
			0,
			this.fullPathInternal.lastIndexOf('/') + 1
		) + this.name;
	}

	public get fullPath(): string {
		return this.fullPathInternal;
	}

	public set fullPath(path: string) {
		this.fullPathInternal = path;
		this.nameInternal = this.fullPath.substring(
			this.fullPathInternal.lastIndexOf('/') + 1
		);
	}

	public get containedFiles(): File[] {
		if (this.containedFilesInternal === undefined) {
			throw new Error('containedFiles is undefined');
		}
		return this.containedFilesInternal;
	}

	public set containedFiles(files: File[]) {
		this.containedFilesInternal = files;
	}

	public get metadata(): object {
		return JSON.parse(this.metadataInternal);
	}

	public set metadata(metadata: object) {
		this.metadataInternal = JSON.stringify(metadata);
	}

	public set mimetype(mime: string) {
		this.type = mimes.get(mime) || 'generic';
	}
}