Skip to content

Fix loading workflow from of edited webp #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2024
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
56 changes: 55 additions & 1 deletion browser_tests/ComfyPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Page, Locator } from '@playwright/test'
import { test as base } from '@playwright/test'
import dotenv from 'dotenv'
dotenv.config()
import * as fs from 'fs'
import * as path from 'path'

interface Position {
x: number
Expand Down Expand Up @@ -202,6 +204,10 @@ export class ComfyPage {
await this.resetView()
}

public assetPath(fileName: string) {
return `./browser_tests/assets/${fileName}`
}

async setSetting(settingId: string, settingValue: any) {
return await this.page.evaluate(
async ({ id, value }) => {
Expand Down Expand Up @@ -238,7 +244,7 @@ export class ComfyPage {

async loadWorkflow(workflowName: string) {
await this.workflowUploadInput.setInputFiles(
`./browser_tests/assets/${workflowName}.json`
this.assetPath(`${workflowName}.json`)
)
await this.nextFrame()
}
Expand Down Expand Up @@ -300,6 +306,54 @@ export class ComfyPage {
await this.nextFrame()
}

async dragAndDropFile(fileName: string) {
const filePath = this.assetPath(fileName)

// Read the file content
const buffer = fs.readFileSync(filePath)

// Get file type
const getFileType = (fileName: string) => {
if (fileName.endsWith('.png')) return 'image/png'
if (fileName.endsWith('.webp')) return 'image/webp'
if (fileName.endsWith('.json')) return 'application/json'
return 'application/octet-stream'
}

const fileType = getFileType(fileName)

await this.page.evaluate(
async ({ buffer, fileName, fileType }) => {
const file = new File([new Uint8Array(buffer)], fileName, {
type: fileType
})
const dataTransfer = new DataTransfer()
dataTransfer.items.add(file)

const dropEvent = new DragEvent('drop', {
bubbles: true,
cancelable: true,
dataTransfer
})

Object.defineProperty(dropEvent, 'preventDefault', {
value: () => {},
writable: false
})

Object.defineProperty(dropEvent, 'stopPropagation', {
value: () => {},
writable: false
})

document.dispatchEvent(dropEvent)
},
{ buffer: [...new Uint8Array(buffer)], fileName, fileType }
)

await this.nextFrame()
}

async dragNode2() {
await this.dragAndDrop({ x: 622, y: 400 }, { x: 622, y: 300 })
await this.nextFrame()
Expand Down
Binary file added browser_tests/assets/edited_workflow.webp
Binary file not shown.
Binary file added browser_tests/assets/no_workflow.webp
Binary file not shown.
Binary file added browser_tests/assets/workflow.webp
Binary file not shown.
13 changes: 13 additions & 0 deletions browser_tests/loadWorkflowInMedia.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from '@playwright/test'
import { comfyPageFixture as test } from './ComfyPage'

test.describe('Load Workflow in Media', () => {
;['workflow.webp', 'edited_workflow.webp', 'no_workflow.webp'].forEach(
async (fileName) => {
test(`Load workflow in ${fileName}`, async ({ comfyPage }) => {
await comfyPage.dragAndDropFile(fileName)
await expect(comfyPage.canvas).toHaveScreenshot(`${fileName}.png`)
})
}
)
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions src/scripts/pnginfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ export function getWebpMetadata(file) {
webp.slice(offset + 8, offset + 8 + chunk_length)
)
for (var key in data) {
var value = data[key] as string
let index = value.indexOf(':')
txt_chunks[value.slice(0, index)] = value.slice(index + 1)
const value = data[key] as string
if (typeof value === 'string') {
const index = value.indexOf(':')
txt_chunks[value.slice(0, index)] = value.slice(index + 1)
}
}
break
}
Expand Down