add: upload file
This commit is contained in:
parent
bee8557055
commit
8941184656
|
@ -36,6 +36,7 @@
|
|||
"class-validator": "^0.13.1",
|
||||
"crypto": "^1.0.1",
|
||||
"decimal.js": "^10.3.1",
|
||||
"fs-extra": "^10.0.0",
|
||||
"joi": "^17.4.2",
|
||||
"lodash": "^4.17.21",
|
||||
"nestjs-pino": "^2.3.1",
|
||||
|
@ -57,6 +58,7 @@
|
|||
"@nestjs/testing": "^8.0.0",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/multer": "^1.4.7",
|
||||
"@types/node": "^16.0.0",
|
||||
"@types/passport-jwt": "^3.0.6",
|
||||
"@types/passport-local": "^1.0.34",
|
||||
|
|
|
@ -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}`);
|
||||
// }
|
||||
});
|
||||
}
|
||||
|
||||
|
|
39
yarn.lock
39
yarn.lock
|
@ -990,6 +990,13 @@
|
|||
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
|
||||
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==
|
||||
|
||||
"@types/multer@^1.4.7":
|
||||
version "1.4.7"
|
||||
resolved "https://registry.yarnpkg.com/@types/multer/-/multer-1.4.7.tgz#89cf03547c28c7bbcc726f029e2a76a7232cc79e"
|
||||
integrity sha512-/SNsDidUFCvqqcWDwxv2feww/yqhNeTRL5CVoL3jU4Goc4kKEL10T7Eye65ZqPNi4HRx8sAEX59pV1aEH7drNA==
|
||||
dependencies:
|
||||
"@types/express" "*"
|
||||
|
||||
"@types/node@*", "@types/node@^16.0.0":
|
||||
version "16.11.10"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.10.tgz#2e3ad0a680d96367103d3e670d41c2fed3da61ae"
|
||||
|
@ -1803,15 +1810,6 @@ chalk@^1.1.1:
|
|||
strip-ansi "^3.0.0"
|
||||
supports-color "^2.0.0"
|
||||
|
||||
chalk@^2.0.0:
|
||||
version "2.4.2"
|
||||
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
|
||||
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
|
||||
dependencies:
|
||||
ansi-styles "^3.2.1"
|
||||
escape-string-regexp "^1.0.5"
|
||||
supports-color "^5.3.0"
|
||||
|
||||
chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.1:
|
||||
version "4.1.2"
|
||||
resolved "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz"
|
||||
|
@ -2840,7 +2838,7 @@ fresh@0.5.2:
|
|||
resolved "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz"
|
||||
integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=
|
||||
|
||||
fs-extra@10.0.0:
|
||||
fs-extra@10.0.0, fs-extra@^10.0.0:
|
||||
version "10.0.0"
|
||||
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.0.0.tgz#9ff61b655dde53fb34a82df84bb214ce802e17c1"
|
||||
integrity sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==
|
||||
|
@ -4599,7 +4597,6 @@ picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
|
|||
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
|
||||
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==
|
||||
|
||||
pino-abstract-transport@v0.5.0:
|
||||
pino-abstract-transport@^0.5.0, pino-abstract-transport@v0.5.0:
|
||||
version "0.5.0"
|
||||
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-0.5.0.tgz#4b54348d8f73713bfd14e3dc44228739aa13d9c0"
|
||||
|
@ -4681,23 +4678,6 @@ pino@^7.0.5:
|
|||
sonic-boom "^2.2.1"
|
||||
thread-stream "^0.13.0"
|
||||
|
||||
pino@^7.5.1:
|
||||
version "7.5.1"
|
||||
resolved "https://registry.yarnpkg.com/pino/-/pino-7.5.1.tgz#6e4cd3389e9365caf64039d4b83dcfa84ed43fee"
|
||||
integrity sha512-Wzo2G7CLaRHKOz3+Ex006LC5Xi0xEUm+mwm/h0NKzuKZONdekcbmjXg7vWDoO8hVTGX+1RuUy2fwlzvZ24EI5A==
|
||||
dependencies:
|
||||
fast-redact "^3.0.0"
|
||||
fastify-warning "^0.2.0"
|
||||
get-caller-file "^2.0.5"
|
||||
on-exit-leak-free "^0.2.0"
|
||||
pino-abstract-transport v0.5.0
|
||||
pino-std-serializers "^4.0.0"
|
||||
quick-format-unescaped "^4.0.3"
|
||||
real-require "^0.1.0"
|
||||
safe-stable-stringify "^2.1.0"
|
||||
sonic-boom "^2.2.1"
|
||||
thread-stream "^0.13.0"
|
||||
|
||||
pirates@^4.0.1:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
|
||||
|
@ -4988,7 +4968,6 @@ reusify@^1.0.4:
|
|||
resolved "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz"
|
||||
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==
|
||||
|
||||
rfdc@^1.1.4, rfdc@^1.2.0:
|
||||
rfdc@^1.1.4, rfdc@^1.2.0, rfdc@^1.3.0:
|
||||
version "1.3.0"
|
||||
resolved "https://registry.npmjs.org/rfdc/-/rfdc-1.3.0.tgz"
|
||||
|
@ -5084,7 +5063,6 @@ schema-utils@^3.1.0, schema-utils@^3.1.1:
|
|||
ajv "^6.12.5"
|
||||
ajv-keywords "^3.5.2"
|
||||
|
||||
secure-json-parse@^2.0.0:
|
||||
secure-json-parse@^2.0.0, secure-json-parse@^2.4.0:
|
||||
version "2.4.0"
|
||||
resolved "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.4.0.tgz"
|
||||
|
@ -6124,4 +6102,3 @@ zen-observable@0.8.15:
|
|||
version "0.8.15"
|
||||
resolved "https://registry.npmjs.org/zen-observable/-/zen-observable-0.8.15.tgz"
|
||||
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user