monorepo-microservice-rbac/apps/services/dmp/gateway/src/dicom/dicom.controller.ts
2023-09-05 15:07:15 +08:00

29 lines
895 B
TypeScript

import { Body, Controller, Get, Inject, Param, Post, Res } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { Response } from 'express';
import { firstValueFrom } from 'rxjs';
import axios from 'axios';
@Controller('dicom')
export class DicomController {
constructor(@Inject('Client') private client: ClientProxy) {}
@Post('download')
async downloadDicom(@Body() body, @Res() res: Response) {
const { ID, Type } = body;
const url = await firstValueFrom(
this.client.send({ cmd: 'dicom.archive.url' }, { ID, Type }),
);
try {
const { data: dataStream } = await axios.get(url, {
responseType: 'stream',
});
res.setHeader('Content-Name', `${ID}.zip`);
res.setHeader('Content-Type', 'application/zip');
dataStream.pipe(res);
} catch (error) {
return { code: 1, msg: error };
}
}
}