6 min read
Original source

Uploading files to the server

So far, in this series, we’ve described two ways of storing files on a server. In the 10th article, we’ve uploaded files to Amazon S3. While it is very…

So far, in this series, we’ve described two ways of storing files on a server. In the 10th article, we’ve uploaded files to Amazon S3. While it is very scalable, we might not want to use cloud services such as AWS for various reasons. Therefore, in the 54th part of the series, we’ve learned how to store files straight in our PostgreSQL database. While it has some advantages, it might be perceived as less than ideal in terms of performance. In this article, we look into using NestJS to store uploaded files on the server. Again, we persist some information into the database, but it is just the metadata this time. Storing the files on the server Fortunately, NestJS makes it very easy to store the files on the server. We need to pass additional arguments to the FileInterceptor. users.service.ts import { UsersService } from './users.service'; import { Controller, Post, Req, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common'; import JwtAuthenticationGuard from '../authentication/jwt-authentication.guard'; import RequestWithUser from '../authentication/requestWithUser.interface'; import { Express } from 'express'; import { FileInterceptor } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; @Controller('users') export class UsersController { constructor( private readonly usersService: UsersService, ) {} @Post('avatar') @UseGuards(JwtAuthenticationGuard) @UseInterceptors(FileInterceptor('file', { storage: diskStorage({ destination: './uploadedFiles/avatars' }) })) async addAvatar(@Req() request: RequestWithUser, @UploadedFile() file: Express.Multer.File) { return this.usersService.addAvatar(request.user.id, { path: file.path, filename: file.originalname, mimetype: file.mimetype }); } }When we do the above, NestJS stores uploaded files in the ./uploadedFiles/avatars directory. There are a few issues with the above approach, though. First, we might need more than one endpoint to accept files. In such a case, we would need to repeat some parts of the configuration for each one of them. Also, we should put the ./uploadedFiles part of the destination in an environment variable to change it based on the environment the app runs in. Extending the FileInterceptor A way to achieve the above is to extend the FileInterceptor. After looking under the hood of NestJS, we can see that it uses the mixin pattern. Because FileInterceptor is not a class, we can’t use the extend keyword. We want to extend the FileInterceptor functionalities while: having Dependency Injection to inject the ConfigService, being able to pass additional properties from the controller. To do that, we can create our mixin: localFiles.interceptor.ts import { FileInterceptor } from '@nestjs/platform-express'; import { Injectable, mixin, NestInterceptor, Type } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'; import { diskStorage } from 'multer'; interface LocalFilesInterceptorOptions { fieldName: string; path?: string; } function LocalFilesInterceptor (options: LocalFilesInterceptorOptions): Type { @Injectable() class Interceptor implements NestInterceptor { fileInterceptor: NestInterceptor; constructor(configService: ConfigService) { const filesDestination = configService.get('UPLOADED_FILES_DESTINATION'); const destination = `${filesDestination}${options.path}` const multerOptions: MulterOptions = { storage: diskStorage({ destination }) } this.fileInterceptor = new (FileInterceptor(options.fieldName, multerOptions)); } intercept(...args: Parameters<NestInterceptor['intercept']>) { return this.fileInterceptor.intercept(...args); } } return mixin(Interceptor); } export default LocalFilesInterceptor;Above, we use the UPLOADED_FILES_DESTINATION variable and concatenate it with the provided path. To do that, let’s define the necessary environment variable. app.module.ts import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from '@hapi/joi'; @Module({ imports: [ ConfigModule.forRoot({ validationSchema: Joi.object({ UPLOADED_FILES_DESTINATION: Joi.string().required(), // ... }) }), // ... ], // ... }) export class AppModule { // ... } .env UPLOADED_FILES_DESTINATION=./uploadedFiles # ...When all of the above is ready, we can use the LocalFilesInterceptor in our controller: users.controller.ts import { UsersService } from './users.service'; import { Controller, Post, Req, UploadedFile, UseGuards, UseInterceptors } from '@nestjs/common'; import JwtAuthenticationGuard from '../authentication/jwt-authentication.guard'; import RequestWithUser from '../authentication/requestWithUser.interface'; import { Express } from 'express'; import LocalFilesInterceptor from '../localFiles/localFiles.interceptor'; @Controller('users') export class UsersController { constructor( private readonly usersService: UsersService, ) {} @Post('avatar') @UseGuards(JwtAuthenticationGuard) @UseInterceptors(LocalFilesInterceptor({ fieldName: 'file', path: '/avatars' })) async addAvatar(@Req() request: RequestWithUser, @UploadedFile() file: Express.Multer.File) { return this.usersService.addAvatar(request.user.id, { path: file.path, filename: file.originalname, mimetype: file.mimetype }); } } Saving the metadata in the database Besides storing the file on the server, we also need to save the file’s metadata in the database. Since NestJS generates a random filename for uploaded files, we also want to store the original filename. To do all of the above, we need to create an entity for the metadata. localFile.entity.ts import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; @Entity() class LocalFile { @PrimaryGeneratedColumn() public id: number; @Column() filename: string; @Column() path: string; @Column() mimetype: string; } export default LocalFile; localFile.dto.ts interface LocalFileDto { filename: string; path: string; mimetype: string; }We also need to create a relationship between users and files. user.entity.ts import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm'; import LocalFile from '../localFiles/localFile.entity'; @Entity() class User { @PrimaryGeneratedColumn() public id: number; @JoinColumn({ name: 'avatarId' }) @OneToOne( () => LocalFile, { nullable: true } ) public avatar?: LocalFile; @Column({ nullable: true }) public avatarId?: number; // ... } export default User; We add the avatarId column above so that the entity of the user can hold the id of the avatar without joining all of the data of the avatar. While we’re at it, we also need to create the basics of the LocalFilesService: localFiles.service.ts import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import LocalFile from './localFile.entity'; @Injectable() class LocalFilesService { constructor( @InjectRepository(LocalFile) private localFilesRepository: Repository, ) {} async saveLocalFileData(fileData: LocalFileDto) { const newFile = await this.localFilesRepository.create(fileData) await this.localFilesRepository.save(newFile); return newFile; } } export default LocalFilesService;The last step is to use the saveLocalFileData method in the UsersService: users.service.ts import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Connection, In } from 'typeorm'; import User from './user.entity'; import LocalFilesService from '../localFiles/localFiles.service'; @Injectable() export class UsersService { constructor( @InjectRepository(User) private usersRepository: Repository, private localFilesService: LocalFilesService ) {} async addAvatar(userId: number, fileData: LocalFileDto) { const avatar = await this.localFilesService.saveLocalFileData(fileData); await this.usersRepository.update(userId, { avatarId: avatar.id }) } // ... } Retrieving the files Now, the user can retrieve the id of their avatar. To download the file with a given id, we can create a controller that streams the content. The first step in achieving the above is extending the LocalFilesService:import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import LocalFile from './localFile.entity'; @Injectable() class LocalFilesService { constructor( @InjectRepository(LocalFile) private localFilesRepository: Repository, ) {} async getFileById(fileId: number) { const file = await this.localFilesRepository.findOne(fileId); if (!file) { throw new NotFoundException(); } return file; } // ... } export default LocalFilesService;We also need to create a controller that uses the above method: localFiles.controller.ts import { Controller, Get, Param, UseInterceptors, ClassSerializerInterceptor, StreamableFile, Res, ParseIntPipe, } from '@nestjs/common'; import LocalFilesService from './localFiles.service'; import { Response } from 'express'; import { createReadStream } from 'fs'; import { join } from 'path'; @Controller('local-files') @UseInterceptors(ClassSerializerInterceptor) export default class LocalFilesController { constructor( private readonly localFilesService: LocalFilesService ) {} @Get(':id') async getDatabaseFileById(@Param('id',

Uploading files to the server | NestJS.io