Add MediaUpload entity & Media module

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-13 10:33:54 +02:00
parent 0a0732049a
commit f01c7dbbe2
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 75 additions and 0 deletions

View file

@ -10,6 +10,7 @@ import { NotesModule } from './notes/notes.module';
import { PermissionsModule } from './permissions/permissions.module'; import { PermissionsModule } from './permissions/permissions.module';
import { RevisionsModule } from './revisions/revisions.module'; import { RevisionsModule } from './revisions/revisions.module';
import { UsersModule } from './users/users.module'; import { UsersModule } from './users/users.module';
import { MediaModule } from './media/media.module';
@Module({ @Module({
imports: [ imports: [
@ -29,6 +30,7 @@ import { UsersModule } from './users/users.module';
PermissionsModule, PermissionsModule,
GroupsModule, GroupsModule,
LoggerModule, LoggerModule,
MediaModule,
], ],
controllers: [], controllers: [],
providers: [], providers: [],

View file

@ -0,0 +1,65 @@
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';
@Entity()
export class MediaUpload {
@PrimaryColumn()
id: string;
@ManyToOne(_ => Note, { nullable: false })
note: Note;
@ManyToOne(_ => User, { nullable: false })
user: User;
@Column({
nullable: false,
})
extension: string;
@Column({
nullable: false,
})
backendType: string;
@Column({
nullable: true,
})
backendData: string | null;
@CreateDateColumn()
createdAt: Date;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static create(
note: Note,
user: User,
extension: string,
backendType: string,
backendData?: string,
): MediaUpload {
const upload = new MediaUpload();
const randomBytes = crypto.randomBytes(16);
upload.id = randomBytes.toString('hex');
upload.note = note;
upload.user = user;
upload.extension = extension;
upload.backendType = backendType;
if (backendData) {
upload.backendData = backendData;
} else {
upload.backendData = null;
}
return upload;
}
}

View file

@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MediaUpload } from './media-upload.entity';
@Module({
imports: [TypeOrmModule.forFeature([MediaUpload])],
})
export class MediaModule {}