-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from HeliosLive/rest-fa-14
files upload added
- Loading branch information
Showing
7 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters