Skip to content

Commit

Permalink
Merge pull request #14 from HeliosLive/rest-fa-14
Browse files Browse the repository at this point in the history
files upload added
  • Loading branch information
HeliosLive authored Feb 9, 2020
2 parents f84505c + 32e7fc8 commit d382c68
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bcrypt": "^3.0.7",
"class-transformer": "^0.2.3",
"class-validator": "^0.11.0",
"cloudinary": "^1.19.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.9",
"reflect-metadata": "^0.1.13",
Expand Down
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { LoginModule } from './login/login.module';
import { TokenMiddleware } from 'libs/middlewares/token.middleware';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from 'libs/guards/auth.guard';
import { UploadModule } from './upload/upload.module';
import { MulterModule } from '@nestjs/platform-express';

@Module({
imports: [
Expand All @@ -42,6 +44,10 @@ import { AuthGuard } from 'libs/guards/auth.guard';
LibsModule,
TotalModule,
LoginModule,
UploadModule,
MulterModule.register({
dest: './uploads',
}),
MongooseModule.forRoot(environment.mongoUrl),
],
controllers: [AppController],
Expand Down
28 changes: 28 additions & 0 deletions src/upload/upload.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Controller,
Post,
UseInterceptors,
UploadedFiles,
} from '@nestjs/common';
import { UploadService } from './upload.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import { extname } from 'path';
import { diskStorage } from 'multer';

const storageOptions = diskStorage({
destinaton: './uploads',
filename: (req, file, callback) => {
callback(null, `${Date.now()}.${extname(file.originalname)}`);
},
});

@Controller('upload')
export class UploadController {
constructor(private readonly uploadService: UploadService) {}

@Post()
@UseInterceptors(FilesInterceptor('files', 20, { storage: storageOptions }))
async uploadFile(@UploadedFiles() files): Promise<any> {
return this.uploadService.upload(files);
}
}
10 changes: 10 additions & 0 deletions src/upload/upload.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { UploadController } from './upload.controller';
import { UploadService } from './upload.service';

@Module({
imports: [],
controllers: [UploadController],
providers: [UploadService],
})
export class UploadModule {}
32 changes: 32 additions & 0 deletions src/upload/upload.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@nestjs/common';
import * as cloudinary from 'cloudinary';
import environment from 'tools/environment/environment';
import { response } from 'express';

@Injectable()
export class UploadService {
constructor() {
cloudinary.v2.config({
cloud_name: environment.cloudinary.cloud_name,
api_key: environment.cloudinary.api_key,
api_secret: environment.cloudinary.api_secret,
});
}

async upload(files: any[]): Promise<any> {
let result = [];
try {
for (const file of files) {
await cloudinary.v2.uploader.upload(file.path, function(
error,
response,
) {
result.push(response);
});
}
return await result;
} catch (err) {
return await err;
}
}
}
5 changes: 5 additions & 0 deletions tools/environment/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ export default {
'mongodb+srv://udemy:Udemy_123@nestjs-jcbid.mongodb.net/test?retryWrites=true&w=majority',
hashText: 'myStrongPasswordHashText',
jwtText: '32dhfnct94q',
cloudinary: {
cloud_name: 'your_own_cloud_name',
api_key: 'your_own_api_key',
api_secret: 'your_own_api_secret',
},
};

0 comments on commit d382c68

Please sign in to comment.