Skip to content

Commit 74e8e4f

Browse files
committed
Fix loading workflow from of edited webp
1 parent 23796d9 commit 74e8e4f

File tree

7 files changed

+68
-4
lines changed

7 files changed

+68
-4
lines changed

browser_tests/ComfyPage.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { Page, Locator } from '@playwright/test'
22
import { test as base } from '@playwright/test'
33
import dotenv from 'dotenv'
44
dotenv.config()
5+
import * as fs from 'fs';
6+
import * as path from 'path';
7+
58

69
interface Position {
710
x: number
@@ -202,6 +205,10 @@ export class ComfyPage {
202205
await this.resetView()
203206
}
204207

208+
public assetPath(fileName: string) {
209+
return `./browser_tests/assets/${fileName}`
210+
}
211+
205212
async setSetting(settingId: string, settingValue: any) {
206213
return await this.page.evaluate(
207214
async ({ id, value }) => {
@@ -238,7 +245,7 @@ export class ComfyPage {
238245

239246
async loadWorkflow(workflowName: string) {
240247
await this.workflowUploadInput.setInputFiles(
241-
`./browser_tests/assets/${workflowName}.json`
248+
this.assetPath(`${workflowName}.json`)
242249
)
243250
await this.nextFrame()
244251
}
@@ -300,6 +307,49 @@ export class ComfyPage {
300307
await this.nextFrame()
301308
}
302309

310+
async dragAndDropFile(fileName: string) {
311+
const filePath = this.assetPath(fileName)
312+
313+
// Read the file content
314+
const buffer = fs.readFileSync(filePath);
315+
316+
// Get file type
317+
const getFileType = (fileName: string) => {
318+
if (fileName.endsWith('.png')) return 'image/png'
319+
if (fileName.endsWith('.webp')) return 'image/webp'
320+
if (fileName.endsWith('.json')) return 'application/json'
321+
return 'application/octet-stream'
322+
}
323+
324+
const fileType = getFileType(fileName)
325+
326+
await this.page.evaluate(async ({ buffer, fileName, fileType }) => {
327+
const file = new File([new Uint8Array(buffer)], fileName, { type: fileType });
328+
const dataTransfer = new DataTransfer();
329+
dataTransfer.items.add(file);
330+
331+
const dropEvent = new DragEvent('drop', {
332+
bubbles: true,
333+
cancelable: true,
334+
dataTransfer
335+
});
336+
337+
Object.defineProperty(dropEvent, 'preventDefault', {
338+
value: () => {},
339+
writable: false
340+
});
341+
342+
Object.defineProperty(dropEvent, 'stopPropagation', {
343+
value: () => {},
344+
writable: false
345+
});
346+
347+
document.dispatchEvent(dropEvent);
348+
}, { buffer: [...new Uint8Array(buffer)], fileName, fileType });
349+
350+
await this.nextFrame();
351+
}
352+
303353
async dragNode2() {
304354
await this.dragAndDrop({ x: 622, y: 400 }, { x: 622, y: 300 })
305355
await this.nextFrame()
200 KB
Binary file not shown.

browser_tests/assets/no_workflow.webp

195 KB
Binary file not shown.

browser_tests/assets/workflow.webp

200 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect } from '@playwright/test'
2+
import { comfyPageFixture as test } from './ComfyPage'
3+
4+
test.describe('Load Workflow in Media', () => {
5+
['workflow.webp', 'edited_workflow.webp', 'no_workflow.webp'].forEach(async (fileName) => {
6+
test(`Load workflow in ${fileName}`, async ({ comfyPage }) => {
7+
await comfyPage.dragAndDropFile(fileName)
8+
await expect(comfyPage.canvas).toHaveScreenshot(`${fileName}.png`)
9+
})
10+
})
11+
})

src/scripts/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ export class ComfyApp {
999999
#addDropHandler() {
10001000
// Get prompt from dropped PNG or json
10011001
document.addEventListener('drop', async (event) => {
1002+
console.log("Received drop event")
10021003
event.preventDefault()
10031004
event.stopPropagation()
10041005

src/scripts/pnginfo.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ export function getWebpMetadata(file) {
113113
webp.slice(offset + 8, offset + 8 + chunk_length)
114114
)
115115
for (var key in data) {
116-
var value = data[key] as string
117-
let index = value.indexOf(':')
118-
txt_chunks[value.slice(0, index)] = value.slice(index + 1)
116+
const value = data[key] as string
117+
if (typeof value === 'string') {
118+
const index = value.indexOf(':')
119+
txt_chunks[value.slice(0, index)] = value.slice(index + 1)
120+
}
119121
}
120122
break
121123
}

0 commit comments

Comments
 (0)