Created
December 12, 2021 00:50
-
-
Save eufelipemateus/bdcb51dac629c2527173eb9741197576 to your computer and use it in GitHub Desktop.
Scripts de examplo https://github.com/eufelipemateus/nestjs-files-mysql https://felipemateus.com/blog/2021/12/mysql-salvando-arquivos-com-nestjs--mysql
This file contains hidden or 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
import { | |
Controller, | |
Get, | |
Param, | |
Post, | |
Res, | |
UploadedFile, | |
UseInterceptors, | |
} from '@nestjs/common'; | |
import { FileInterceptor } from '@nestjs/platform-express'; | |
import { File } from './file.entity'; | |
import { FilesService } from './file'; | |
import fs = require('fs'); | |
import path = require('path'); | |
import { Readable } from 'stream'; | |
@Controller('file') | |
export class FileController { | |
constructor(private readonly fileService: FilesService) {} | |
@Post() | |
@UseInterceptors(FileInterceptor('file')) | |
async create(@UploadedFile() file, @Res() response) { | |
const data = { | |
file:file.originalname, | |
content_type: file.mimetype, | |
data: file.buffer, | |
} as unknown as File; | |
try { | |
const content = await this.fileService.create(data); | |
return response.status(200).json(content); | |
} catch (e) { | |
return response.status(500).json('Error ao tentar salvar'); | |
} | |
} | |
@Get() | |
getAll(): Promise<File[]> { | |
return this.fileService.findAll(); | |
} | |
@Get('/view/:id') | |
async getVideo(@Param('id') id: string, @Res() res) { | |
const fileData = await this.fileService.findOne(id); | |
res.set('Content-Type', fileData.content_type); | |
res.set('Content-Disposition', `attachment; filename=${fileData.file}`); | |
const myBuffer = Buffer.from(fileData.data); | |
const stream = Readable.from(myBuffer.toString()); | |
stream.pipe(res); | |
} | |
} |
This file contains hidden or 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
import { Injectable } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Repository } from 'typeorm'; | |
import { File } from './file.entity'; | |
@Injectable() | |
export class FilesService { | |
constructor( | |
@InjectRepository(File) | |
private contentsRepository: Repository<File>, | |
) {} | |
create(file: File) { | |
return this.contentsRepository.save(file); | |
} | |
findAll(): Promise<File[]> { | |
return this.contentsRepository.find(); | |
} | |
findOne(id: string): Promise<File> { | |
return this.contentsRepository.findOne(id); | |
} | |
async update(id: number, data: File): Promise<File> { | |
await this.contentsRepository.update({ id }, data); | |
return this.contentsRepository.findOne(id); | |
} | |
async remove(id: string): Promise<void> { | |
await this.contentsRepository.delete(id); | |
} | |
} |
This file contains hidden or 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
import { Injectable } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { Repository } from 'typeorm'; | |
import { File } from './file.entity'; | |
@Injectable() | |
export class FilesService { | |
constructor( | |
@InjectRepository(File) | |
private contentsRepository: Repository<File>, | |
) {} | |
create(file: File) { | |
return this.contentsRepository.save(file); | |
} | |
findAll(): Promise<File[]> { | |
return this.contentsRepository.find(); | |
} | |
findOne(id: string): Promise<File> { | |
return this.contentsRepository.findOne(id); | |
} | |
async update(id: number, data: File): Promise<File> { | |
await this.contentsRepository.update({ id }, data); | |
return this.contentsRepository.findOne(id); | |
} | |
async remove(id: string): Promise<void> { | |
await this.contentsRepository.delete(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment