From 3feeecc740aca3e2bb7401e61092b840ccb07be8 Mon Sep 17 00:00:00 2001 From: Rvage Date: Thu, 4 Dec 2025 20:14:38 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20workflow=20loading=20from=20PNG=20images?= =?UTF-8?q?=20with=20both=20workflow=20and=20parameter=E2=80=A6=20(#7154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …s metadata - Reorder handleFile() to check workflow before parameters - Add validation to prevent JSON parse errors from crashing imports - Fix loadGraphData() to use explicit type validation instead of falsy check - Ensures ComfyUI-generated PNGs with both metadata types load the workflow, not parameters Fixes issue where large workflows (e.g., 634 nodes) were replaced with basic A1111 format when importing PNG files. ## Summary Fixed workflow loading from PNG images to prioritize workflow metadata over parameters, preventing large workflows from being replaced with basic A1111 format. ## Changes - **What**: Reordered `handleFile()` to check workflow before parameters, added JSON parse error handling and validation, fixed `loadGraphData()` to use explicit type checking instead of falsy check - **Dependencies**: None ## Review Focus The key issue was in `handleFile()` where parameters were checked before workflow, causing ComfyUI-generated PNGs (which contain both workflow and parameters metadata) to incorrectly import as A1111 format. The fix ensures: 1. Workflow is always checked first and validated properly 2. Parameters are only used as a fallback when no workflow exists 3. Invalid/malformed workflow data doesn't crash the import process Additionally, `loadGraphData()` was using a falsy check (`if (!graphData)`) which could incorrectly replace valid but falsy values. Now uses explicit type validation. Tested with real-world PNG containing 634-node workflow (780KB) + parameters (1KB) - now correctly loads the workflow instead of discarding it. ## Screenshots (if applicable) N/A - Backend logic fix, no UI changes Fixes #6633 ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7154-Fix-workflow-loading-from-PNG-images-with-both-workflow-and-parameter-2bf6d73d365081ecb7a6c4bf6b6ccd51) by [Unito](https://www.unito.io) --------- Co-authored-by: Alexander Brown Co-authored-by: Alexander Brown --- src/scripts/app.ts | 47 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 728e35d05..051b76048 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1057,7 +1057,13 @@ export class ComfyApp { } let reset_invalid_values = false - if (!graphData) { + // Use explicit validation instead of falsy check to avoid replacing + // valid but falsy values (empty objects, 0, false, etc.) + if ( + !graphData || + typeof graphData !== 'object' || + Array.isArray(graphData) + ) { graphData = defaultGraph reset_invalid_values = true } @@ -1432,6 +1438,36 @@ export class ComfyApp { this.loadTemplateData({ templates }) } + // Check workflow first - it should take priority over parameters + // when both are present (e.g., in ComfyUI-generated PNGs) + if (workflow) { + let workflowObj: ComfyWorkflowJSON | undefined = undefined + try { + workflowObj = + typeof workflow === 'string' ? JSON.parse(workflow) : workflow + + // Only load workflow if parsing succeeded AND validation passed + if ( + workflowObj && + typeof workflowObj === 'object' && + !Array.isArray(workflowObj) + ) { + await this.loadGraphData(workflowObj, true, true, fileName, { + openSource + }) + return + } else { + console.error('Invalid workflow structure, trying parameters fallback') + this.showErrorOnFileLoad(file) + } + } catch (err) { + console.error('Failed to parse workflow:', err) + this.showErrorOnFileLoad(file) + // Fall through to check parameters as fallback + } + } + + // Use parameters as fallback when no workflow exists if (parameters) { // Note: Not putting this in `importA1111` as it is mostly not used // by external callers, and `importA1111` has no access to `app`. @@ -1444,15 +1480,6 @@ export class ComfyApp { return } - if (workflow) { - const workflowObj = - typeof workflow === 'string' ? JSON.parse(workflow) : workflow - await this.loadGraphData(workflowObj, true, true, fileName, { - openSource - }) - return - } - if (prompt) { const promptObj = typeof prompt === 'string' ? JSON.parse(prompt) : prompt this.loadApiJson(promptObj, fileName)