hedgedoc/src/media/media-upload.entity.ts
Philip Molares 165bb7602b MediaUploadEntity: Add fileUrl
Save the fileUrl, returned to the user on creation, in the DB.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
2021-02-27 21:29:14 +01:00

71 lines
1.4 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2021 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()
fileUrl: 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;
}
}