-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e79b55b
commit 1e8c28e
Showing
3 changed files
with
38 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import { Controller, Post, UploadedFile, UseInterceptors, Res } from '@nestjs/common'; | ||
import { FileInterceptor } from '@nestjs/platform-express'; | ||
import { | ||
Controller, | ||
Post, | ||
UploadedFile, | ||
UseInterceptors, | ||
Res, | ||
} from '@nestjs/common' | ||
import { FileInterceptor } from '@nestjs/platform-express' | ||
// import { Multer } from 'multer'; | ||
import { Response } from 'express'; | ||
import { Response } from 'express' | ||
|
||
@Controller('file') | ||
export class UploadController { | ||
@Post('upload') | ||
@UseInterceptors(FileInterceptor('file')) | ||
async uploadFile(@UploadedFile() file: Express.Multer.File, @Res() res: Response) { | ||
console.log(file); | ||
|
||
res.json({ message: 'File uploaded successfully' }); | ||
@UseInterceptors(FileInterceptor('file', { dest: './uploads'})) | ||
async uploadFile( | ||
@UploadedFile() file: Express.Multer.File, | ||
@Res() res: Response, | ||
) { | ||
console.log(file) | ||
|
||
res.json({ message: 'File uploaded successfully' }) | ||
} | ||
} | ||
|
||
export default UploadController; | ||
export default UploadController |
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { Resolver, Mutation, Args } from '@nestjs/graphql'; | ||
import { GraphQLUpload } from 'graphql-upload'; | ||
import UploadService from './upload.service'; | ||
import { Resolver, Mutation, Args } from '@nestjs/graphql' | ||
import { GraphQLUpload } from 'graphql-upload' | ||
import UploadService from './upload.service' | ||
|
||
@Resolver() | ||
export default class UploadResolver { | ||
constructor(private readonly uploadService: UploadService) {} | ||
|
||
@Mutation(() => String) | ||
async uploadFile(@Args({ name: 'file', type: () => GraphQLUpload }) file: any): Promise<string> { | ||
const result = await this.uploadService.processFile(file); | ||
async uploadFile( | ||
@Args({ name: 'file', type: () => GraphQLUpload }) file: any, | ||
): Promise<string> { | ||
const result = await this.uploadService.processFile(file) | ||
|
||
return result; | ||
return result | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import * as fs from 'fs'; | ||
import path from 'path'; | ||
import { Injectable } from '@nestjs/common' | ||
import * as fs from 'fs' | ||
import path from 'path' | ||
|
||
@Injectable() | ||
export default class UploadService { | ||
async processFile(file: Express.Multer.File): Promise<string> { | ||
try { | ||
const uploadDirectory = path.join(__dirname, 'Upload'); | ||
const uploadDirectory = path.join(__dirname, 'Upload') | ||
if (!fs.existsSync(uploadDirectory)) { | ||
fs.mkdirSync(uploadDirectory); | ||
fs.mkdirSync(uploadDirectory) | ||
} | ||
const filename = `${Date.now()}_${file.originalname}`; | ||
const filename = `${Date.now()}_${file.originalname}` | ||
|
||
const filePath = path.join(uploadDirectory, filename); | ||
fs.writeFileSync(filePath, file.buffer); | ||
const filePath = path.join(uploadDirectory, filename) | ||
fs.writeFileSync(filePath, file.buffer) | ||
|
||
return 'File has been saved successfully'; | ||
return 'File has been saved successfully' | ||
} catch (error) { | ||
console.error('Error:', error); | ||
throw new Error('Failed to save the file'); | ||
console.error('Error:', error) | ||
throw new Error('Failed to save the file') | ||
} | ||
} | ||
} | ||
} |