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