pretty useless upload endpoint

This commit is contained in:
Evert Prants 2022-08-30 21:09:43 +03:00
parent 7f523c9606
commit 564f3427a4
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,14 @@
import { OAuth2AccessToken } from '@icynet/oauth2-provider'; 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 { Bearer } from 'src/decorators/bearer.decorator';
import { Scope } from 'src/decorators/scope.decorator'; import { Scope } from 'src/decorators/scope.decorator';
import { CurrentUser } from 'src/decorators/user.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 { OAuth2ClientService } from '../objects/oauth2-client/oauth2-client.service';
import { User } from '../objects/user/user.entity'; import { User } from '../objects/user/user.entity';
import { FormUtilityService } from '../utility/services/form-utility.service'; import { FormUtilityService } from '../utility/services/form-utility.service';
import { UploadService } from '../objects/upload/upload.service';
@Controller({ @Controller({
path: '/api', path: '/api',
@ -18,6 +28,7 @@ export class ApiController {
private _config: ConfigurationService, private _config: ConfigurationService,
private _oaClient: OAuth2ClientService, private _oaClient: OAuth2ClientService,
private _form: FormUtilityService, private _form: FormUtilityService,
private _upload: UploadService,
) {} ) {}
@Get('/user') @Get('/user')
@ -77,4 +88,19 @@ export class ApiController {
'verified', '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);
}
} }

View File

@ -30,6 +30,14 @@ export class UploadService {
return upload; return upload;
} }
public async getById(id: number): Promise<Upload> {
return this.uploadRepository.findOne({ where: { id } });
}
public async getByFile(file: string): Promise<Upload> {
return this.uploadRepository.findOne({ where: { file } });
}
public async checkImageAspect(file: Express.Multer.File): Promise<boolean> { public async checkImageAspect(file: Express.Multer.File): Promise<boolean> {
const opened = file.buffer || (await readFile(file.path)); const opened = file.buffer || (await readFile(file.path));
return new Promise((resolve) => { return new Promise((resolve) => {

View File

@ -9,7 +9,7 @@ export class PaginationService {
public paginate(options: PageOptions, rowCount: number): PaginationOptions { public paginate(options: PageOptions, rowCount: number): PaginationOptions {
const paginationOptions: PaginationOptions = { const paginationOptions: PaginationOptions = {
page: parseInt(options.page?.toString(), 10) || 1, page: parseInt(options.page?.toString(), 10) || 1,
pageSize: parseInt(options.pageSize?.toString(), 10) || 50, pageSize: parseInt(options.pageSize?.toString(), 10) || 16,
rowCount, rowCount,
}; };