mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-14 07:45:39 +01:00
systempreference api done
This commit is contained in:
@@ -8,6 +8,7 @@ import { DemoManagerModule } from './managers/demo/demomanager.module';
|
||||
import { EImageBackend } from './models/entities/image.entity';
|
||||
import { EUserBackend } from './models/entities/user.entity';
|
||||
import { PrefModule } from './routes/api/pref/pref.module';
|
||||
import { ESysPreferenceBackend } from './models/entities/syspreference.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -20,7 +21,7 @@ import { PrefModule } from './routes/api/pref/pref.module';
|
||||
database: Config.database.database,
|
||||
synchronize: true,
|
||||
|
||||
entities: [EUserBackend, EImageBackend],
|
||||
entities: [EUserBackend, EImageBackend, ESysPreferenceBackend],
|
||||
}),
|
||||
ServeStaticModule.forRoot({
|
||||
rootPath: Config.static.frontendRoot,
|
||||
@@ -31,5 +32,4 @@ import { PrefModule } from './routes/api/pref/pref.module';
|
||||
PrefModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
||||
export class AppModule {}
|
||||
|
||||
@@ -8,4 +8,4 @@ import { SysPreferenceService } from './syspreferencedb.service';
|
||||
providers: [SysPreferenceService],
|
||||
exports: [SysPreferenceService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
export class SysPreferenceModule {}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
import { validate } from 'class-validator';
|
||||
import { SysPreferences } from 'picsur-shared/dist/dto/syspreferences.dto';
|
||||
import { AsyncFailable, Fail } from 'picsur-shared/dist/types';
|
||||
import { AsyncFailable, Fail, HasFailed } from 'picsur-shared/dist/types';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SysPreferenceDefaults } from '../../models/dto/syspreference.dto';
|
||||
import { ESysPreferenceBackend } from '../../models/entities/syspreference.entity';
|
||||
@@ -17,22 +18,15 @@ export class SysPreferenceService {
|
||||
) {}
|
||||
|
||||
public async setPreference(
|
||||
key: SysPreferences,
|
||||
key: string,
|
||||
value: string,
|
||||
): AsyncFailable<ESysPreferenceBackend> {
|
||||
let sysPreference = new ESysPreferenceBackend();
|
||||
sysPreference.key = key;
|
||||
sysPreference.value = value;
|
||||
|
||||
const errors = await validate(sysPreference);
|
||||
if (errors.length > 0) {
|
||||
this.logger.warn(errors);
|
||||
return Fail('Invalid preference');
|
||||
}
|
||||
let sysPreference = await this.validatePref(key, value);
|
||||
if (HasFailed(sysPreference)) return sysPreference;
|
||||
|
||||
try {
|
||||
sysPreference = await this.sysPreferenceRepository.save(sysPreference, {
|
||||
reload: true,
|
||||
await this.sysPreferenceRepository.upsert(sysPreference, {
|
||||
conflictPaths: ['key'],
|
||||
});
|
||||
} catch (e: any) {
|
||||
this.logger.warn(e);
|
||||
@@ -43,23 +37,33 @@ export class SysPreferenceService {
|
||||
}
|
||||
|
||||
public async getPreference(
|
||||
key: SysPreferences,
|
||||
key: string,
|
||||
): AsyncFailable<ESysPreferenceBackend> {
|
||||
let sysPreference: ESysPreferenceBackend | undefined;
|
||||
let sysPreference = await this.validatePref(key);
|
||||
if (HasFailed(sysPreference)) return sysPreference;
|
||||
|
||||
let foundSysPreference: ESysPreferenceBackend | undefined;
|
||||
try {
|
||||
sysPreference = await this.sysPreferenceRepository.findOne({
|
||||
key,
|
||||
foundSysPreference = await this.sysPreferenceRepository.findOne({
|
||||
key: sysPreference.key,
|
||||
});
|
||||
} catch (e: any) {
|
||||
this.logger.warn(e);
|
||||
return Fail('Could not get preference');
|
||||
}
|
||||
|
||||
if (!sysPreference) {
|
||||
return this.saveDefault(key);
|
||||
if (!foundSysPreference) {
|
||||
return this.saveDefault(sysPreference.key);
|
||||
} else {
|
||||
foundSysPreference = plainToClass(ESysPreferenceBackend, foundSysPreference);
|
||||
const errors = await validate(foundSysPreference);
|
||||
if (errors.length > 0) {
|
||||
this.logger.warn(errors);
|
||||
return Fail('Invalid preference');
|
||||
}
|
||||
}
|
||||
|
||||
return sysPreference;
|
||||
return foundSysPreference;
|
||||
}
|
||||
|
||||
private async saveDefault(
|
||||
@@ -67,4 +71,21 @@ export class SysPreferenceService {
|
||||
): AsyncFailable<ESysPreferenceBackend> {
|
||||
return this.setPreference(key, SysPreferenceDefaults[key]());
|
||||
}
|
||||
|
||||
private async validatePref(
|
||||
key: string,
|
||||
value: string = 'validate',
|
||||
): AsyncFailable<ESysPreferenceBackend> {
|
||||
let verifySysPreference = new ESysPreferenceBackend();
|
||||
verifySysPreference.key = key as SysPreferences;
|
||||
verifySysPreference.value = value;
|
||||
|
||||
const errors = await validate(verifySysPreference);
|
||||
if (errors.length > 0) {
|
||||
this.logger.warn(errors);
|
||||
return Fail('Invalid preference');
|
||||
}
|
||||
|
||||
return verifySysPreference;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export class ESysPreferenceBackend extends ESysPreference {
|
||||
@PrimaryGeneratedColumn()
|
||||
override id?: number;
|
||||
|
||||
@Column()
|
||||
@Column({ unique: true })
|
||||
override key: SysPreferences;
|
||||
|
||||
@Column()
|
||||
|
||||
@@ -46,8 +46,10 @@ export class AuthController {
|
||||
register.username,
|
||||
register.password,
|
||||
);
|
||||
|
||||
if (HasFailed(user)) throw new ConflictException('User already exists');
|
||||
if (HasFailed(user)) {
|
||||
console.warn(user.getReason());
|
||||
throw new InternalServerErrorException('Could not create user');
|
||||
}
|
||||
|
||||
if (register.isAdmin) {
|
||||
await this.authService.makeAdmin(user);
|
||||
@@ -63,7 +65,10 @@ export class AuthController {
|
||||
@Body() deleteData: AuthDeleteRequest,
|
||||
) {
|
||||
const user = await this.authService.deleteUser(deleteData.username);
|
||||
if (HasFailed(user)) throw new NotFoundException('User does not exist');
|
||||
if (HasFailed(user)) {
|
||||
console.warn(user.getReason());
|
||||
throw new InternalServerErrorException('Could not delete user');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
@@ -72,9 +77,10 @@ export class AuthController {
|
||||
@Get('list')
|
||||
async listUsers(@Request() req: AuthFasityRequest) {
|
||||
const users = this.authService.listUsers();
|
||||
|
||||
if (HasFailed(users))
|
||||
if (HasFailed(users)) {
|
||||
console.warn(users.getReason());
|
||||
throw new InternalServerErrorException('Could not list users');
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,47 @@
|
||||
import { Controller, Post, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
Param,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { UpdateSysPreferenceRequest } from 'picsur-shared/dist/dto/syspreferences.dto';
|
||||
import { HasFailed } from 'picsur-shared/dist/types';
|
||||
import { SysPreferenceService } from '../../../collections/syspreferencesdb/syspreferencedb.service';
|
||||
import { AdminGuard } from '../auth/admin.guard';
|
||||
import { JwtAuthGuard } from '../auth/jwt.guard';
|
||||
|
||||
@Controller('pref')
|
||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
||||
@Controller('api/pref')
|
||||
export class PrefController {
|
||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
||||
@Post('set/:key')
|
||||
async register(
|
||||
@Request() req: AuthFasityRequest,
|
||||
@Body() register: AuthRegisterRequest,
|
||||
) {
|
||||
const user = await this.authService.createUser(
|
||||
register.username,
|
||||
register.password,
|
||||
);
|
||||
constructor(private prefService: SysPreferenceService) {}
|
||||
|
||||
if (HasFailed(user)) throw new ConflictException('User already exists');
|
||||
|
||||
if (register.isAdmin) {
|
||||
await this.authService.makeAdmin(user);
|
||||
@Get('sys/:key')
|
||||
async getSysPref(@Param('key') key: string) {
|
||||
const returned = await this.prefService.getPreference(key);
|
||||
if (HasFailed(returned)) {
|
||||
console.warn(returned.getReason());
|
||||
throw new InternalServerErrorException('Could not get preference');
|
||||
}
|
||||
|
||||
return user;
|
||||
return returned;
|
||||
}
|
||||
|
||||
@Post('sys/:key')
|
||||
async setSysPref(
|
||||
@Param('key') key: string,
|
||||
@Body() body: UpdateSysPreferenceRequest,
|
||||
) {
|
||||
const value = body.value;
|
||||
const returned = await this.prefService.setPreference(key, value);
|
||||
if (HasFailed(returned)) {
|
||||
console.warn(returned.getReason());
|
||||
throw new InternalServerErrorException('Could not set preference');
|
||||
}
|
||||
|
||||
return returned;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SysPreferenceModule } from '../../../collections/syspreferencesdb/syspreferencedb.module';
|
||||
import { PrefController } from './pref.controller';
|
||||
|
||||
@Module({
|
||||
imports: [SysPreferenceModule],
|
||||
controllers: [PrefController]
|
||||
})
|
||||
export class PrefModule {}
|
||||
|
||||
@@ -27,8 +27,10 @@ export class ImageController {
|
||||
if (!isHash(hash, 'sha256')) throw new BadRequestException('Invalid hash');
|
||||
|
||||
const image = await this.imagesService.retrieveComplete(hash);
|
||||
if (HasFailed(image))
|
||||
throw new NotFoundException('Image not found');
|
||||
if (HasFailed(image)) {
|
||||
console.warn(image.getReason());
|
||||
throw new NotFoundException('Could not find image');
|
||||
}
|
||||
|
||||
res.type(image.mime);
|
||||
return image.data;
|
||||
@@ -39,8 +41,10 @@ export class ImageController {
|
||||
if (!isHash(hash, 'sha256')) throw new BadRequestException('Invalid hash');
|
||||
|
||||
const image = await this.imagesService.retrieveInfo(hash);
|
||||
if (HasFailed(image))
|
||||
throw new NotFoundException('Image not found');
|
||||
if (HasFailed(image)) {
|
||||
console.warn(image.getReason());
|
||||
throw new NotFoundException('Could not find image');
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
@@ -53,7 +57,8 @@ export class ImageController {
|
||||
const fileBuffer = await multipart.image.toBuffer();
|
||||
const image = await this.imagesService.upload(fileBuffer);
|
||||
if (HasFailed(image)) {
|
||||
throw new InternalServerErrorException('Failed to upload image');
|
||||
console.warn(image.getReason());
|
||||
throw new InternalServerErrorException('Could not upload image');
|
||||
}
|
||||
|
||||
return image;
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import { generateRandomString } from '../util/random';
|
||||
import tuple from '../types/tuple';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
const SysPreferencesTuple = tuple('jwt_secret');
|
||||
|
||||
export const SysPreferences: string[] = SysPreferencesTuple;
|
||||
export type SysPreferences = typeof SysPreferencesTuple[number];
|
||||
|
||||
export class UpdateSysPreferenceRequest {
|
||||
@IsNotEmpty()
|
||||
value: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user