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

View File

@ -277,7 +277,11 @@ export default abstract class ChannelHandler<
*/ */
public async download(token: string, _req: Request) { public async download(token: string, _req: Request) {
try { 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); const attachment = plainToClass(Attachment, result);
return await this.attachmentService.download(attachment); return await this.attachmentService.download(attachment);
} catch (err) { } catch (err) {

View File

@ -27,7 +27,7 @@ export type AttachmentRef =
id: string | null; 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; 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). * 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'; import { Injectable, StreamableFile } from '@nestjs/common';
@ -48,7 +48,7 @@ export abstract class BaseStoragePlugin extends BasePlugin {
readAsBuffer?(attachment: Attachment): Promise<Buffer>; readAsBuffer?(attachment: Attachment): Promise<Buffer>;
store?( store?(
file: Buffer | Readable | Express.Multer.File, file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto, metadata: AttachmentMetadataDto,
rootDir?: string, rootDir?: string,
): Promise<Attachment>; ): Promise<Attachment>;