Commit dee497f for stable-diffusion.cpp
commit dee497f4df67786a24a30b46176ebc188f247192
Author: leejet <leejet714@gmail.com>
Date: Sat Nov 28 01:34:32 2026 +0800
fix(server): honor configured DGL output directories (#2444)
diff --git a/examples/server/README.md b/examples/server/README.md
index ce97cb2..b56a1b6 100644
--- a/examples/server/README.md
+++ b/examples/server/README.md
@@ -161,11 +161,11 @@ drag the timeline below it to seek. AVI path previews support MJPEG and
uncompressed 24-bit RGB video with up to 64 MiB of PCM or 32-bit float audio.
The speaker button mutes the preview. To adjust volume in an inline preview,
hover or focus the speaker button to open the volume slider.
-Use a relative path under the server's working directory, such as
-`assets/reference.png` or `output/clip.webm`. Turn off **Media view** in the
-editor toolbar to hide the preview. The server previews files under `input/`,
-`output/`, and `assets/`. Start the server
-with `--no-media-view` to disable file previews at the server.
+The editor previews files under `input/`, `assets/`, and the configured output
+directory (`output/` by default). For example, use `assets/reference.png` or
+`output/clip.webm` with the default configuration. Turn off **Media view** in
+the editor toolbar to hide previews. Start the server with `--no-media-view`
+to disable file previews at the server.
DGL jobs share an execution context for the lifetime of the server. Compatible
scripts can reuse loaded model components across runs. The context can release
@@ -175,10 +175,14 @@ Scripts are listed and saved in the server's working directory under
`dgl/` by default. Use `--dgl-dir` to select another directory. Script paths
are relative to the server's working directory. Model files and their
dependencies must reside under one of the configured `--model-dir` roots.
-Write results under `output/` in the working directory. For example,
+Write results under `output/` by default. For example,
`image::save("output/result.png", image)` saves a PNG. Use `--output-dir` to
-redirect writable output to another directory; writes outside the resolved
-output directory are denied.
+choose a dedicated directory for DGL writes and previews. For example, start
+the server with `--model-dir ../models --output-dir renders` and use
+`image::save("renders/result.png", image)` to save and preview the PNG.
+Relative paths still resolve from the server's working directory. Absolute
+paths under the configured output directory also work. Writes outside that
+directory are denied.
## Model management
@@ -461,6 +465,7 @@ configured defaults.
| `--listen-ip <ip>` | `127.0.0.1` | Server listen address |
| `--listen-port <port>` | `1234` | Server listen port |
| `--dgl-dir <path>` | `dgl` | Script directory for the DGL editor |
+| `--output-dir <path>` | `output` | Directory for DGL writes and output previews |
| `--no-media-view` | Off | Disable the media view endpoint |
| `--max-pending-jobs <count>` | `64` | Maximum async jobs queued or generating |
| `--completed-ttl-seconds <seconds>` | `600` | Retention time for completed async jobs |
diff --git a/examples/server/api.md b/examples/server/api.md
index f2ebb5d..ae61d05 100644
--- a/examples/server/api.md
+++ b/examples/server/api.md
@@ -859,6 +859,7 @@ Top-level fields:
| `upscalers` | `array<object>` | Available highres upscalers, built-in and model-backed |
| `preview_modes` | `array<string>` | Available preview modes, e.g. `["none", "proj", "tae", "vae"]` |
| `media_view` | `boolean` | Whether the media view endpoint is enabled |
+| `media_output` | `object`, optional | Configured output directory spellings in `paths` for matching DGL paths in the editor. `case_insensitive` is `true` on Windows. Omitted for the default directory or when media view is disabled. |
| `limits` | `object` | Shared queue and size limits |
`model`
@@ -1832,10 +1833,12 @@ Example cancelled job:
## Media view
`GET /sdcpp/v1/view?root=assets&path=reference.png` serves an image or video
-over the configured listener. `root` selects `input`, `output`, or `assets`
-in the server's working directory. `path` is a slash-separated relative path
-within that directory. Supported formats are PNG, JPEG, GIF, WebP, WebM, MP4,
-and AVI. The endpoint supports HTTP byte ranges and sends `Cache-Control: no-store`.
+over the configured listener. `root` selects `input` or `assets` in the
+server's working directory, or `output` for the directory selected by
+`--output-dir` (`output/` in the working directory by default). `path` is a
+slash-separated relative path within the selected directory. Supported formats
+are PNG, JPEG, GIF, WebP, WebM, MP4, and AVI. The endpoint supports HTTP byte
+ranges and sends `Cache-Control: no-store`.
The server returns `403` when access is denied or the server was started with
`--no-media-view`. Unavailable paths, links, and unsupported files return
@@ -1861,7 +1864,8 @@ from the runtime.
Scripts share the component cache and execution settings used for generation.
Model reads are restricted to the configured model directories. Script writes
-are restricted to output/ in the server's working directory.
+are restricted to `--output-dir`, which defaults to `output/` in the server's
+working directory.
Cancellation returns the current job status with HTTP 200. A queued job
becomes `cancelled` immediately. A running job sets the `cancel_requested`
diff --git a/examples/server/async_jobs.cpp b/examples/server/async_jobs.cpp
index 0621ffc..c3eead3 100644
--- a/examples/server/async_jobs.cpp
+++ b/examples/server/async_jobs.cpp
@@ -712,10 +712,12 @@ namespace {
void configure_server_file_access(ServerModels& models, const SDSvrParams& params, const SDContextParams& config) {
auto& policy = models.file_access;
- const fs::path output_root =
+ fs::path output_root =
params.output_dir.empty()
? fs::weakly_canonical(fs::current_path()) / "output"
: fs::weakly_canonical(fs::absolute(fs::u8path(params.output_dir)));
+ if (output_root.has_relative_path() && !output_root.has_filename())
+ output_root = output_root.parent_path();
policy = {get_model_roots(params), output_root, {}, {}};
models.workspace_file_access = policy;
for (const auto* directory : {&config.lora_model_dir, &config.hires_upscalers_dir})
diff --git a/examples/server/frontend b/examples/server/frontend
index dd77dc8..37c158f 160000
--- a/examples/server/frontend
+++ b/examples/server/frontend
@@ -1 +1 @@
-Subproject commit dd77dc8255bfd2371f733b894f7f773d05b92aa0
+Subproject commit 37c158f2517c1657a20af6d8fb96e2a681b8c2fc
diff --git a/examples/server/main.cpp b/examples/server/main.cpp
index 5c1737b..432d0cd 100644
--- a/examples/server/main.cpp
+++ b/examples/server/main.cpp
@@ -154,7 +154,7 @@ int main(int argc, const char** argv) {
index_html = "Stable Diffusion Server is running";
#endif
register_index_endpoints(svr, svr_params, index_html);
- register_media_view_endpoint(svr, svr_params);
+ register_media_view_endpoint(svr, svr_params.media_view, models.workspace_file_access.output);
register_openai_api_endpoints(svr, runtime);
register_sdapi_endpoints(svr, runtime);
register_sdcpp_api_endpoints(svr, runtime);
diff --git a/examples/server/routes.h b/examples/server/routes.h
index 656c56a..01c8de1 100644
--- a/examples/server/routes.h
+++ b/examples/server/routes.h
@@ -6,7 +6,7 @@
#include "runtime.h"
void register_index_endpoints(httplib::Server& svr, const SDSvrParams& svr_params, const std::string& index_html);
-void register_media_view_endpoint(httplib::Server& svr, const SDSvrParams& svr_params);
+void register_media_view_endpoint(httplib::Server& svr, bool enabled, const std::filesystem::path& output_root);
void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt);
void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt);
void register_sdcpp_api_endpoints(httplib::Server& svr, ServerRuntime& rt);
diff --git a/examples/server/routes_media.cpp b/examples/server/routes_media.cpp
index ad390a3..6f04678 100644
--- a/examples/server/routes_media.cpp
+++ b/examples/server/routes_media.cpp
@@ -15,6 +15,8 @@
#endif
namespace media_view {
+ namespace fs = std::filesystem;
+
#ifdef _WIN32
class FileHandle {
HANDLE handle_;
@@ -34,17 +36,17 @@ namespace media_view {
HANDLE get() const { return handle_; }
};
- static FileHandle working_directory() {
- return FileHandle(CreateFileW(L".", FILE_TRAVERSE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
+ static FileHandle open_directory(const fs::path& path) {
+ return FileHandle(CreateFileW(path.c_str(), FILE_TRAVERSE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr));
}
- static FileHandle open_child(const FileHandle& parent, const std::string& part, bool directory) {
+ static FileHandle open_child(const FileHandle& parent, const fs::path& part, bool directory) {
static const auto open_file = reinterpret_cast<decltype(&NtOpenFile)>(
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtOpenFile"));
if (!open_file)
return FileHandle();
- auto name = std::filesystem::u8path(part).native();
+ auto name = part.native();
const auto bytes = static_cast<USHORT>(name.size() * sizeof(wchar_t));
UNICODE_STRING relative{bytes, bytes, name.data()};
OBJECT_ATTRIBUTES object;
@@ -99,11 +101,11 @@ namespace media_view {
int get() const { return fd_; }
};
- static FileHandle working_directory() {
- return FileHandle(open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+ static FileHandle open_directory(const fs::path& path) {
+ return FileHandle(open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
}
- static FileHandle open_child(const FileHandle& parent, const std::string& part, bool directory) {
+ static FileHandle open_child(const FileHandle& parent, const fs::path& part, bool directory) {
const auto flags = O_RDONLY | O_NOFOLLOW | O_CLOEXEC | (directory ? O_DIRECTORY : O_NONBLOCK);
return FileHandle(openat(parent.get(), part.c_str(), flags));
}
@@ -130,20 +132,39 @@ namespace media_view {
std::none_of(part.begin(), part.end(), [](unsigned char c) { return c < 32 || c == 127; });
}
- static FileHandle open_media(const std::string& root, std::string_view path) {
- if (root != "input" && root != "output" && root != "assets")
- return FileHandle();
- const auto cwd = working_directory();
- if (!cwd)
- return FileHandle();
- auto parent = open_child(cwd, root, true);
+ static FileHandle open_output_root(const fs::path& path) {
+ auto root = path.root_path();
+ const auto relative = path.relative_path();
+ auto part = relative.begin();
+#ifdef _WIN32
+ // A UNC path needs the share name before it can be opened as a directory.
+ if (path.root_name().native().compare(0, 2, L"\\\\") == 0 && part != relative.end()) {
+ root /= *part;
+ ++part;
+ }
+#endif
+ auto directory = open_directory(root);
+ for (; directory && part != relative.end(); ++part)
+ directory = open_child(directory, *part, true);
+ return directory;
+ }
+
+ static FileHandle open_media(const fs::path& output_root, const std::string& root, std::string_view path) {
+ FileHandle parent;
+ if (root == "output") {
+ parent = open_output_root(output_root);
+ } else if (root == "input" || root == "assets") {
+ const auto cwd = open_directory(".");
+ if (cwd)
+ parent = open_child(cwd, root, true);
+ }
while (parent) {
const auto slash = path.find('/');
const auto part = path.substr(0, slash);
if (!valid_component(part))
break;
const bool directory = slash != std::string_view::npos;
- auto child = open_child(parent, std::string(part), directory);
+ auto child = open_child(parent, fs::u8path(part), directory);
if (!directory)
return child;
parent = std::move(child);
@@ -230,8 +251,11 @@ namespace media_view {
return nullptr;
}
- static bool set_media_content(const std::string& root, const std::string& path, httplib::Response& res) {
- auto file = open_media(root, path);
+ static bool set_media_content(const fs::path& output_root,
+ const std::string& root,
+ const std::string& path,
+ httplib::Response& res) {
+ auto file = open_media(output_root, root, path);
if (!file)
return false;
const auto size = file_size(file);
@@ -256,8 +280,8 @@ namespace media_view {
}
}
-void register_media_view_endpoint(httplib::Server& svr, const SDSvrParams& params) {
- svr.Get("/sdcpp/v1/view", [enabled = params.media_view](const httplib::Request& req, httplib::Response& res) {
+void register_media_view_endpoint(httplib::Server& svr, bool enabled, const std::filesystem::path& output_root) {
+ svr.Get("/sdcpp/v1/view", [enabled, output_root](const httplib::Request& req, httplib::Response& res) {
res.set_header("Cache-Control", "no-store");
res.set_header("X-Content-Type-Options", "nosniff");
if (!enabled) {
@@ -266,7 +290,7 @@ void register_media_view_endpoint(httplib::Server& svr, const SDSvrParams& param
}
try {
if (req.get_param_value_count("root") == 1 && req.get_param_value_count("path") == 1 &&
- media_view::set_media_content(req.get_param_value("root"), req.get_param_value("path"), res))
+ media_view::set_media_content(output_root, req.get_param_value("root"), req.get_param_value("path"), res))
return;
} catch (const std::system_error&) {
// Invalid path encodings are treated like unavailable files.
diff --git a/examples/server/routes_sdcpp.cpp b/examples/server/routes_sdcpp.cpp
index 5e92df0..d107229 100644
--- a/examples/server/routes_sdcpp.cpp
+++ b/examples/server/routes_sdcpp.cpp
@@ -342,12 +342,23 @@ static json make_capabilities_json(ServerRuntime& runtime) {
result["generation_features"] = generation_features;
result["generation_model_selection"] = true;
result["media_view"] = runtime.svr_params->media_view;
- result["runtime"] = model_status(runtime);
- result["current_mode"] = current_mode;
- result["supported_modes"] = supported_modes;
- result["defaults"] = top_level_defaults;
- result["defaults_by_mode"] = defaults_by_mode;
- result["limits"] = {
+ if (runtime.svr_params->media_view && !runtime.svr_params->output_dir.empty()) {
+ const auto& output = runtime.models->workspace_file_access.output;
+ auto paths = json::array({output.generic_u8string()});
+ const auto relative = output.lexically_relative(fs::weakly_canonical(fs::current_path()));
+ if (!relative.empty())
+ paths.push_back(relative.generic_u8string());
+ result["media_output"] = {{"paths", std::move(paths)}};
+#ifdef _WIN32
+ result["media_output"]["case_insensitive"] = true;
+#endif
+ }
+ result["runtime"] = model_status(runtime);
+ result["current_mode"] = current_mode;
+ result["supported_modes"] = supported_modes;
+ result["defaults"] = top_level_defaults;
+ result["defaults_by_mode"] = defaults_by_mode;
+ result["limits"] = {
{"min_width", 64},
{"max_width", k_max_generation_dimension},
{"min_height", 64},
diff --git a/examples/server/runtime.cpp b/examples/server/runtime.cpp
index 0a6b80b..6853acd 100644
--- a/examples/server/runtime.cpp
+++ b/examples/server/runtime.cpp
@@ -304,7 +304,7 @@ ArgOptions SDSvrParams::get_options() {
{"-l", "--listen-ip", "server listen ip (default: 127.0.0.1)", 0, &listen_ip},
{"", "--serve-html-path", "path to HTML file to serve at root (optional)", 0, &serve_html_path},
{"", "--dgl-dir", "Web UI script directory (default: dgl)", 0, &dgl_dir},
- {"", "--output-dir", "directory generated files may be written to (default: <working directory>/output)", 0,
+ {"", "--output-dir", "directory for DGL writes and output previews (default: <working directory>/output)", 0,
&output_dir},
};