Delete imported Comfy stills from disk instead of looking for them in the video folder.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -530,34 +530,73 @@ function sharedRoots() {
|
|||||||
const local = process.env.LOCALAPPDATA || ''
|
const local = process.env.LOCALAPPDATA || ''
|
||||||
return {
|
return {
|
||||||
input: process.env.COMFY_INPUT_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'input'),
|
input: process.env.COMFY_INPUT_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'input'),
|
||||||
output: process.env.COMFY_OUTPUT_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'output')
|
output: process.env.COMFY_OUTPUT_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'output'),
|
||||||
|
temp: process.env.COMFY_TEMP_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'temp')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function inputRoots() {
|
function installLeafRoots(leaf) {
|
||||||
const local = process.env.LOCALAPPDATA || ''
|
const local = process.env.LOCALAPPDATA || ''
|
||||||
const roots = [
|
const roots = []
|
||||||
process.env.COMFY_INPUT_DIR || join(local, 'Comfy-Desktop', 'ComfyUI-Shared', 'input')
|
|
||||||
]
|
|
||||||
const cwd = process.env.COMFY_LAUNCH_CWD
|
|
||||||
if (cwd) roots.push(join(cwd, 'input'))
|
|
||||||
const installs = join(local, 'Comfy-Desktop', 'ComfyUI-Installs')
|
const installs = join(local, 'Comfy-Desktop', 'ComfyUI-Installs')
|
||||||
try {
|
try {
|
||||||
for (const dir of readdirSync(installs, { withFileTypes: true })) {
|
for (const dir of readdirSync(installs, { withFileTypes: true })) {
|
||||||
if (!dir.isDirectory()) continue
|
if (!dir.isDirectory()) continue
|
||||||
roots.push(join(installs, dir.name, 'ComfyUI', 'input'))
|
roots.push(join(installs, dir.name, 'ComfyUI', leaf))
|
||||||
}
|
}
|
||||||
} catch { /* ignore */ }
|
} catch { /* ignore */ }
|
||||||
|
return roots
|
||||||
|
}
|
||||||
|
|
||||||
|
function inputRoots() {
|
||||||
|
const roots = [sharedRoots().input]
|
||||||
|
const cwd = process.env.COMFY_LAUNCH_CWD
|
||||||
|
if (cwd) roots.push(join(cwd, 'input'))
|
||||||
|
roots.push(...installLeafRoots('input'))
|
||||||
return [...new Set(roots)]
|
return [...new Set(roots)]
|
||||||
}
|
}
|
||||||
|
|
||||||
function outputRoots() {
|
function outputRoots() {
|
||||||
const roots = [sharedRoots().output]
|
const roots = [sharedRoots().output]
|
||||||
const cwd = process.env.COMFY_LAUNCH_CWD
|
const cwd = process.env.COMFY_LAUNCH_CWD
|
||||||
if (cwd) roots.push(join(cwd, 'output'))
|
if (cwd) {
|
||||||
|
roots.push(join(cwd, 'output'))
|
||||||
|
roots.push(join(cwd, 'ComfyUI', 'output'))
|
||||||
|
}
|
||||||
|
roots.push(...installLeafRoots('output'))
|
||||||
return [...new Set(roots)]
|
return [...new Set(roots)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tempRoots() {
|
||||||
|
const roots = [sharedRoots().temp]
|
||||||
|
const cwd = process.env.COMFY_LAUNCH_CWD
|
||||||
|
if (cwd) {
|
||||||
|
roots.push(join(cwd, 'temp'))
|
||||||
|
roots.push(join(cwd, 'ComfyUI', 'temp'))
|
||||||
|
}
|
||||||
|
roots.push(...installLeafRoots('temp'))
|
||||||
|
return [...new Set(roots)]
|
||||||
|
}
|
||||||
|
|
||||||
|
function rootsForType(type) {
|
||||||
|
if (type === 'input') return inputRoots()
|
||||||
|
if (type === 'temp') return [...tempRoots(), ...outputRoots()]
|
||||||
|
return [...outputRoots(), ...tempRoots()]
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeNamedFile(roots, filename, subfolders) {
|
||||||
|
const deleted = []
|
||||||
|
const subs = [...new Set((subfolders || []).map(item => String(item || '')))]
|
||||||
|
for (const root of roots) {
|
||||||
|
if (!existsSync(root)) continue
|
||||||
|
for (const sub of subs) {
|
||||||
|
const path = safeFile(root, sub, filename)
|
||||||
|
if (removeFile(path)) deleted.push(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deleted
|
||||||
|
}
|
||||||
|
|
||||||
function safeFile(root, subfolder, filename) {
|
function safeFile(root, subfolder, filename) {
|
||||||
const name = basename(String(filename || ''))
|
const name = basename(String(filename || ''))
|
||||||
if (!name || name === '.' || name === '..') return null
|
if (!name || name === '.' || name === '..') return null
|
||||||
@@ -597,43 +636,37 @@ function purgeDesktopFiles(body) {
|
|||||||
].map(name => String(name || '').trim()).filter(Boolean)
|
].map(name => String(name || '').trim()).filter(Boolean)
|
||||||
const imageSub = String(body?.imageSubfolder || '')
|
const imageSub = String(body?.imageSubfolder || '')
|
||||||
for (const imageName of names) {
|
for (const imageName of names) {
|
||||||
for (const root of inputRoots()) {
|
deleted.push(...removeNamedFile(inputRoots(), imageName, [imageSub, '']))
|
||||||
if (!existsSync(root)) continue
|
|
||||||
const nested = safeFile(root, imageSub, imageName)
|
|
||||||
if (removeFile(nested)) deleted.push(nested)
|
|
||||||
if (imageSub) {
|
|
||||||
const flat = safeFile(root, '', imageName)
|
|
||||||
if (removeFile(flat)) deleted.push(flat)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const video = body?.video || null
|
const video = body?.video || null
|
||||||
const videoName = String(video?.filename || '')
|
const videoName = String(video?.filename || '')
|
||||||
if (videoName) {
|
if (videoName) {
|
||||||
for (const root of outputRoots()) {
|
const type = String(video?.type || 'output')
|
||||||
const nested = safeFile(root, video.subfolder || 'video', videoName)
|
const imageOut = /\.(png|jpe?g|webp|gif)$/i.test(videoName)
|
||||||
if (removeFile(nested)) deleted.push(nested)
|
deleted.push(...removeNamedFile(
|
||||||
const flat = safeFile(root, '', videoName)
|
rootsForType(type),
|
||||||
if (removeFile(flat)) deleted.push(flat)
|
videoName,
|
||||||
}
|
[video.subfolder, imageOut ? '' : 'video', '']
|
||||||
|
))
|
||||||
}
|
}
|
||||||
const output = body?.output || null
|
const output = body?.output || null
|
||||||
const outputName = String(output?.filename || '')
|
const outputName = String(output?.filename || '')
|
||||||
if (outputName) {
|
if (outputName) {
|
||||||
const type = String(output?.type || 'output')
|
deleted.push(...removeNamedFile(
|
||||||
const sub = String(output?.subfolder || '')
|
rootsForType(String(output?.type || 'output')),
|
||||||
const rootsForType = type === 'input' ? inputRoots() : outputRoots()
|
outputName,
|
||||||
for (const root of rootsForType) {
|
[output.subfolder, '']
|
||||||
if (!existsSync(root)) continue
|
))
|
||||||
const nested = safeFile(root, sub, outputName)
|
|
||||||
if (removeFile(nested)) deleted.push(nested)
|
|
||||||
if (sub) {
|
|
||||||
const flat = safeFile(root, '', outputName)
|
|
||||||
if (removeFile(flat)) deleted.push(flat)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.log(JSON.stringify({ src: 'comfy-host-agent', event: 'purge', deleted: deleted.length, files: deleted.map((path) => basename(path)), imageNames: names }))
|
console.log(JSON.stringify({
|
||||||
|
src: 'comfy-host-agent',
|
||||||
|
event: 'purge',
|
||||||
|
deleted: deleted.length,
|
||||||
|
files: deleted.map((path) => basename(path)),
|
||||||
|
imageNames: names,
|
||||||
|
video: videoName || null,
|
||||||
|
output: outputName || null
|
||||||
|
}))
|
||||||
return { ok: true, deleted }
|
return { ok: true, deleted }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -451,7 +451,8 @@ async function purgeOnDesktop(opts: {
|
|||||||
imageName: opts.imageName || '',
|
imageName: opts.imageName || '',
|
||||||
imageSubfolder: opts.imageSubfolder || '',
|
imageSubfolder: opts.imageSubfolder || '',
|
||||||
imageNames: (opts.extraImageNames || []).filter(Boolean),
|
imageNames: (opts.extraImageNames || []).filter(Boolean),
|
||||||
video: opts.video || null
|
video: opts.video || null,
|
||||||
|
output: opts.video && /\.(png|jpe?g|webp|gif)$/i.test(opts.video.filename) ? opts.video : null
|
||||||
}),
|
}),
|
||||||
signal: AbortSignal.timeout(8000)
|
signal: AbortSignal.timeout(8000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createJob, emitJob, type Job } from '~/server/utils/jobs'
|
import { createJob, emitJob, type Job } from '~/server/utils/jobs'
|
||||||
import { ensureComfyReady } from '~/server/utils/comfyLifecycle'
|
import { ensureComfyReady } from '~/server/utils/comfyLifecycle'
|
||||||
import { assertImageScaleToTotalPixelsNode, getComfyHost, uploadImage, queuePrompt, purgeComfyArtifacts } from '~/server/utils/comfy'
|
import { assertImageScaleToTotalPixelsNode, getComfyHost, uploadImage, queuePrompt } from '~/server/utils/comfy'
|
||||||
import { withImageComfyHost, waitForImageEdit, downloadEditedImage } from '~/server/utils/imageComfy'
|
import { withImageComfyHost, waitForImageEdit, downloadEditedImage, purgeImageComfyArtifacts } from '~/server/utils/imageComfy'
|
||||||
import { buildEditWorkflow } from '~/server/utils/imageWorkflow'
|
import { buildEditWorkflow } from '~/server/utils/imageWorkflow'
|
||||||
import { ensureComfyLoraNames } from '~/server/utils/loras'
|
import { ensureComfyLoraNames } from '~/server/utils/loras'
|
||||||
import { imageDimensions } from '~/server/utils/resolution'
|
import { imageDimensions } from '~/server/utils/resolution'
|
||||||
@@ -188,11 +188,11 @@ export async function runEdit(job: Job, params: EditRunParams) {
|
|||||||
})
|
})
|
||||||
job.stillId = still?.id
|
job.stillId = still?.id
|
||||||
if (!batch) parentStillId = still?.id
|
if (!batch) parentStillId = still?.id
|
||||||
await purgeComfyArtifacts({
|
await purgeImageComfyArtifacts({
|
||||||
video: { filename: output.filename, subfolder: output.subfolder, type: output.type },
|
output: { filename: output.filename, subfolder: output.subfolder, type: output.type },
|
||||||
imageName: uploaded.name,
|
inputName: uploaded.name,
|
||||||
imageSubfolder: uploaded.subfolder,
|
inputSubfolder: uploaded.subfolder,
|
||||||
extraImageNames: uploadedRef?.name ? [uploadedRef.name] : [],
|
extraInputNames: uploadedRef?.name ? [uploadedRef.name] : [],
|
||||||
promptId: job.promptId
|
promptId: job.promptId
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createJob, emitJob, type Job } from '~/server/utils/jobs'
|
import { createJob, emitJob, type Job } from '~/server/utils/jobs'
|
||||||
import { ensureComfyReady } from '~/server/utils/comfyLifecycle'
|
import { ensureComfyReady } from '~/server/utils/comfyLifecycle'
|
||||||
import { assertImageScaleToTotalPixelsNode, getComfyHost, uploadImage, queuePrompt, purgeComfyArtifacts } from '~/server/utils/comfy'
|
import { assertImageScaleToTotalPixelsNode, getComfyHost, uploadImage, queuePrompt } from '~/server/utils/comfy'
|
||||||
import { withImageComfyHost, waitForImageEdit, downloadEditedImage } from '~/server/utils/imageComfy'
|
import { withImageComfyHost, waitForImageEdit, downloadEditedImage, purgeImageComfyArtifacts } from '~/server/utils/imageComfy'
|
||||||
import { buildImageV2Workflow, IMAGE_V2_NODE_LABELS, resolveKreaGenerateAssets } from '~/server/utils/imageWorkflowV2'
|
import { buildImageV2Workflow, IMAGE_V2_NODE_LABELS, resolveKreaGenerateAssets } from '~/server/utils/imageWorkflowV2'
|
||||||
import { ensureComfyLoraNames, persistLoraFields } from '~/server/utils/loras'
|
import { ensureComfyLoraNames, persistLoraFields } from '~/server/utils/loras'
|
||||||
import { imageDimensions } from '~/server/utils/resolution'
|
import { imageDimensions } from '~/server/utils/resolution'
|
||||||
@@ -321,11 +321,11 @@ export async function runEditV2(job: Job, params: EditV2RunParams) {
|
|||||||
})
|
})
|
||||||
job.stillId = still?.id
|
job.stillId = still?.id
|
||||||
if (!batch) parentStillId = still?.id
|
if (!batch) parentStillId = still?.id
|
||||||
await purgeComfyArtifacts({
|
await purgeImageComfyArtifacts({
|
||||||
video: { filename: output.filename, subfolder: output.subfolder, type: output.type },
|
output: { filename: output.filename, subfolder: output.subfolder, type: output.type },
|
||||||
imageName: uploaded?.name,
|
inputName: uploaded?.name,
|
||||||
imageSubfolder: uploaded?.subfolder,
|
inputSubfolder: uploaded?.subfolder,
|
||||||
extraImageNames: last
|
extraInputNames: last
|
||||||
? [uploadedRef?.name, uploadedMask?.name].filter((name): name is string => Boolean(name))
|
? [uploadedRef?.name, uploadedMask?.name].filter((name): name is string => Boolean(name))
|
||||||
: [],
|
: [],
|
||||||
promptId: job.promptId
|
promptId: job.promptId
|
||||||
|
|||||||
@@ -365,6 +365,7 @@ async function deleteSidecarFile(file: { filename: string; subfolder?: string; t
|
|||||||
async function purgeImageOnDesktop(opts: {
|
async function purgeImageOnDesktop(opts: {
|
||||||
inputName?: string
|
inputName?: string
|
||||||
inputSubfolder?: string
|
inputSubfolder?: string
|
||||||
|
extraInputNames?: string[]
|
||||||
output?: { filename: string; subfolder: string; type: string }
|
output?: { filename: string; subfolder: string; type: string }
|
||||||
}) {
|
}) {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
@@ -382,9 +383,10 @@ async function purgeImageOnDesktop(opts: {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
imageName: opts.inputName || '',
|
imageName: opts.inputName || '',
|
||||||
imageSubfolder: opts.inputSubfolder || '',
|
imageSubfolder: opts.inputSubfolder || '',
|
||||||
|
imageNames: (opts.extraInputNames || []).filter(Boolean),
|
||||||
output: opts.output || null
|
output: opts.output || null
|
||||||
}),
|
}),
|
||||||
signal: AbortSignal.timeout(4000)
|
signal: AbortSignal.timeout(8000)
|
||||||
})
|
})
|
||||||
} catch {
|
} catch {
|
||||||
/* Host agent purge is optional; Comfy HTTP delete is the primary path. */
|
/* Host agent purge is optional; Comfy HTTP delete is the primary path. */
|
||||||
@@ -395,6 +397,7 @@ export async function purgeImageComfyArtifacts(opts: {
|
|||||||
output?: { filename: string; subfolder: string; type: string }
|
output?: { filename: string; subfolder: string; type: string }
|
||||||
inputName?: string
|
inputName?: string
|
||||||
inputSubfolder?: string
|
inputSubfolder?: string
|
||||||
|
extraInputNames?: string[]
|
||||||
promptId?: string
|
promptId?: string
|
||||||
}) {
|
}) {
|
||||||
if (!purgeImageEnabled()) return
|
if (!purgeImageEnabled()) return
|
||||||
@@ -413,12 +416,21 @@ export async function purgeImageComfyArtifacts(opts: {
|
|||||||
type: 'input'
|
type: 'input'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
for (const name of opts.extraInputNames || []) {
|
||||||
|
if (!name || name === opts.inputName) continue
|
||||||
|
files.push({
|
||||||
|
filename: name,
|
||||||
|
subfolder: opts.inputSubfolder || '',
|
||||||
|
type: 'input'
|
||||||
|
})
|
||||||
|
}
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
await deleteSidecarFile(file)
|
await deleteSidecarFile(file)
|
||||||
}
|
}
|
||||||
await purgeImageOnDesktop({
|
await purgeImageOnDesktop({
|
||||||
inputName: opts.inputName,
|
inputName: opts.inputName,
|
||||||
inputSubfolder: opts.inputSubfolder,
|
inputSubfolder: opts.inputSubfolder,
|
||||||
|
extraInputNames: opts.extraInputNames,
|
||||||
output: opts.output
|
output: opts.output
|
||||||
})
|
})
|
||||||
if (opts.promptId) {
|
if (opts.promptId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user