fix: omit jwt extra attrs + add stream support for storing attachments

This commit is contained in:
Mohamed Marrouchi 2025-01-13 17:55:20 +01:00
parent f399416553
commit 716c48f3b4
4 changed files with 11 additions and 7 deletions

View File

@ -8,7 +8,7 @@
import fs, { createReadStream, promises as fsPromises } from 'fs';
import { join, resolve } from 'path';
import { Readable } from 'stream';
import { Readable, Stream } from 'stream';
import {
Injectable,
@ -202,7 +202,7 @@ export class AttachmentService extends BaseService<Attachment> {
* @returns A promise that resolves to an array of uploaded attachments.
*/
async store(
file: Buffer | Readable | Express.Multer.File,
file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto,
rootDir = config.parameters.uploadDir,
): Promise<Attachment | undefined> {
@ -219,7 +219,7 @@ export class AttachmentService extends BaseService<Attachment> {
if (Buffer.isBuffer(file)) {
await fsPromises.writeFile(filePath, file);
} else if (file instanceof Readable) {
} else if (file instanceof Readable || file instanceof Stream) {
await new Promise((resolve, reject) => {
const writeStream = fs.createWriteStream(filePath);
file.pipe(writeStream);

View File

@ -277,7 +277,11 @@ export default abstract class ChannelHandler<
*/
public async download(token: string, _req: Request) {
try {
const result = this.jwtService.verify(token, this.jwtSignOptions);
const {
exp: _exp,
iat: _iat,
...result
} = this.jwtService.verify(token, this.jwtSignOptions);
const attachment = plainToClass(Attachment, result);
return await this.attachmentService.download(attachment);
} catch (err) {

View File

@ -27,7 +27,7 @@ export type AttachmentRef =
id: string | null;
}
| {
/** To be used only for external URLs (plugins), for attachments use "id" instead */
/** @deprecated To be used only for external URLs (plugins), for stored attachments use "id" instead */
url: string;
};

View File

@ -6,7 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { Readable } from 'stream';
import { Readable, Stream } from 'stream';
import { Injectable, StreamableFile } from '@nestjs/common';
@ -48,7 +48,7 @@ export abstract class BaseStoragePlugin extends BasePlugin {
readAsBuffer?(attachment: Attachment): Promise<Buffer>;
store?(
file: Buffer | Readable | Express.Multer.File,
file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto,
rootDir?: string,
): Promise<Attachment>;