embed diagram in url

This commit is contained in:
2026-01-12 15:52:58 +11:00
parent 3dbfc6d96b
commit b6de489be7

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template" "html/template"
@@ -238,8 +239,8 @@ func uploadOpenHandler(w http.ResponseWriter, r *http.Request) {
} }
id := storeDiagram(xml) id := storeDiagram(xml)
targetURL := buildDiagramsNetURL(r, id) targetURL := buildDiagramsNetURL(r, id, xml)
log.Printf("[open] redirecting to %s", targetURL) log.Printf("[open] redirecting to %s (data bytes=%d)", targetURL, len(xml))
http.Redirect(w, r, targetURL, http.StatusFound) http.Redirect(w, r, targetURL, http.StatusFound)
} }
@@ -333,18 +334,22 @@ func storeDiagram(xml string) string {
return id return id
} }
func buildDiagramsNetURL(r *http.Request, id string) string { func buildDiagramsNetURL(r *http.Request, id string, xml string) string {
scheme := "http" scheme := "http"
if r.Header.Get("X-Forwarded-Proto") == "https" || r.TLS != nil { if r.Header.Get("X-Forwarded-Proto") == "https" || r.TLS != nil {
scheme = "https" scheme = "https"
} }
base := fmt.Sprintf("%s://%s/diagram?id=%s", scheme, r.Host, url.QueryEscape(id)) base := fmt.Sprintf("%s://%s/diagram?id=%s", scheme, r.Host, url.QueryEscape(id))
// Request AWS library for custom shapes (e.g., aws4) to render correctly. // Request AWS library for custom shapes (e.g., aws4) to render correctly.
// Use only the fragment form (#U) to maximize compatibility with self-hosted draw.io instances that // Use the fragment form (#U) to point at a downloadable URL.
// expect the remote URL there. The fragment keeps the remote file URL intact and avoids the `url=` query encodedURL := url.QueryEscape(base)
// parameter, which some deployments ignore. redirect := fmt.Sprintf("%s/?splash=0&ui=min&libs=aws4", drawioBaseURL)
encoded := url.QueryEscape(base) // Also embed the XML directly via `data=` so self-hosted instances that block remote fetches can still open it.
return fmt.Sprintf("%s/?splash=0&ui=min&libs=aws4#U%s", drawioBaseURL, encoded) if xml != "" {
data := base64.StdEncoding.EncodeToString([]byte(xml))
redirect += "&data=" + url.QueryEscape(data)
}
return fmt.Sprintf("%s#U%s", redirect, encodedURL)
} }
func addCORS(w http.ResponseWriter) { func addCORS(w http.ResponseWriter) {