Partypack/Server/Source/Schemas/Song.ts

101 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-01-28 16:54:32 +01:00
import { BaseEntity, BeforeInsert, BeforeRemove, Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { FULL_SERVER_ROOT } from "../Modules/Constants";
2024-01-24 01:35:47 +01:00
import { Rating } from "./Rating";
2024-01-28 16:54:32 +01:00
import { existsSync, mkdirSync, rmSync } from "fs";
2024-01-24 01:35:47 +01:00
import { v4 } from "uuid";
2024-01-28 16:54:32 +01:00
import { User } from "./User";
import { join } from "path";
2024-01-22 00:41:59 +01:00
@Entity()
export class Song extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
ID: string;
2024-01-28 16:54:32 +01:00
@ManyToOne(() => User, U => U.CreatedTracks)
Author: User;
2024-01-22 00:41:59 +01:00
@Column()
Name: string;
@Column()
Year: number;
@Column()
ArtistName: string;
@Column()
Length: number;
@Column()
Scale: "Minor" | "Major";
@Column()
Key: string;
@Column()
Album: string;
@Column({ default: "Guitar" })
GuitarStarterType: "Keytar" | "Guitar";
@Column()
Tempo: number;
@Column()
Directory: string;
@Column({ nullable: true })
Midi?: string;
@Column({ nullable: true })
Cover?: string;
@Column()
BassDifficulty: number;
@Column()
GuitarDifficulty: number;
@Column()
DrumsDifficulty: number;
@Column()
VocalsDifficulty: number;
2024-01-28 16:54:32 +01:00
@Column()
IsDraft: boolean;
@Column()
CreationDate: Date;
2024-01-22 00:41:59 +01:00
@Column({ nullable: true })
Lipsync?: string;
2024-01-24 01:35:47 +01:00
@OneToMany(() => Rating, R => R.Rated)
Ratings: Rating[];
@BeforeInsert()
Setup() {
2024-01-24 01:35:47 +01:00
this.ID = v4();
this.Directory = `./Saved/Songs/${this.ID}`;
2024-01-28 16:54:32 +01:00
if (!existsSync(join(this.Directory, "Chunks")))
mkdirSync(join(this.Directory, "Chunks"), { recursive: true });
2024-01-24 01:35:47 +01:00
this.CreationDate = new Date();
}
2024-01-28 16:54:32 +01:00
@BeforeRemove()
Delete() {
if (existsSync(this.Directory) && this.Directory.endsWith(this.ID))
rmSync(this.Directory, { recursive: true, force: true }); // lets hope this does not cause steam launcher for linux 2.0
}
public Package() {
return {
...this,
Directory: undefined, // we should NOT reveal that
Midi: this.Midi ?? `${FULL_SERVER_ROOT}/song/download/${this.ID}/midi.mid`,
Cover: this.Cover ?? `${FULL_SERVER_ROOT}/song/download/${this.ID}/cover.png`
}
}
2024-01-22 00:41:59 +01:00
}