├── .prettierrc ├── nest-cli.json ├── tsconfig.build.json ├── screenshots ├── screenshot-01.png └── screenshot-02.png ├── src ├── app.service.ts ├── main.ts ├── core │ └── storage │ │ ├── models │ │ └── file-metadata.model.ts │ │ ├── storage.module.ts │ │ ├── storage.controller.spec.ts │ │ ├── storage.controller.ts │ │ ├── uppy.service.ts │ │ └── tus.service.ts ├── config │ └── storage.config.ts └── app.module.ts ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── .env.sample ├── tsconfig.json ├── public ├── index.html ├── tus-upload.html └── s3-upload.html ├── .gitignore ├── .eslintrc.js ├── README.md └── package.json /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /screenshots/screenshot-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tumainimosha/nestjs-resumable-upload-demo/HEAD/screenshots/screenshot-01.png -------------------------------------------------------------------------------- /screenshots/screenshot-02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tumainimosha/nestjs-resumable-upload-demo/HEAD/screenshots/screenshot-02.png -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | app.enableCors(); 7 | await app.listen(3000); 8 | } 9 | bootstrap(); 10 | -------------------------------------------------------------------------------- /src/core/storage/models/file-metadata.model.ts: -------------------------------------------------------------------------------- 1 | 2 | export class FileMetadata { 3 | 4 | public relativePath?: string; 5 | public name?: string; 6 | public type?: string; 7 | public filetype?: string; 8 | public filename?: string; 9 | public extension?: string; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /.env.sample: -------------------------------------------------------------------------------- 1 | # Storage Config 2 | AWS_S3_BUCKET_NAME = 3 | AWS_ACCESS_KEY_ID = 4 | AWS_SECRET_ACCESS_KEY = 5 | AWS_DEFAULT_REGION = 6 | 7 | # Storage driver can be 'local' or 's3' 8 | TUS_STORAGE_DRIVER = local 9 | -------------------------------------------------------------------------------- /src/core/storage/storage.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { StorageController } from './storage.controller'; 3 | import { TusService } from './tus.service'; 4 | import { UppyService } from './uppy.service'; 5 | 6 | @Module({ 7 | controllers: [StorageController], 8 | providers: [ 9 | TusService, 10 | UppyService, 11 | ], 12 | }) 13 | export class StorageModule { } 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es2017", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./", 12 | "incremental": true 13 | }, 14 | "exclude": ["node_modules", "dist"] 15 | } 16 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | NestJs Resumable Upload Example 4 | 5 | 6 | 7 |

NestJs Resumable Upload Example

8 | 9 | 10 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/config/storage.config.ts: -------------------------------------------------------------------------------- 1 | import * as dotenv from 'dotenv'; 2 | 3 | // loading .env file 4 | dotenv.config(); 5 | 6 | export const storageConfig = { 7 | storageDriver: process.env.TUS_STORAGE_DRIVER || 'local', // valid: 'local', 's3' 8 | 9 | accessKeyId: process.env.AWS_ACCESS_KEY_ID, 10 | secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, 11 | region: process.env.AWS_DEFAULT_REGION, 12 | bucket: process.env.AWS_BUCKET, 13 | }; 14 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | import { ServeStaticModule } from '@nestjs/serve-static'; 4 | import { join } from 'path'; 5 | import { StorageModule } from './core/storage/storage.module'; 6 | 7 | @Module({ 8 | imports: [ 9 | ServeStaticModule.forRoot({ 10 | rootPath: join(__dirname, '..', 'public'), 11 | }), 12 | StorageModule, 13 | ], 14 | controllers: [], 15 | providers: [AppService], 16 | }) 17 | export class AppModule { } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json 35 | 36 | # Env file 37 | .env 38 | 39 | # Local file store 40 | local-store/ 41 | -------------------------------------------------------------------------------- /src/core/storage/storage.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { StorageController } from './storage.controller'; 3 | 4 | describe('Storage Controller', () => { 5 | let controller: StorageController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [StorageController], 10 | }).compile(); 11 | 12 | controller = module.get(StorageController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/core/storage/storage.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, All, Req, Res } from '@nestjs/common'; 2 | import { TusService } from './tus.service'; 3 | import { UppyService } from './uppy.service'; 4 | 5 | @Controller() 6 | export class StorageController { 7 | 8 | constructor( 9 | private tusService: TusService, 10 | private uppyService: UppyService, 11 | ) { } 12 | 13 | @All('files') 14 | async tus(@Req() req, @Res() res) { 15 | return this.tusService.handleTus(req, res); 16 | } 17 | 18 | @All('uppy-companion') 19 | async companion(@Req() req, @Res() res) { 20 | return this.uppyService.handleCompanion(req, res); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/eslint-recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'prettier', 12 | 'prettier/@typescript-eslint', 13 | ], 14 | root: true, 15 | env: { 16 | node: true, 17 | jest: true, 18 | }, 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /public/tus-upload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | NestJs Resumable Tus Upload Example 4 | 5 | 6 | 10 | 11 | 12 | 13 |

NestJs Resumable Tus Upload Example

14 | 15 | 16 |
17 |
18 |
19 |
20 |
21 |
22 | 23 | 24 | 25 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /public/s3-upload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | NestJs Resumable S3 Upload Example 4 | 5 | 6 | 10 | 11 | 12 | 13 |

NestJs Resumable S3 Upload Example

14 | 15 | 16 |
17 |
18 |
19 |
20 |
21 |
22 | 23 | 24 | 25 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NestJs Tus Server Example 2 | 3 | > ## Motivation 4 | > 5 | > I wrote this demo code while researching use of Resumable upload implementation for NestJS, as a way for myself to tinker with different implementation before I include in production code, and also to provide me with quick reference in the future. 6 | 7 | ## Description 8 | 9 | An example implementation of [TUS](https://tus.io) file server in [NestJS](https://github.com/nestjs/nest) framework. 10 | 11 | ## Installation 12 | 13 | ```bash 14 | $ npm install 15 | ``` 16 | 17 | Initialize env file. 18 | 19 | ```bash 20 | $ cp .env.example .env 21 | ``` 22 | 23 | By default this uses the local file store option, but can also store to S3. Need to provide AWS config in .env for this to work. 24 | 25 | ## Running the app 26 | 27 | ```bash 28 | # development 29 | $ npm run start 30 | 31 | # watch mode 32 | $ npm run start:dev 33 | 34 | # production mode 35 | $ npm run start:prod 36 | ``` 37 | 38 | Open browser to access demo frontend at URL below 39 | 40 | [http://127.0.0.1:3000/index.html](http://127.0.0.1:3000/index.html) 41 | 42 | #### Frontend using Uppy uploader 43 | 44 | screenshot-01 45 | 46 | #### Uploading... 47 | 48 | screenshot-02 49 | 50 | ### Bookmarks 51 | 52 | Below are some material I found helpful when researching use of Tus Protocol for resumable uploads in NodeJS 53 | 54 | - Tus.io S3 File Store https://tus.io/blog/2016/03/07/tus-s3-backend.html 55 | 56 | - JSConfUS 2013 Talk on Tus.io https://www.youtube.com/watch?v=ilCxWswGm1I 57 | 58 | - Can TUSD run in AWS Lambda - NO https://github.com/tus/tusd/issues/291 59 | 60 | - Uppy AWS S3 Multipart https://uppy.io/docs/aws-s3-multipart/ 61 | -------------------------------------------------------------------------------- /src/core/storage/uppy.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, OnModuleInit, Logger } from '@nestjs/common'; 2 | import companion = require('@uppy/companion') 3 | import { storageConfig } from 'src/config/storage.config'; 4 | import { v4 as uuid } from 'uuid'; 5 | 6 | 7 | @Injectable() 8 | export class UppyService implements OnModuleInit { 9 | 10 | private logger = new Logger('UppyService'); 11 | 12 | private companionServer; 13 | 14 | onModuleInit() { 15 | this.initializeCompanionServer(); 16 | } 17 | 18 | async handleCompanion(req, res) { 19 | return this.companionServer.handle(req, res); 20 | } 21 | 22 | private initializeCompanionServer() { 23 | 24 | this.logger.verbose(`Initializing Companion Server.`); 25 | 26 | const tempDirectory = require('temp-dir') + '/'; 27 | const options = { 28 | providerOptions: { 29 | s3: { 30 | key: storageConfig.accessKeyId, 31 | secret: storageConfig.secretAccessKey, 32 | bucket: storageConfig.bucket, 33 | region: storageConfig.region, 34 | getKey: (req, filename, metadata) => { 35 | let extension: string = (filename) ? filename.split('.').pop() : null; 36 | extension = extension && extension.length === 3 ? extension : null; 37 | const prefix: string = uuid(); 38 | const s3filname = extension ? prefix + '.' + extension : prefix; 39 | return s3filname; 40 | }, 41 | awsClientOptions: { 42 | acl: 'private' 43 | }, 44 | redisOptions: { 45 | host: '127.0.0.1', 46 | port: '6379', 47 | } 48 | } 49 | }, 50 | server: { 51 | host: '127.0.0.1:3000', 52 | protocol: 'http', 53 | path: 'uppy-companion', 54 | }, 55 | secret: 'Cplh4ISm9QGTW739qw9m3w==', 56 | filePath: tempDirectory, 57 | debug: true, 58 | }; 59 | 60 | this.companionServer = companion.app(options); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-tus-server", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^7.0.0", 25 | "@nestjs/core": "^7.0.0", 26 | "@nestjs/platform-express": "^7.0.0", 27 | "@nestjs/serve-static": "^2.1.1", 28 | "@uppy/companion": "^2.0.0-alpha.5", 29 | "dotenv": "^8.2.0", 30 | "helmet": "^3.22.0", 31 | "reflect-metadata": "^0.1.13", 32 | "rimraf": "^3.0.2", 33 | "rxjs": "^6.5.4", 34 | "temp-dir": "^2.0.0", 35 | "tus-node-server": "^0.3.2" 36 | }, 37 | "devDependencies": { 38 | "@nestjs/cli": "^7.0.0", 39 | "@nestjs/schematics": "^7.0.0", 40 | "@nestjs/testing": "^7.0.0", 41 | "@types/express": "^4.17.3", 42 | "@types/jest": "25.1.4", 43 | "@types/node": "^13.9.1", 44 | "@types/supertest": "^2.0.8", 45 | "@typescript-eslint/eslint-plugin": "^2.23.0", 46 | "@typescript-eslint/parser": "^2.23.0", 47 | "eslint": "^6.8.0", 48 | "eslint-config-prettier": "^6.10.0", 49 | "eslint-plugin-import": "^2.20.1", 50 | "jest": "^25.1.0", 51 | "prettier": "^1.19.1", 52 | "supertest": "^4.0.2", 53 | "ts-jest": "25.2.1", 54 | "ts-loader": "^6.2.1", 55 | "ts-node": "^8.6.2", 56 | "tsconfig-paths": "^3.9.0", 57 | "typescript": "^3.7.4" 58 | }, 59 | "jest": { 60 | "moduleFileExtensions": [ 61 | "js", 62 | "json", 63 | "ts" 64 | ], 65 | "rootDir": "src", 66 | "testRegex": ".spec.ts$", 67 | "transform": { 68 | "^.+\\.(t|j)s$": "ts-jest" 69 | }, 70 | "coverageDirectory": "../coverage", 71 | "testEnvironment": "node" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/core/storage/tus.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, OnModuleInit, Logger } from '@nestjs/common'; 2 | import tus = require('tus-node-server'); 3 | import { storageConfig } from 'src/config/storage.config'; 4 | import { v4 as uuid } from 'uuid'; 5 | import { FileMetadata } from './models/file-metadata.model'; 6 | 7 | 8 | @Injectable() 9 | export class TusService implements OnModuleInit { 10 | 11 | private logger = new Logger('TusService'); 12 | 13 | private readonly tusServer = new tus.Server(); 14 | 15 | onModuleInit() { 16 | this.initializeTusServer(); 17 | } 18 | 19 | async handleTus(req, res) { 20 | return this.tusServer.handle(req, res); 21 | } 22 | 23 | private initializeTusServer() { 24 | this.logger.verbose(`Initializing Tus Server`); 25 | switch (storageConfig.storageDriver) { 26 | case 'local': 27 | this.tusServer.datastore = new tus.FileStore({ 28 | path: '/local-store', 29 | namingFunction: this.fileNameFromRequest, 30 | }); 31 | break; 32 | case 's3': 33 | this.tusServer.datastore = new tus.S3Store({ 34 | path: '/s3-store', 35 | namingFunction: this.fileNameFromRequest, 36 | bucket: storageConfig.bucket, 37 | accessKeyId: storageConfig.accessKeyId, 38 | secretAccessKey: storageConfig.secretAccessKey, 39 | region: storageConfig.region, 40 | partSize: 8 * 1024 * 1024, 41 | tmpDirPrefix: 'tus-s3-store', 42 | }); 43 | break; 44 | default: 45 | throw 'Invalid storage driver' + storageConfig.storageDriver; 46 | } 47 | this.tusServer.on(tus.EVENTS.EVENT_UPLOAD_COMPLETE, (event) => { 48 | this.logger.verbose(`Upload complete for file ${JSON.stringify(event.file)}`); 49 | }); 50 | } 51 | 52 | private fileNameFromRequest = (req) => { 53 | try { 54 | const metadata = this.getFileMetadata(req); 55 | 56 | const prefix: string = uuid(); 57 | 58 | const fileName = metadata.extension ? prefix + '.' + metadata.extension : prefix; 59 | 60 | return fileName; 61 | } catch (e) { 62 | this.logger.error(e); 63 | 64 | // rethrow error 65 | throw e; 66 | } 67 | } 68 | 69 | private getFileMetadata(req: any): FileMetadata { 70 | const uploadMeta: string = req.header('Upload-Metadata'); 71 | const metadata = new FileMetadata(); 72 | 73 | uploadMeta.split(',').map(item => { 74 | const tmp = item.split(' '); 75 | const key = tmp[0]; 76 | const value = Buffer.from(tmp[1], 'base64').toString('ascii');; 77 | metadata[`${key}`] = value; 78 | }); 79 | 80 | let extension: string = (metadata.name) ? metadata.name.split('.').pop() : null; 81 | extension = extension && extension.length === 3 ? extension : null; 82 | metadata.extension = extension; 83 | 84 | return metadata; 85 | } 86 | 87 | } 88 | --------------------------------------------------------------------------------