Handle save paths across Windows drives
This commit is contained in:
@@ -95,6 +95,13 @@ def _safe_pattern(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _is_within_directory(parent_path, child_path):
|
||||||
|
try:
|
||||||
|
return os.path.commonpath([parent_path, child_path]) == parent_path
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _next_counter(directory, filename_template):
|
def _next_counter(directory, filename_template):
|
||||||
os.makedirs(directory, exist_ok=True)
|
os.makedirs(directory, exist_ok=True)
|
||||||
if "%counter%" not in filename_template:
|
if "%counter%" not in filename_template:
|
||||||
@@ -356,7 +363,7 @@ class DumasSaveImageNode:
|
|||||||
pnginfo = _build_pnginfo(prompt=prompt, extra_pnginfo=extra_pnginfo)
|
pnginfo = _build_pnginfo(prompt=prompt, extra_pnginfo=extra_pnginfo)
|
||||||
image.save(full_path, "PNG", pnginfo=pnginfo)
|
image.save(full_path, "PNG", pnginfo=pnginfo)
|
||||||
|
|
||||||
if os.path.commonpath([output_dir, full_path]) == output_dir:
|
if _is_within_directory(output_dir, full_path):
|
||||||
subfolder = os.path.relpath(frame_dir, output_dir)
|
subfolder = os.path.relpath(frame_dir, output_dir)
|
||||||
ui_images.append(
|
ui_images.append(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import sys
|
|||||||
import tempfile
|
import tempfile
|
||||||
import types
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
|
||||||
class FakeImageArray:
|
class FakeImageArray:
|
||||||
@@ -185,6 +186,32 @@ class DumasImageNodeTests(unittest.TestCase):
|
|||||||
self.assertEqual(result["ui"]["images"][0]["type"], "external")
|
self.assertEqual(result["ui"]["images"][0]["type"], "external")
|
||||||
self.assertEqual(result["ui"]["images"][0]["subfolder"], external_dir.replace("\\", "/"))
|
self.assertEqual(result["ui"]["images"][0]["subfolder"], external_dir.replace("\\", "/"))
|
||||||
|
|
||||||
|
def test_save_image_handles_windows_different_drive_paths(self):
|
||||||
|
node = self.image_nodes.DumasSaveImageNode()
|
||||||
|
image = FakeTensorBatch()
|
||||||
|
|
||||||
|
with mock.patch.object(
|
||||||
|
self.image_nodes.folder_paths,
|
||||||
|
"get_output_directory",
|
||||||
|
return_value="C:\\ComfyUI\\output",
|
||||||
|
), mock.patch.object(
|
||||||
|
self.image_nodes.os.path,
|
||||||
|
"abspath",
|
||||||
|
return_value="D:\\renders",
|
||||||
|
):
|
||||||
|
result = node.save_images(
|
||||||
|
images=image,
|
||||||
|
folder="D:\\renders",
|
||||||
|
pattern="drive_%counter%",
|
||||||
|
format="png",
|
||||||
|
quality=100,
|
||||||
|
embed_workflow=False,
|
||||||
|
save_on_run=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(result["ui"]["images"][0]["type"], "external")
|
||||||
|
self.assertEqual(result["ui"]["images"][0]["subfolder"], "D:/renders")
|
||||||
|
|
||||||
def test_save_image_skips_when_save_is_disabled(self):
|
def test_save_image_skips_when_save_is_disabled(self):
|
||||||
node = self.image_nodes.DumasSaveImageNode()
|
node = self.image_nodes.DumasSaveImageNode()
|
||||||
image = FakeTensorBatch()
|
image = FakeTensorBatch()
|
||||||
|
|||||||
Reference in New Issue
Block a user