add: upload file
This commit is contained in:
@@ -10,6 +10,7 @@ import { ProductModule } from './product/product.module';
|
||||
import { ConfigurableModule } from './configurable/configurable.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import configuration from './config/configuration';
|
||||
import { MulterModule } from '@nestjs/platform-express';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -30,6 +31,9 @@ import configuration from './config/configuration';
|
||||
DATABASE_PORT: Joi.number().default(5432),
|
||||
}),
|
||||
}),
|
||||
MulterModule.register({
|
||||
dest: './files',
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
useFactory: (configService: ConfigService) => {
|
||||
|
||||
@@ -10,5 +10,7 @@ export default () => {
|
||||
password: process.env.DATABASE_PASSWORD || '',
|
||||
name: process.env.DATABASE_NAME,
|
||||
},
|
||||
upload_dir: __dirname + '/../uploads',
|
||||
upload_url_path: '/files/',
|
||||
};
|
||||
};
|
||||
|
||||
@@ -9,9 +9,15 @@ import {
|
||||
ParseUUIDPipe,
|
||||
HttpStatus,
|
||||
Query,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import { RoleService } from './roles.service';
|
||||
import { CommissionService } from './commission.service';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { diskStorage } from 'multer';
|
||||
import { editFileName } from '../helper/file-handler';
|
||||
|
||||
@Controller({
|
||||
path: 'config',
|
||||
@@ -61,6 +67,11 @@ export class ConfigurableController {
|
||||
};
|
||||
}
|
||||
|
||||
@Get('/image/:imgpath')
|
||||
seeUploadedFile(@Param('imgpath') image, @Res() res) {
|
||||
return res.sendFile(image, { root: './files' });
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||
return {
|
||||
@@ -70,6 +81,23 @@ export class ConfigurableController {
|
||||
};
|
||||
}
|
||||
|
||||
@Post('/upload-files')
|
||||
@UseInterceptors(
|
||||
FileInterceptor('file', {
|
||||
storage: diskStorage({
|
||||
destination: './files',
|
||||
filename: editFileName,
|
||||
}),
|
||||
}),
|
||||
)
|
||||
async uploadedFile(@UploadedFile() file: Express.Multer.File) {
|
||||
const response = {
|
||||
originalname: file,
|
||||
filename: file.filename,
|
||||
};
|
||||
return response;
|
||||
}
|
||||
|
||||
@Put('/commission/:id')
|
||||
async updateCommission(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
|
||||
12
src/helper/file-handler.ts
Normal file
12
src/helper/file-handler.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import * as path from 'path';
|
||||
|
||||
export const editFileName = (req, file, callback) => {
|
||||
const name = file.originalname.split('.')[0];
|
||||
const fileExtName = path.extname(file.originalname);
|
||||
// const fileExtName = 'asdasd';
|
||||
const randomName = Array(4)
|
||||
.fill(null)
|
||||
.map(() => Math.round(Math.random() * 16).toString(16))
|
||||
.join('');
|
||||
callback(null, `${name}-${randomName}${fileExtName}`);
|
||||
};
|
||||
26
src/main.ts
26
src/main.ts
@@ -1,17 +1,14 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe, VersioningType} from '@nestjs/common';
|
||||
import { ValidationPipe, VersioningType } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Logger } from 'nestjs-pino';
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
const app = await NestFactory.create<NestExpressApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter(),
|
||||
// new FastifyAdapter(),
|
||||
{ bufferLogs: true },
|
||||
);
|
||||
|
||||
@@ -31,13 +28,14 @@ async function bootstrap() {
|
||||
const configService = app.get<ConfigService>(ConfigService);
|
||||
const port = configService.get<number>('port');
|
||||
|
||||
await app.listen(port, '0.0.0.0', (error, address) => {
|
||||
if (error) {
|
||||
logger.error(error);
|
||||
process.exit(1);
|
||||
} else {
|
||||
logger.log(`Server listening on ${address}`);
|
||||
}
|
||||
await app.listen(port, '0.0.0.0', () => {
|
||||
logger.log('Service Started');
|
||||
// if (error) {
|
||||
// logger.error(error);
|
||||
// process.exit(1);
|
||||
// } else {
|
||||
// logger.log(`Server listening on ${address}`);
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user