hedgedoc/src/media/media-upload.entity.ts
Philip Molares dc63c76f43
added reuse information
Signed-off-by: Philip Molares <philip.molares@udo.edu>
2021-01-05 22:12:38 +01:00

68 lines
1.4 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import * as crypto from 'crypto';
import {
Column,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryColumn,
} from 'typeorm';
import { Note } from '../notes/note.entity';
import { User } from '../users/user.entity';
import { BackendType } from './backends/backend-type.enum';
export type BackendData = string | null;
@Entity()
export class MediaUpload {
@PrimaryColumn()
id: string;
@ManyToOne(_ => Note, { nullable: false })
note: Note;
@ManyToOne(_ => User, { nullable: false })
user: User;
@Column({
nullable: false,
})
backendType: string;
@Column({
nullable: true,
})
backendData: BackendData;
@CreateDateColumn()
createdAt: Date;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static create(
note: Note,
user: User,
extension: string,
backendType: BackendType,
backendData?: string,
): MediaUpload {
const upload = new MediaUpload();
const randomBytes = crypto.randomBytes(16);
upload.id = randomBytes.toString('hex') + '.' + extension;
upload.note = note;
upload.user = user;
upload.backendType = backendType;
if (backendData) {
upload.backendData = backendData;
} else {
upload.backendData = null;
}
return upload;
}
}