From 564f3427a49fb73324f31b6bf6c700c606320889 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 30 Aug 2022 21:09:43 +0300 Subject: [PATCH] pretty useless upload endpoint --- src/modules/api/api.controller.ts | 28 ++++++++++++++++++- src/modules/objects/upload/upload.service.ts | 8 ++++++ .../utility/services/paginate.service.ts | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/modules/api/api.controller.ts b/src/modules/api/api.controller.ts index 71aa14f..8d494b0 100644 --- a/src/modules/api/api.controller.ts +++ b/src/modules/api/api.controller.ts @@ -1,5 +1,14 @@ import { OAuth2AccessToken } from '@icynet/oauth2-provider'; -import { Controller, Get, UseGuards } from '@nestjs/common'; +import { + Controller, + Get, + NotFoundException, + Param, + StreamableFile, + UseGuards, +} from '@nestjs/common'; +import { join } from 'path'; +import { createReadStream } from 'fs'; import { Bearer } from 'src/decorators/bearer.decorator'; import { Scope } from 'src/decorators/scope.decorator'; import { CurrentUser } from 'src/decorators/user.decorator'; @@ -9,6 +18,7 @@ import { ConfigurationService } from '../config/config.service'; import { OAuth2ClientService } from '../objects/oauth2-client/oauth2-client.service'; import { User } from '../objects/user/user.entity'; import { FormUtilityService } from '../utility/services/form-utility.service'; +import { UploadService } from '../objects/upload/upload.service'; @Controller({ path: '/api', @@ -18,6 +28,7 @@ export class ApiController { private _config: ConfigurationService, private _oaClient: OAuth2ClientService, private _form: FormUtilityService, + private _upload: UploadService, ) {} @Get('/user') @@ -77,4 +88,19 @@ export class ApiController { 'verified', ]); } + + @Get('/upload/:file') + @UseGuards(OAuth2Guard) + async sendFile(@Param('file') fileName: string) { + const cleanFile = decodeURI(fileName).replace(/(\&(.*))/, ''); + const file = await this._upload.getByFile(cleanFile); + if (!file) { + throw new NotFoundException('File not found'); + } + + const path = join(this._upload.uploadPath, file.file); + const stream = createReadStream(path); + + return new StreamableFile(stream); + } } diff --git a/src/modules/objects/upload/upload.service.ts b/src/modules/objects/upload/upload.service.ts index fd37157..9e05051 100644 --- a/src/modules/objects/upload/upload.service.ts +++ b/src/modules/objects/upload/upload.service.ts @@ -30,6 +30,14 @@ export class UploadService { return upload; } + public async getById(id: number): Promise { + return this.uploadRepository.findOne({ where: { id } }); + } + + public async getByFile(file: string): Promise { + return this.uploadRepository.findOne({ where: { file } }); + } + public async checkImageAspect(file: Express.Multer.File): Promise { const opened = file.buffer || (await readFile(file.path)); return new Promise((resolve) => { diff --git a/src/modules/utility/services/paginate.service.ts b/src/modules/utility/services/paginate.service.ts index 5f5f2dc..6a527b3 100644 --- a/src/modules/utility/services/paginate.service.ts +++ b/src/modules/utility/services/paginate.service.ts @@ -9,7 +9,7 @@ export class PaginationService { public paginate(options: PageOptions, rowCount: number): PaginationOptions { const paginationOptions: PaginationOptions = { page: parseInt(options.page?.toString(), 10) || 1, - pageSize: parseInt(options.pageSize?.toString(), 10) || 50, + pageSize: parseInt(options.pageSize?.toString(), 10) || 16, rowCount, };