aboutsummaryrefslogtreecommitdiff
path: root/src/db/model/File.ts
diff options
context:
space:
mode:
authorjosh97josh2018-04-22 17:36:39 +0100
committerjosh97josh2018-04-22 17:36:39 +0100
commitaec20c7e3c9a01dbb073590201b473da5669e8e1 (patch)
tree53df187fd473c1d0ca19568f03995bb462d9f633 /src/db/model/File.ts
parentMerge branch 'jl247/permissions' into trh/gets (diff)
parentautocreating logs folder on startup (diff)
downloadmedicine-aec20c7e3c9a01dbb073590201b473da5669e8e1.tar.gz
medicine-aec20c7e3c9a01dbb073590201b473da5669e8e1.zip

Merge branch ‘trh/gets’ of ssh://localhost:2222/cs3099group-be-4/project-code into trh/gets

Diffstat (limited to 'src/db/model/File.ts')
-rw-r--r--src/db/model/File.ts30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/db/model/File.ts b/src/db/model/File.ts
index 8a12431..89415b7 100644
--- a/src/db/model/File.ts
+++ b/src/db/model/File.ts
@@ -3,7 +3,9 @@ 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';
+import { RequestError } from '../../errors';
+import { FileTypeName, INITIAL_METADATA, Metadata, mimes } from '../../files';
+import { match, metadata } from '../../matcher';
/*
* attributes:
@@ -11,7 +13,6 @@ import { FileTypeName, mimes } from '../../files';
* - fullPathInternal
* - uuid
* - type
- * - size
* - parentFolderId
* - containedFilesInternal
* - creatorName
@@ -31,7 +32,7 @@ export default class File extends Model<File> {
@AllowNull
@Column
- private fullPathInternal!: string;
+ public fullPathInternal!: string;
@PrimaryKey
@Column
@@ -40,9 +41,6 @@ export default class File extends Model<File> {
@Column(DataType.TEXT)
public type!: FileTypeName;
- @Column
- public size!: number;
-
@ForeignKey(() => File)
@AllowNull
@Column(DataType.TEXT)
@@ -117,12 +115,24 @@ export default class File extends Model<File> {
this.containedFilesInternal = files;
}
- public get metadata(): object {
- return JSON.parse(this.metadataInternal);
+ public get metadata(): Metadata {
+ return !!(this.metadataInternal) ? JSON.parse(this.metadataInternal)
+ : INITIAL_METADATA;
}
- public set metadata(metadata: object) {
- this.metadataInternal = JSON.stringify(metadata);
+ public set metadata(md: Metadata) {
+ const oldMD: Metadata = this.metadata;
+ const newMD: Metadata = md;
+ if (!match(metadata, newMD)) {
+ throw new RequestError(400, 'invalid_request');
+ }
+ if (newMD.version !== oldMD.version + 1) {
+ throw new RequestError(400, 'invalid_request');
+ }
+ this.metadataInternal = JSON.stringify(newMD);
+ }
+ public setMetadataInternal(md: Metadata): void {
+ this.metadataInternal = JSON.stringify(md);
}
public set mimetype(mime: string) {