(hopefully) working photos upload backend

This commit is contained in:
2020-10-13 23:58:49 +03:00
committed by Stepan Usatiuk
parent 755e849ded
commit 2887aedc3c
19 changed files with 1255 additions and 104 deletions

View File

@@ -1,10 +1,29 @@
import * as path from "path";
import * as fs from "fs/promises";
import * as mime from "mime-types";
import { constants as fsConstants } from "fs";
import {
AfterRemove,
BaseEntity,
BeforeRemove,
Column,
Entity,
Index,
ManyToOne,
PrimaryGeneratedColumn,
} from "typeorm";
import { User } from "./User";
export interface IPhotoJSON {
id: number;
user: number;
hash: string;
size: string;
format: string;
createdAt: number;
editedAt: number;
}
@Entity()
export class Photo extends BaseEntity {
@@ -14,4 +33,73 @@ export class Photo extends BaseEntity {
@Column({ length: 190 })
@Index()
public hash: string;
@Column({ length: 190 })
@Index()
public size: string;
@Column({ length: 190 })
@Index()
public format: string;
@Column({ type: "timestamp", default: null })
public createdAt: Date;
@Column({ type: "timestamp", default: null })
public editedAt: Date;
@ManyToOne(() => User, (user) => user.photos, { eager: true })
public user: User;
public getFileName(): string {
return `${this.user.id.toString()}-${this.hash}-${this.size}.${
mime.extension(this.format) as string
}`;
}
public getPath(): string {
return path.join(this.user.getDataPath(), this.getFileName());
}
@BeforeRemove()
async cleanupFiles(): Promise<void> {
try {
await fs.unlink(this.getPath());
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
}
public async isUploaded(): Promise<boolean> {
try {
await fs.access(this.getPath(), fsConstants.F_OK);
return true;
} catch (e) {
return false;
}
}
constructor(user: User, hash: string, size: string, format: string) {
super();
this.createdAt = new Date();
this.editedAt = this.createdAt;
this.hash = hash;
this.format = format;
this.size = size;
this.user = user;
}
public toJSON(): IPhotoJSON {
return {
id: this.id,
user: this.user.id,
hash: this.hash,
size: this.size,
format: this.format,
createdAt: this.createdAt.getTime(),
editedAt: this.editedAt.getTime(),
};
}
}