mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-12 14:55:39 +01:00
refactor models
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
|||||||
Fail, HasSuccess
|
Fail, HasSuccess
|
||||||
} from 'picsur-shared/dist/types';
|
} from 'picsur-shared/dist/types';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { SupportedMime } from '../../models/dto/mimes.dto';
|
|
||||||
import { EImageBackend } from '../../models/entities/image.entity';
|
import { EImageBackend } from '../../models/entities/image.entity';
|
||||||
import { GetCols } from '../../models/util/collection';
|
import { GetCols } from '../../models/util/collection';
|
||||||
|
|
||||||
@@ -20,7 +19,7 @@ export class ImageDBService {
|
|||||||
|
|
||||||
public async create(
|
public async create(
|
||||||
image: Buffer,
|
image: Buffer,
|
||||||
type: SupportedMime,
|
type: string,
|
||||||
): AsyncFailable<EImageBackend> {
|
): AsyncFailable<EImageBackend> {
|
||||||
const hash = this.hash(image);
|
const hash = this.hash(image);
|
||||||
const find = await this.findOne(hash);
|
const find = await this.findOne(hash);
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Fail, Failable } from 'picsur-shared/dist/types';
|
import { Fail, Failable } from 'picsur-shared/dist/types';
|
||||||
import { FullMime, SupportedAnimMimes, SupportedImageMimes, SupportedMime } from '../../models/dto/mimes.dto';
|
import {
|
||||||
|
FullMime,
|
||||||
|
SupportedAnimMimes,
|
||||||
|
SupportedImageMimes
|
||||||
|
} from '../../models/dto/mimes.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MimesService {
|
export class MimesService {
|
||||||
public getFullMime(mime: string): Failable<FullMime> {
|
public getFullMime(mime: string): Failable<FullMime> {
|
||||||
if (SupportedImageMimes.includes(mime)) {
|
if (SupportedImageMimes.includes(mime)) {
|
||||||
return { mime: mime as SupportedMime, type: 'image' };
|
return { mime, type: 'image' };
|
||||||
}
|
}
|
||||||
if (SupportedAnimMimes.includes(mime)) {
|
if (SupportedAnimMimes.includes(mime)) {
|
||||||
return { mime: mime as SupportedMime, type: 'anim' };
|
return { mime, type: 'anim' };
|
||||||
}
|
}
|
||||||
return Fail('Unsupported mime type');
|
return Fail('Unsupported mime type');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { strictValidate } from 'picsur-shared/dist/util/validate';
|
|||||||
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
|
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
|
||||||
import { Permissions } from '../../../models/dto/permissions.dto';
|
import { Permissions } from '../../../models/dto/permissions.dto';
|
||||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
import { EUserBackend } from '../../../models/entities/user.entity';
|
||||||
import { isPermissionsArray } from '../../../models/util/permissions';
|
import { isPermissionsArray } from '../../../models/util/permissions.validator';
|
||||||
|
|
||||||
// This guard extends both the jwt authenticator and the guest authenticator
|
// This guard extends both the jwt authenticator and the guest authenticator
|
||||||
// The order matters here, because this results in the guest authenticator being used as a fallback
|
// The order matters here, because this results in the guest authenticator being used as a fallback
|
||||||
|
|||||||
@@ -1,33 +1,30 @@
|
|||||||
import tuple from 'picsur-shared/dist/types/tuple';
|
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
|
export enum ImageMime {
|
||||||
|
JPEG = 'image/jpeg',
|
||||||
|
PNG = 'image/png',
|
||||||
|
WEBP = 'image/webp',
|
||||||
|
TIFF = 'image/tiff',
|
||||||
|
BMP = 'image/bmp',
|
||||||
|
ICO = 'image/x-icon',
|
||||||
|
}
|
||||||
|
|
||||||
const SupportedImageMimesTuple = tuple(
|
export enum AnimMime {
|
||||||
'image/jpeg',
|
APNG = 'image/apng',
|
||||||
'image/png',
|
GIF = 'image/gif',
|
||||||
'image/webp',
|
}
|
||||||
'image/tiff',
|
|
||||||
'image/bmp',
|
|
||||||
'image/x-icon',
|
|
||||||
);
|
|
||||||
|
|
||||||
const SupportedAnimMimesTuple = tuple('image/apng', 'image/gif');
|
export const SupportedMime = {...ImageMime, ...AnimMime};
|
||||||
|
|
||||||
const SupportedMimesTuple = [
|
|
||||||
...SupportedImageMimesTuple,
|
|
||||||
...SupportedAnimMimesTuple,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Derivatives
|
// Derivatives
|
||||||
|
|
||||||
export const SupportedImageMimes: string[] = SupportedImageMimesTuple;
|
export const SupportedImageMimes: string[] = Object.values(ImageMime);
|
||||||
export const SupportedAnimMimes: string[] = SupportedAnimMimesTuple;
|
export const SupportedAnimMimes: string[] = Object.values(AnimMime);
|
||||||
|
export const SupportedMimes: string[] = Object.values(SupportedMime);
|
||||||
|
|
||||||
export const SupportedMimes: string[] = SupportedMimesTuple;
|
|
||||||
export type SupportedMime = typeof SupportedMimesTuple[number];
|
|
||||||
export type SupportedMimeCategory = 'image' | 'anim';
|
export type SupportedMimeCategory = 'image' | 'anim';
|
||||||
|
|
||||||
export interface FullMime {
|
export interface FullMime {
|
||||||
mime: SupportedMime;
|
mime: string;
|
||||||
type: SupportedMimeCategory;
|
type: SupportedMimeCategory;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import {
|
|||||||
export type SysPreferences = SysPreference[];
|
export type SysPreferences = SysPreference[];
|
||||||
export const SysPreferenceList: string[] = Object.values(SysPreference);
|
export const SysPreferenceList: string[] = Object.values(SysPreference);
|
||||||
|
|
||||||
// Syspref Values
|
// Syspref Value types
|
||||||
|
|
||||||
export const SysPreferenceValueTypes: {
|
export const SysPreferenceValueTypes: {
|
||||||
[key in SysPreference]: SysPrefValueTypeStrings;
|
[key in SysPreference]: SysPrefValueTypeStrings;
|
||||||
} = {
|
} = {
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { IsEnum } from 'class-validator';
|
|
||||||
import { EImage } from 'picsur-shared/dist/entities/image.entity';
|
import { EImage } from 'picsur-shared/dist/entities/image.entity';
|
||||||
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
||||||
import { SupportedMime, SupportedMimes } from '../dto/mimes.dto';
|
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class EImageBackend extends EImage {
|
export class EImageBackend extends EImage {
|
||||||
@@ -16,7 +14,6 @@ export class EImageBackend extends EImage {
|
|||||||
@Column({ type: 'bytea', nullable: false, select: false })
|
@Column({ type: 'bytea', nullable: false, select: false })
|
||||||
override data?: Buffer;
|
override data?: Buffer;
|
||||||
|
|
||||||
@Column({ enum: SupportedMimes, nullable: false })
|
@Column({ nullable: false })
|
||||||
@IsEnum(SupportedMimes)
|
override mime: string;
|
||||||
override mime: SupportedMime;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import { ESysPreference } from 'picsur-shared/dist/entities/syspreference.entity';
|
import { ESysPreference } from 'picsur-shared/dist/entities/syspreference.entity';
|
||||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class ESysPreferenceBackend extends ESysPreference {
|
export class ESysPreferenceBackend extends ESysPreference {
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
override id?: number;
|
override id?: number;
|
||||||
|
|
||||||
|
@Index()
|
||||||
@Column({ nullable: false, unique: true })
|
@Column({ nullable: false, unique: true })
|
||||||
override key: string;
|
override key: string;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
import { EUserBackend } from '../entities/user.entity';
|
import { EUserBackend } from '../entities/user.entity';
|
||||||
|
|
||||||
|
// Add typing to FastifyRequest to make using the user object easier
|
||||||
export default interface AuthFasityRequest extends FastifyRequest {
|
export default interface AuthFasityRequest extends FastifyRequest {
|
||||||
user: EUserBackend;
|
user: EUserBackend;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import { Type } from 'class-transformer';
|
|
||||||
import { IsDefined, ValidateNested } from 'class-validator';
|
|
||||||
import { MultiPartFileDto } from './multipart.dto';
|
import { MultiPartFileDto } from './multipart.dto';
|
||||||
|
import { IsMultiPartFile } from './multipart.validator';
|
||||||
|
|
||||||
|
// A validation class for form based file upload of an image
|
||||||
export class ImageUploadDto {
|
export class ImageUploadDto {
|
||||||
@IsDefined()
|
@IsMultiPartFile()
|
||||||
@ValidateNested()
|
|
||||||
@Type(() => MultiPartFileDto)
|
|
||||||
image: MultiPartFileDto;
|
image: MultiPartFileDto;
|
||||||
}
|
}
|
||||||
|
|||||||
16
backend/src/models/requests/multipart.validator.ts
Normal file
16
backend/src/models/requests/multipart.validator.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { Type } from 'class-transformer';
|
||||||
|
import { IsDefined, ValidateNested } from 'class-validator';
|
||||||
|
import { CombinePDecorators } from 'picsur-shared/dist/util/decorator';
|
||||||
|
import { MultiPartFieldDto, MultiPartFileDto } from './multipart.dto';
|
||||||
|
|
||||||
|
export const IsMultiPartFile = CombinePDecorators(
|
||||||
|
IsDefined(),
|
||||||
|
ValidateNested(),
|
||||||
|
Type(() => MultiPartFileDto),
|
||||||
|
);
|
||||||
|
|
||||||
|
export const IsMultiPartField = CombinePDecorators(
|
||||||
|
IsDefined(),
|
||||||
|
ValidateNested(),
|
||||||
|
Type(() => MultiPartFieldDto),
|
||||||
|
);
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
|
// This is a function that returns an array of all available columns in a database table
|
||||||
|
// It is used to fetch hidden columns from the database
|
||||||
export function GetCols<T>(repository: Repository<T>): (keyof T)[] {
|
export function GetCols<T>(repository: Repository<T>): (keyof T)[] {
|
||||||
return repository.metadata.columns.map(
|
return repository.metadata.columns.map(
|
||||||
(col) => col.propertyName,
|
(col) => col.propertyName,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import {
|
|||||||
SoulBoundRolesList,
|
SoulBoundRolesList,
|
||||||
UndeletableRolesList
|
UndeletableRolesList
|
||||||
} from '../../../models/dto/roles.dto';
|
} from '../../../models/dto/roles.dto';
|
||||||
import { isPermissionsArray } from '../../../models/util/permissions';
|
import { isPermissionsArray } from '../../../models/util/permissions.validator';
|
||||||
|
|
||||||
@Controller('api/roles')
|
@Controller('api/roles')
|
||||||
@RequiredPermissions(Permission.RoleManage)
|
@RequiredPermissions(Permission.RoleManage)
|
||||||
|
|||||||
Reference in New Issue
Block a user