408 lines
12 KiB
Go
408 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
gliffy2drawio "gliffy2drawio"
|
|
)
|
|
|
|
const (
|
|
maxUploadSize = 10 << 20 // 10MB
|
|
)
|
|
|
|
var diagramStore = struct {
|
|
data map[string]string
|
|
}{
|
|
data: make(map[string]string),
|
|
}
|
|
|
|
var uploadTpl = template.Must(template.New("upload").Parse(`
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>Gliffy → draw.io Converter</title>
|
|
<style>
|
|
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; margin: 2rem; background: #f6f7fb; color: #111; }
|
|
.wrap { max-width: 820px; margin: 0 auto; }
|
|
h1 { margin: 0 0 0.5rem 0; font-size: 1.6rem; }
|
|
p { margin: 0.25rem 0 1rem 0; color: #333; }
|
|
.card { background: #fff; border-radius: 14px; padding: 1.25rem; box-shadow: 0 8px 30px rgba(0,0,0,0.08); }
|
|
.drop {
|
|
border: 2px dashed #9aa3b2; border-radius: 14px;
|
|
padding: 1.25rem; text-align: center; background: #fbfcff;
|
|
transition: 120ms ease;
|
|
}
|
|
.drop.drag { border-color: #3b82f6; background: #eef5ff; }
|
|
.btnrow { display: flex; gap: 0.75rem; justify-content: center; margin-top: 0.75rem; flex-wrap: wrap; }
|
|
button, .buttonlike {
|
|
appearance: none; border: 0; border-radius: 10px; padding: 0.65rem 0.9rem;
|
|
background: #111827; color: #fff; cursor: pointer; font-weight: 600;
|
|
}
|
|
.secondary { background: #e5e7eb; color: #111; }
|
|
.linkbtn { background: #0b62d6; }
|
|
input[type=file] { display: none; }
|
|
.note { font-size: 0.92rem; color: #444; margin-top: 0.6rem; }
|
|
a { color: #0b62d6; text-decoration: none; font-weight: 700; }
|
|
a:hover { text-decoration: underline; }
|
|
code { background: #f1f5f9; padding: 0.1rem 0.3rem; border-radius: 6px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="wrap">
|
|
<h1>Gliffy → draw.io Converter</h1>
|
|
<p>Upload a <code>.gliffy</code> (zip) or <code>.gon</code> JSON file. Convert to <code>.drawio</code>, or open directly in diagrams.net.</p>
|
|
|
|
<div class="card">
|
|
<form id="uploadForm" method="post" enctype="multipart/form-data">
|
|
<div id="drop" class="drop">
|
|
<strong>Drag & drop</strong> your Gliffy file here<br/>
|
|
<span class="note">…or choose a file.</span>
|
|
<div class="btnrow">
|
|
<label class="buttonlike secondary" for="fileInput">Choose file</label>
|
|
<button type="submit" formaction="/convert" id="downloadBtn">Download .drawio</button>
|
|
<button type="submit" class="linkbtn" formaction="/open" id="openBtn">Open in diagrams.net</button>
|
|
</div>
|
|
<div class="note" id="picked"></div>
|
|
</div>
|
|
|
|
<input id="fileInput" type="file" name="file" accept=".gliffy,.gon,application/zip,application/json" />
|
|
</form>
|
|
|
|
<p class="note">API docs: <a href="/swagger">Swagger UI</a></p>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
(function(){
|
|
const drop = document.getElementById('drop');
|
|
const input = document.getElementById('fileInput');
|
|
const picked = document.getElementById('picked');
|
|
const form = document.getElementById('uploadForm');
|
|
|
|
function setPicked(file) {
|
|
if (!file) { picked.textContent = ''; return; }
|
|
picked.textContent = 'Selected: ' + file.name + ' (' + Math.round(file.size/1024) + ' KB)';
|
|
}
|
|
|
|
drop.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
drop.classList.add('drag');
|
|
});
|
|
drop.addEventListener('dragleave', () => drop.classList.remove('drag'));
|
|
drop.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
drop.classList.remove('drag');
|
|
if (!e.dataTransfer || !e.dataTransfer.files || e.dataTransfer.files.length === 0) return;
|
|
input.files = e.dataTransfer.files;
|
|
setPicked(input.files[0]);
|
|
});
|
|
|
|
input.addEventListener('change', () => setPicked(input.files[0]));
|
|
|
|
form.addEventListener('submit', (e) => {
|
|
if (!input.files || input.files.length === 0) {
|
|
e.preventDefault();
|
|
alert('Please choose a .gliffy or .gon file first.');
|
|
return;
|
|
}
|
|
});
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
`))
|
|
|
|
type apiRequest struct {
|
|
Data string `json:"data"` // raw Gliffy JSON
|
|
}
|
|
|
|
type apiResponse struct {
|
|
XML string `json:"xml"`
|
|
Warn string `json:"warning,omitempty"`
|
|
}
|
|
|
|
func main() {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/", uploadHandler)
|
|
mux.HandleFunc("/convert", uploadConvertHandler)
|
|
mux.HandleFunc("/open", uploadOpenHandler)
|
|
mux.HandleFunc("/api/convert", apiConvertHandler)
|
|
mux.HandleFunc("/openapi.json", openAPISpecHandler)
|
|
mux.HandleFunc("/swagger", swaggerUIHandler)
|
|
mux.HandleFunc("/diagram", diagramHandler)
|
|
|
|
addr := ":8080"
|
|
log.Printf("Server listening on %s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, mux))
|
|
}
|
|
|
|
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
if err := uploadTpl.Execute(w, nil); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func uploadConvertHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
log.Printf("[upload] %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
|
|
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
|
|
http.Error(w, "failed to parse form: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
file, header, err := r.FormFile("file")
|
|
if err != nil {
|
|
http.Error(w, "missing file: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
data, err := io.ReadAll(file)
|
|
if err != nil {
|
|
http.Error(w, "failed to read file: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
xml, warn, err := convert(string(data))
|
|
if err != nil {
|
|
log.Printf("[upload] conversion failed: %v", err)
|
|
http.Error(w, "conversion failed: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.drawio"`, safeName(header.Filename)))
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
if warn != "" {
|
|
w.Header().Set("X-Conversion-Warning", warn)
|
|
}
|
|
log.Printf("[upload] conversion succeeded, warn: %q, bytes out: %d", warn, len(xml))
|
|
_, _ = w.Write([]byte(xml))
|
|
}
|
|
|
|
func uploadOpenHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
log.Printf("[open] %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
|
|
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
|
|
http.Error(w, "failed to parse form: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
file, _, err := r.FormFile("file")
|
|
if err != nil {
|
|
http.Error(w, "missing file: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
data, err := io.ReadAll(file)
|
|
if err != nil {
|
|
http.Error(w, "failed to read file: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
xml, warn, err := convert(string(data))
|
|
if err != nil {
|
|
log.Printf("[open] conversion failed: %v", err)
|
|
http.Error(w, "conversion failed: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if warn != "" {
|
|
log.Printf("[open] conversion warning: %s", warn)
|
|
}
|
|
|
|
id := storeDiagram(xml)
|
|
targetURL := buildDiagramsNetURL(r, id)
|
|
log.Printf("[open] redirecting to %s", targetURL)
|
|
http.Redirect(w, r, targetURL, http.StatusFound)
|
|
}
|
|
|
|
func apiConvertHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
log.Printf("[api] %s %s from %s", r.Method, r.URL.Path, r.RemoteAddr)
|
|
defer r.Body.Close()
|
|
var req apiRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid JSON: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
xml, warn, err := convert(req.Data)
|
|
if err != nil {
|
|
log.Printf("[api] conversion failed: %v", err)
|
|
http.Error(w, "conversion failed: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
resp := apiResponse{XML: xml, Warn: warn}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
log.Printf("[api] conversion succeeded, warn: %q, bytes out: %d", warn, len(xml))
|
|
_ = json.NewEncoder(w).Encode(resp)
|
|
}
|
|
|
|
func convert(gliffyJSON string) (string, string, error) {
|
|
log.Printf("[convert] starting, input bytes: %d", len(gliffyJSON))
|
|
converter, err := gliffy2drawio.NewGliffyDiagramConverter(gliffyJSON)
|
|
if err != nil {
|
|
log.Printf("[convert] failed to initialize converter: %v", err)
|
|
return "", "", err
|
|
}
|
|
xml, err := converter.GraphXML()
|
|
if err != nil {
|
|
log.Printf("[convert] failed to generate XML: %v", err)
|
|
return "", "", err
|
|
}
|
|
warn := ""
|
|
log.Printf("[convert] done, output bytes: %d", len(xml))
|
|
return xml, warn, nil
|
|
}
|
|
|
|
func safeName(name string) string {
|
|
if name == "" {
|
|
return "diagram"
|
|
}
|
|
dot := strings.LastIndex(name, ".")
|
|
if dot > 0 {
|
|
name = name[:dot]
|
|
}
|
|
name = strings.ReplaceAll(name, "\"", "")
|
|
return name
|
|
}
|
|
|
|
func openAPISpecHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(openAPISpec))
|
|
}
|
|
|
|
func diagramHandler(w http.ResponseWriter, r *http.Request) {
|
|
id := r.URL.Query().Get("id")
|
|
if id == "" {
|
|
http.Error(w, "missing id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
xml, ok := diagramStore.data[id]
|
|
if !ok {
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
_, _ = w.Write([]byte(xml))
|
|
}
|
|
|
|
func storeDiagram(xml string) string {
|
|
now := time.Now().UnixNano()
|
|
id := fmt.Sprintf("diag-%d", now)
|
|
diagramStore.data[id] = xml
|
|
return id
|
|
}
|
|
|
|
func buildDiagramsNetURL(r *http.Request, id 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))
|
|
return "https://app.diagrams.net/?splash=0&ui=min&url=" + url.QueryEscape(base)
|
|
}
|
|
|
|
func swaggerUIHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
swaggerHTML := `<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>Gliffy → draw.io API</title>
|
|
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
|
|
</head>
|
|
<body>
|
|
<div id="swagger-ui"></div>
|
|
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
|
<script>
|
|
window.onload = function() {
|
|
SwaggerUIBundle({
|
|
url: '/openapi.json',
|
|
dom_id: '#swagger-ui'
|
|
});
|
|
};
|
|
</script>
|
|
</body>
|
|
</html>`
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = w.Write([]byte(swaggerHTML))
|
|
}
|
|
|
|
var openAPISpec = `
|
|
{
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Gliffy to draw.io Converter",
|
|
"version": "1.0.0"
|
|
},
|
|
"paths": {
|
|
"/api/convert": {
|
|
"post": {
|
|
"summary": "Convert Gliffy JSON to draw.io XML",
|
|
"requestBody": {
|
|
"required": true,
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"data": {
|
|
"type": "string",
|
|
"description": "Raw Gliffy JSON content"
|
|
}
|
|
},
|
|
"required": ["data"]
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"responses": {
|
|
"200": {
|
|
"description": "Conversion succeeded",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"xml": { "type": "string" },
|
|
"warning": { "type": "string" }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"400": {
|
|
"description": "Conversion failed"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|