2020-11-22 13:49:08 +01:00
|
|
|
import Model from "swaf/db/Model";
|
|
|
|
import Controller from "swaf/Controller";
|
2020-06-14 13:01:52 +02:00
|
|
|
import config from "config";
|
2020-11-22 13:49:08 +01:00
|
|
|
import User from "swaf/auth/models/User";
|
2020-06-14 21:23:57 +02:00
|
|
|
import {Request} from "express";
|
2020-07-06 11:05:04 +02:00
|
|
|
import URLRedirect from "./URLRedirect";
|
2020-06-14 13:01:52 +02:00
|
|
|
|
|
|
|
export default class FileModel extends Model {
|
|
|
|
public static get table(): string {
|
|
|
|
return 'files';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async getBySlug(slug: string): Promise<FileModel | null> {
|
2020-06-27 16:31:36 +02:00
|
|
|
return await this.select().where('slug', slug).first();
|
2020-06-14 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 21:23:57 +02:00
|
|
|
public static async paginateForUser(req: Request, perPage: number, user_id: number): Promise<FileModel[]> {
|
2021-03-30 12:31:47 +02:00
|
|
|
req.params.sortBy = 'created_at';
|
|
|
|
req.params.sortDirection = 'DESC';
|
2020-07-05 19:43:56 +02:00
|
|
|
return await this.paginate(req, perPage, this.select().where('user_id', user_id));
|
2020-06-14 21:23:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:54:15 +02:00
|
|
|
public id?: number = undefined;
|
|
|
|
public readonly user_id?: number = undefined;
|
|
|
|
public readonly slug?: string = undefined;
|
|
|
|
public readonly real_name?: string = undefined;
|
|
|
|
public readonly storage_type?: FileStorage = undefined;
|
|
|
|
public readonly storage_path?: string = undefined;
|
|
|
|
public readonly size?: number = undefined;
|
|
|
|
public created_at?: Date = undefined;
|
|
|
|
public readonly ttl?: number = undefined;
|
2020-06-14 13:01:52 +02:00
|
|
|
|
2020-11-22 14:13:57 +01:00
|
|
|
protected init(): void {
|
2020-08-11 17:54:15 +02:00
|
|
|
this.setValidation('user_id').defined().exists(User, 'id');
|
|
|
|
this.setValidation('slug').defined().minLength(1).maxLength(259).unique(FileModel, 'slug').unique(URLRedirect, 'slug');
|
|
|
|
this.setValidation('real_name').defined().minLength(1).maxLength(259);
|
|
|
|
this.setValidation('storage_type').defined().maxLength(64);
|
|
|
|
this.setValidation('storage_path').defined().maxLength(1745);
|
|
|
|
this.setValidation('size').defined().min(0);
|
|
|
|
this.setValidation('ttl').defined().min(0).max(4294967295);
|
2020-06-14 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 14:27:55 +01:00
|
|
|
public getURL(domain: string = config.get<string>('public_url')): string {
|
2020-07-06 14:52:38 +02:00
|
|
|
return (/^https?:\/\//.test(domain) ? '' : 'https://') + domain + Controller.route('get-file', {
|
2020-11-22 14:13:57 +01:00
|
|
|
slug: this.getOrFail('slug'),
|
2020-06-14 13:01:52 +02:00
|
|
|
});
|
|
|
|
}
|
2020-06-14 14:53:05 +02:00
|
|
|
|
2020-06-14 17:37:47 +02:00
|
|
|
public getExpirationDate(): Date | null {
|
2020-06-14 14:53:05 +02:00
|
|
|
if (!this.created_at) return new Date();
|
2020-06-14 17:37:47 +02:00
|
|
|
if (this.ttl === 0) return null;
|
2020-06-14 14:53:05 +02:00
|
|
|
|
2020-11-22 14:13:57 +01:00
|
|
|
return new Date(this.created_at.getTime() + this.getOrFail('ttl') * 1000);
|
2020-06-14 14:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public shouldBeDeleted(): boolean {
|
2020-06-14 17:37:47 +02:00
|
|
|
const expirationDate = this.getExpirationDate();
|
|
|
|
if (!expirationDate) return false;
|
|
|
|
return new Date().getTime() >= expirationDate.getTime();
|
2020-06-14 14:53:05 +02:00
|
|
|
}
|
2020-06-14 18:10:01 +02:00
|
|
|
|
|
|
|
public canDelete(user_id: number): boolean {
|
|
|
|
return this.user_id === user_id;
|
|
|
|
}
|
2020-06-14 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export type FileStorage = 'local';
|