36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
|
import Model from "wms-core/db/Model";
|
||
|
import Validator from "wms-core/db/Validator";
|
||
|
import Controller from "wms-core/Controller";
|
||
|
import config from "config";
|
||
|
|
||
|
export default class FileModel extends Model {
|
||
|
public static get table(): string {
|
||
|
return 'files';
|
||
|
}
|
||
|
|
||
|
public static async getBySlug(slug: string): Promise<FileModel | null> {
|
||
|
const models = await this.models<FileModel>(this.select().where('slug', slug).first());
|
||
|
return models.length > 0 ? models[0] : null;
|
||
|
}
|
||
|
|
||
|
public readonly slug!: string;
|
||
|
public readonly real_name!: string;
|
||
|
public readonly storage_type!: FileStorage;
|
||
|
public readonly storage_path!: string;
|
||
|
|
||
|
protected defineProperties() {
|
||
|
this.defineProperty('slug', new Validator().defined().minLength(1).maxLength(259).unique(this, 'slug'));
|
||
|
this.defineProperty('real_name', new Validator().defined().minLength(1).maxLength(259));
|
||
|
this.defineProperty('storage_type', new Validator().defined().maxLength(64));
|
||
|
this.defineProperty('storage_path', new Validator().defined().maxLength(1745));
|
||
|
}
|
||
|
|
||
|
public getURL(): string {
|
||
|
return config.get<string>('base_url') + Controller.route('get-file', {
|
||
|
slug: this.slug,
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export type FileStorage = 'local';
|