feat: file download

This commit is contained in:
Methapon2001 2023-11-22 12:58:45 +07:00
parent f5277a36f8
commit 4e3366654d
No known key found for this signature in database
GPG key ID: 849924FEF46BD132

View file

@ -278,4 +278,43 @@ export class FileController extends Controller {
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
@Get("/{fileName}")
@Tags("File")
@SuccessResponse(HttpStatusCode.OK)
public async downloadFile(
@Path() cabinetName: string,
@Path() drawerName: string,
@Path() folderName: string,
@Path() fileName: string,
) {
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
index: "ehr-api-client",
query: {
match: {
pathname: `${cabinetName}/${drawerName}/${folderName}/${fileName}`,
},
},
});
if (search && search.hits.hits.length === 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
}
const data = search.hits.hits[0]._source;
if (!data) {
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "Found data but no info.");
}
const { attachment, ...rest } = data;
return {
...rest,
download: await minioClient.presignedGetObject(
"ehr",
`${cabinetName}/${drawerName}/${folderName}/${fileName}`,
),
};
}
}