@@ -6,12 +6,15 @@ import {
66 Get ,
77 HttpStatus ,
88 Param ,
9+ ParseFilePipe ,
910 Patch ,
1011 Post ,
1112 Query ,
12- Res
13+ Res ,
14+ UploadedFile ,
15+ UseInterceptors
1316} from '@nestjs/common' ;
14- import { ApiOperation , ApiParam , ApiTags } from '@nestjs/swagger' ;
17+ import { ApiOperation , ApiParam , ApiTags , PartialType } from '@nestjs/swagger' ;
1518import { FilterOptions , FilterSchema , SearchFilterOptions , SearchFilterSchema } from '@the-software-compagny/nestjs_module_restools' ;
1619import { Response } from 'express' ;
1720import { Document , Types , isValidObjectId } from 'mongoose' ;
@@ -30,6 +33,14 @@ import { IdentityState } from './_enums/states.enum';
3033import { Identities } from './_schemas/identities.schema' ;
3134import { IdentitiesService } from './identities.service' ;
3235import { IdentitiesValidationService } from './validations/identities.validation.service' ;
36+ import { FileInterceptor } from '@nestjs/platform-express' ;
37+ import { ApiFileUploadDecorator } from '~/_common/decorators/api-file-upload.decorator' ;
38+ import { FilestorageCreateDto , FilestorageDto , FileUploadDto } from '~/core/filestorage/_dto/filestorage.dto' ;
39+ import { FilestorageService } from '~/core/filestorage/filestorage.service' ;
40+ import { FsType } from '~/core/filestorage/_enum/fs-type.enum' ;
41+ import { join } from 'node:path' ;
42+ import { omit } from 'radash' ;
43+ import { TransformersFilestorageService } from '~/core/filestorage/_services/transformers-filestorage.service' ;
3344// import { IdentitiesValidationFilter } from '~/_common/filters/identities-validation.filter';
3445
3546// @UseFilters (new IdentitiesValidationFilter())
@@ -39,6 +50,8 @@ export class IdentitiesController extends AbstractController {
3950 constructor (
4051 protected readonly _service : IdentitiesService ,
4152 protected readonly _validation : IdentitiesValidationService ,
53+ protected readonly filestorage : FilestorageService ,
54+ private readonly transformerService : TransformersFilestorageService ,
4255 ) {
4356 super ( ) ;
4457 }
@@ -282,4 +295,55 @@ export class IdentitiesController extends AbstractController {
282295 } ) ;
283296 }
284297 }
298+
299+ @Post ( 'upsert/photo' )
300+ @UseInterceptors ( FileInterceptor ( 'file' ) )
301+ @ApiFileUploadDecorator ( FileUploadDto , PartialType ( FilestorageCreateDto ) , FilestorageDto )
302+ public async upsertInetOrgPersonJpegPhoto (
303+ @Res ( ) res : Response ,
304+ @Body ( ) body : Partial < FilestorageCreateDto > ,
305+ @SearchFilterSchema ( ) searchFilterSchema : FilterSchema ,
306+ @UploadedFile ( new ParseFilePipe ( { fileIsRequired : false } ) ) file ?: Express . Multer . File ,
307+ ) : Promise < Response > {
308+ const identity = await this . _service . findOne < Identities > ( searchFilterSchema )
309+ const filter = {
310+ namespace : 'identities' ,
311+ path : join ( [
312+ identity . inetOrgPerson ?. employeeType ,
313+ identity . inetOrgPerson ?. employeeNumber ,
314+ 'jpegPhoto.jpg' ,
315+ ] . join ( '/' ) ) ,
316+ }
317+
318+ const data = await this . filestorage . upsertFile ( filter , {
319+ ...filter ,
320+ type : FsType . FILE ,
321+ file,
322+ ...omit ( body , [ 'namespace' , 'path' , 'type' , 'file' ] as any ) ,
323+ } )
324+
325+ return res . status ( HttpStatus . OK ) . json ( {
326+ statusCode : HttpStatus . OK ,
327+ data,
328+ } )
329+ }
330+
331+ @Get ( 'photo/raw' )
332+ @ApiReadResponseDecorator ( FilestorageDto )
333+ public async readPhotoRaw (
334+ @Res ( ) res : Response ,
335+ @SearchFilterSchema ( ) searchFilterSchema : FilterSchema ,
336+ @Query ( 'mime' ) mime : string = '' ,
337+ ) : Promise < void > {
338+ const identity = await this . _service . findOne < Identities > ( searchFilterSchema )
339+ const [ data , stream , parent ] = await this . filestorage . findOneWithRawData ( {
340+ namespace : 'identities' ,
341+ path : join ( [
342+ identity . inetOrgPerson ?. employeeType ,
343+ identity . inetOrgPerson ?. employeeNumber ,
344+ 'jpegPhoto.jpg' ,
345+ ] . join ( '/' ) ) ,
346+ } )
347+ await this . transformerService . transform ( mime , res , data , stream , parent )
348+ }
285349}
0 commit comments