( rootDir: string, channelId: string, filename: string, buffer: Buffer, mimeType?: string, )
| 34 | * Save a buffer as an attachment file and return a ChannelAttachment descriptor. |
| 35 | */ |
| 36 | export async function saveAttachment( |
| 37 | rootDir: string, |
| 38 | channelId: string, |
| 39 | filename: string, |
| 40 | buffer: Buffer, |
| 41 | mimeType?: string, |
| 42 | ): Promise<ChannelAttachment> { |
| 43 | const dir = getAttachmentDir(rootDir, channelId); |
| 44 | fs.mkdirSync(dir, { recursive: true }); |
| 45 | |
| 46 | const ts = Date.now(); |
| 47 | const safeName = sanitizeFilename(filename); |
| 48 | const storedName = `${ts}-${safeName}`; |
| 49 | const fullPath = path.join(dir, storedName); |
| 50 | |
| 51 | fs.writeFileSync(fullPath, buffer); |
| 52 | |
| 53 | return { |
| 54 | filename, |
| 55 | localPath: fullPath, |
| 56 | mimeType, |
| 57 | size: buffer.byteLength, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Download a file from a URL and save it as an attachment. |
no test coverage detected