91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import {
|
|
MiddlewareConsumer,
|
|
Module,
|
|
NestModule,
|
|
RequestMethod,
|
|
} from '@nestjs/common';
|
|
import { MulterModule } from '@nestjs/platform-express';
|
|
import * as multer from 'multer';
|
|
import * as mime from 'mime-types';
|
|
import { join } from 'path';
|
|
import { AuthMiddleware } from 'src/middleware/auth.middleware';
|
|
import { FlashMiddleware } from 'src/middleware/flash.middleware';
|
|
import { ValidateCSRFMiddleware } from 'src/middleware/validate-csrf.middleware';
|
|
import { ConfigurationModule } from 'src/modules/config/config.module';
|
|
import { ConfigurationService } from 'src/modules/config/config.service';
|
|
import { UploadModule } from 'src/modules/objects/upload/upload.module';
|
|
import { UserModule } from 'src/modules/objects/user/user.module';
|
|
import { OAuth2TokenModule } from 'src/modules/objects/oauth2-token/oauth2-token.module';
|
|
import { OAuth2ClientModule } from 'src/modules/objects/oauth2-client/oauth2-client.module';
|
|
import { UserTokenModule } from 'src/modules/objects/user-token/user-token.module';
|
|
import { OAuth2Module } from '../../oauth2/oauth2.module';
|
|
import { SettingsController } from './settings.controller';
|
|
import { SettingsService } from './settings.service';
|
|
import { CSRFMiddleware } from 'src/middleware/csrf.middleware';
|
|
import { UserMiddleware } from 'src/middleware/user.middleware';
|
|
import { AuditModule } from 'src/modules/objects/audit/audit.module';
|
|
|
|
@Module({
|
|
controllers: [SettingsController],
|
|
imports: [
|
|
ConfigurationModule,
|
|
UploadModule,
|
|
UserModule,
|
|
UserTokenModule,
|
|
AuditModule,
|
|
OAuth2Module,
|
|
OAuth2ClientModule,
|
|
OAuth2TokenModule,
|
|
MulterModule.registerAsync({
|
|
imports: [ConfigurationModule],
|
|
useFactory: async (config: ConfigurationService) => {
|
|
return {
|
|
storage: multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, join(__dirname, '..', '..', '..', '..', 'uploads'));
|
|
},
|
|
filename: (req, file, cb) => {
|
|
const hashTruncate = req.user.uuid.split('-')[0];
|
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
const ext = mime.extension(file.mimetype);
|
|
cb(null, `user-${hashTruncate}-${timestamp}.${ext}`);
|
|
},
|
|
}),
|
|
limits: {
|
|
fileSize: 1.049e7, // 10 MiB
|
|
},
|
|
fileFilter: (req, file, cb) => {
|
|
if (
|
|
!file.mimetype.startsWith('image/') ||
|
|
file.mimetype.includes('svg')
|
|
) {
|
|
return cb(new Error('Invalid file type.'), false);
|
|
}
|
|
|
|
cb(null, true);
|
|
},
|
|
};
|
|
},
|
|
inject: [ConfigurationService],
|
|
}),
|
|
],
|
|
providers: [SettingsService],
|
|
})
|
|
export class SettingsModule implements NestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply(CSRFMiddleware, UserMiddleware)
|
|
.forRoutes(SettingsController);
|
|
consumer
|
|
.apply(ValidateCSRFMiddleware)
|
|
.forRoutes(
|
|
{ path: '/account*', method: RequestMethod.POST },
|
|
{ path: '/account*', method: RequestMethod.PATCH },
|
|
{ path: '/account*', method: RequestMethod.DELETE },
|
|
);
|
|
|
|
consumer.apply(AuthMiddleware).forRoutes('account*');
|
|
consumer.apply(FlashMiddleware).forRoutes('account*');
|
|
}
|
|
}
|