mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-13 23:35:39 +01:00
Add demo mode
This commit is contained in:
@@ -6,6 +6,7 @@ import { ImageModule } from './routes/image/imageroute.module';
|
|||||||
import { ServeStaticModule } from '@nestjs/serve-static';
|
import { ServeStaticModule } from '@nestjs/serve-static';
|
||||||
import Config from './env';
|
import Config from './env';
|
||||||
import { ImageEntity } from './collections/imagedb/image.entity';
|
import { ImageEntity } from './collections/imagedb/image.entity';
|
||||||
|
import { DemoManagerModule } from './managers/demo/demomanager.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -25,6 +26,7 @@ import { ImageEntity } from './collections/imagedb/image.entity';
|
|||||||
}),
|
}),
|
||||||
AuthModule,
|
AuthModule,
|
||||||
ImageModule,
|
ImageModule,
|
||||||
|
DemoManagerModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ import { Repository } from 'typeorm';
|
|||||||
import { ImageEntity } from './image.entity';
|
import { ImageEntity } from './image.entity';
|
||||||
import Crypto from 'crypto';
|
import Crypto from 'crypto';
|
||||||
import { SupportedMime } from './mimes.service';
|
import { SupportedMime } from './mimes.service';
|
||||||
import { AsyncFailable, Fail, HasFailed, HasSuccess } from 'imagur-shared/dist/types';
|
import {
|
||||||
|
AsyncFailable,
|
||||||
|
Fail,
|
||||||
|
HasFailed,
|
||||||
|
HasSuccess,
|
||||||
|
} from 'imagur-shared/dist/types';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ImageDBService {
|
export class ImageDBService {
|
||||||
@@ -73,6 +78,15 @@ export class ImageDBService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async deleteAll(): AsyncFailable<true> {
|
||||||
|
try {
|
||||||
|
await this.imageRepository.delete({});
|
||||||
|
} catch (e: any) {
|
||||||
|
return Fail(e?.message);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private hash(image: Buffer): string {
|
private hash(image: Buffer): string {
|
||||||
return Crypto.createHash('sha256').update(image).digest('hex');
|
return Crypto.createHash('sha256').update(image).digest('hex');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ import { fileURLToPath } from 'url';
|
|||||||
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../');
|
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../');
|
||||||
|
|
||||||
const Config = {
|
const Config = {
|
||||||
|
main: {
|
||||||
host: process.env.IMAGUR_HOST || '0.0.0.0',
|
host: process.env.IMAGUR_HOST || '0.0.0.0',
|
||||||
port: process.env.IMAGUR_PORT || 8080,
|
port: process.env.IMAGUR_PORT || 8080,
|
||||||
|
},
|
||||||
database: {
|
database: {
|
||||||
host: process.env.IMAGUR_DB_HOST ?? 'localhost',
|
host: process.env.IMAGUR_DB_HOST ?? 'localhost',
|
||||||
port: process.env.IMAGUR_DB_PORT
|
port: process.env.IMAGUR_DB_PORT
|
||||||
@@ -38,6 +40,12 @@ const Config = {
|
|||||||
join(packageRoot, '../frontend/dist'),
|
join(packageRoot, '../frontend/dist'),
|
||||||
backendRoutes: ['i', 'api'],
|
backendRoutes: ['i', 'api'],
|
||||||
},
|
},
|
||||||
|
demo: {
|
||||||
|
enabled: process.env.IMAGUR_DEMO?.toLowerCase() === 'true',
|
||||||
|
interval: process.env.IMAGUR_DEMO_INTERVAL
|
||||||
|
? parseInt(process.env.IMAGUR_DEMO_INTERVAL)
|
||||||
|
: 1000 * 60 * 5,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Config;
|
export default Config;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ async function bootstrap() {
|
|||||||
app.useGlobalFilters(new MainExceptionFilter());
|
app.useGlobalFilters(new MainExceptionFilter());
|
||||||
app.useGlobalInterceptors(new SuccessInterceptor());
|
app.useGlobalInterceptors(new SuccessInterceptor());
|
||||||
app.useGlobalPipes(new ValidationPipe({ disableErrorMessages: true }));
|
app.useGlobalPipes(new ValidationPipe({ disableErrorMessages: true }));
|
||||||
await app.listen(Config.port, Config.host);
|
await app.listen(Config.main.port, Config.main.host);
|
||||||
}
|
}
|
||||||
|
|
||||||
bootstrap().catch(console.error);
|
bootstrap().catch(console.error);
|
||||||
|
|||||||
20
backend/src/managers/demo/demomanager.module.ts
Normal file
20
backend/src/managers/demo/demomanager.module.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { Module, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { ImageDBModule } from '../../collections/imagedb/imagedb.module';
|
||||||
|
import Config from '../../env';
|
||||||
|
import { DemoManagerService } from './demomanager.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [ImageDBModule],
|
||||||
|
providers: [DemoManagerService],
|
||||||
|
})
|
||||||
|
export class DemoManagerModule implements OnModuleInit {
|
||||||
|
constructor(private readonly demoManagerService: DemoManagerService) {}
|
||||||
|
|
||||||
|
private interval: NodeJS.Timeout;
|
||||||
|
|
||||||
|
onModuleInit() {
|
||||||
|
if (Config.demo.enabled) {
|
||||||
|
setInterval(this.demoManagerService.execute, Config.demo.interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
backend/src/managers/demo/demomanager.service.ts
Normal file
18
backend/src/managers/demo/demomanager.service.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { ImageDBService } from '../../collections/imagedb/imagedb.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DemoManagerService {
|
||||||
|
private readonly logger = new Logger('DemoManagerService');
|
||||||
|
|
||||||
|
constructor(private readonly imagesService: ImageDBService) {}
|
||||||
|
|
||||||
|
private async executeAsync() {
|
||||||
|
this.logger.log('Executing demo cleanup');
|
||||||
|
await this.imagesService.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public execute() {
|
||||||
|
this.executeAsync().catch(this.logger.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user