Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"pdf-lib": "^1.17.1",
"pdf-parse": "^2.0.0",
"pino": "^10.3.1",
"pptxgenjs": "^3.12.0",
"pptxgenjs": "^4.0.1",
"puppeteer": "^25.8.0",
"supports-hyperlinks": "^4.5.0",
"tiktoken": "^1.0.22",
Expand Down
24 changes: 10 additions & 14 deletions src/tools/fileCreate/pptx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { z } from "zod";
import { tool } from "@langchain/core/tools";
import PptxGenJS from "pptxgenjs";
import { resolve, dirname } from "node:path";
import { mkdir, writeFile } from "node:fs/promises";
import { readFile, mkdir, writeFile } from "node:fs/promises";

// ---------------------------------------------------------------------------
// Error class
Expand Down Expand Up @@ -137,9 +137,9 @@ export const pptxGenerateSchema = z.object({
/**
* Validate that an image file has a supported extension and valid content.
* @param {string} imagePath - Absolute path to the image file
* @returns {{ valid: boolean, error?: string }}
* @returns {Promise<{ valid: boolean, error?: string }>}
*/
export function validateImagePath(imagePath) {
export async function validateImagePath(imagePath) {
const ext = imagePath.split(".").pop()?.toLowerCase();
if (!ext || !SUPPORTED_IMAGE_EXTENSIONS.has(ext)) {
return {
Expand All @@ -160,14 +160,10 @@ export function validateImagePath(imagePath) {
if (!expected) return { valid: true };

try {
const buf = readFileSync(imagePath, {
encoding: "buffer",
length: expected.length,
position: 0,
});
const fd = await readFile(imagePath);

for (let i = 0; i < expected.length; i++) {
if (buf[i] !== expected[i]) {
if (fd[i] !== expected[i]) {
return {
valid: false,
error: `File ${imagePath} is not a valid ${ext} image (invalid magic bytes)`,
Expand Down Expand Up @@ -207,11 +203,11 @@ export function validateOutputPath(outputPath, allowedDir) {
/**
* Validate that a template file is a valid PPTX (ZIP structure check).
* @param {string} templatePath - Path to the template file
* @returns {boolean}
* @returns {Promise<boolean>}
*/
export function validateTemplatePath(templatePath) {
export async function validateTemplatePath(templatePath) {
try {
const buf = readFileSync(templatePath, { encoding: "buffer", length: 4, position: 0 });
const buf = await readFile(templatePath);

// PPTX files are ZIP archives starting with PK\x03\x04
if (buf[0] !== 0x50 || buf[1] !== 0x4b) {
Expand Down Expand Up @@ -505,14 +501,14 @@ export async function createPptx(input) {
}

// Validate template if provided
if (templatePath && !validateTemplatePath(templatePath)) {
if (templatePath && !(await validateTemplatePath(templatePath))) {
return JSON.stringify({ ok: false, error: `${templatePath} is not a valid PPTX file` });
}

// Validate all image paths
for (const slide of slides) {
for (const img of slide.images || []) {
const imgValidation = validateImagePath(img.path);
const imgValidation = await validateImagePath(img.path);
if (!imgValidation.valid) {
return JSON.stringify({
ok: false,
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/pptx/not-a-pptx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not a pptx
Binary file added tests/fixtures/pptx/template.pptx
Binary file not shown.
1 change: 1 addition & 0 deletions tests/fixtures/pptx/test.bmp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BM
1 change: 1 addition & 0 deletions tests/fixtures/pptx/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/fixtures/pptx/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/fixtures/pptx/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading