Skip to content

Commit 023299c

Browse files
huchenleigithub-actions
andauthored
Fix loading workflow from of edited webp (#764)
* Fix loading workflow from of edited webp * Update test expectations [skip ci] --------- Co-authored-by: github-actions <github-actions@github.com>
1 parent 23796d9 commit 023299c

File tree

9 files changed

+73
-4
lines changed

9 files changed

+73
-4
lines changed

browser_tests/ComfyPage.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ 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'
57

68
interface Position {
79
x: number
@@ -202,6 +204,10 @@ export class ComfyPage {
202204
await this.resetView()
203205
}
204206

207+
public assetPath(fileName: string) {
208+
return `./browser_tests/assets/${fileName}`
209+
}
210+
205211
async setSetting(settingId: string, settingValue: any) {
206212
return await this.page.evaluate(
207213
async ({ id, value }) => {
@@ -238,7 +244,7 @@ export class ComfyPage {
238244

239245
async loadWorkflow(workflowName: string) {
240246
await this.workflowUploadInput.setInputFiles(
241-
`./browser_tests/assets/${workflowName}.json`
247+
this.assetPath(`${workflowName}.json`)
242248
)
243249
await this.nextFrame()
244250
}
@@ -300,6 +306,54 @@ export class ComfyPage {
300306
await this.nextFrame()
301307
}
302308

309+
async dragAndDropFile(fileName: string) {
310+
const filePath = this.assetPath(fileName)
311+
312+
// Read the file content
313+
const buffer = fs.readFileSync(filePath)
314+
315+
// Get file type
316+
const getFileType = (fileName: string) => {
317+
if (fileName.endsWith('.png')) return 'image/png'
318+
if (fileName.endsWith('.webp')) return 'image/webp'
319+
if (fileName.endsWith('.json')) return 'application/json'
320+
return 'application/octet-stream'
321+
}
322+
323+
const fileType = getFileType(fileName)
324+
325+
await this.page.evaluate(
326+
async ({ buffer, fileName, fileType }) => {
327+
const file = new File([new Uint8Array(buffer)], fileName, {
328+
type: fileType
329+
})
330+
const dataTransfer = new DataTransfer()
331+
dataTransfer.items.add(file)
332+
333+
const dropEvent = new DragEvent('drop', {
334+
bubbles: true,
335+
cancelable: true,
336+
dataTransfer
337+
})
338+
339+
Object.defineProperty(dropEvent, 'preventDefault', {
340+
value: () => {},
341+
writable: false
342+
})
343+
344+
Object.defineProperty(dropEvent, 'stopPropagation', {
345+
value: () => {},
346+
writable: false
347+
})
348+
349+
document.dispatchEvent(dropEvent)
350+
},
351+
{ buffer: [...new Uint8Array(buffer)], fileName, fileType }
352+
)
353+
354+
await this.nextFrame()
355+
}
356+
303357
async dragNode2() {
304358
await this.dragAndDrop({ x: 622, y: 400 }, { x: 622, y: 300 })
305359
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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(
6+
async (fileName) => {
7+
test(`Load workflow in ${fileName}`, async ({ comfyPage }) => {
8+
await comfyPage.dragAndDropFile(fileName)
9+
await expect(comfyPage.canvas).toHaveScreenshot(`${fileName}.png`)
10+
})
11+
}
12+
)
13+
})
Loading
Loading
Loading

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)