diff --git a/docker/Dockerfile b/docker/Dockerfile index 2871254..c4e6e4e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -32,4 +32,4 @@ WORKDIR /srv EXPOSE 8000 # Führe das Skript `MSodt` aus -CMD ["/srv/MSodt"] +CMD ["/srv/fah"] diff --git a/go.mod b/go.mod index 1a3efc1..63c2f7c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module codeberg.org/opennet/PoC/MSodt +module git.masilux.de/Lebenshilfe/fah go 1.23 diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..137cfa3 --- /dev/null +++ b/handlers.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" +) + +func postPasswort(w http.ResponseWriter, r *http.Request) { + felder := make(map[string]string) + err := json.NewDecoder(r.Body).Decode(&felder) + check(err) + fmt.Printf("Username is %s\n", felder) + odtdatei := "www/data/passwort.odt" + fillODTForm("vorlagen/passwortschreiben.odt", odtdatei, felder) + makepdf(odtdatei) + json.NewEncoder(w).Encode("OK") +} + +func postHandy(w http.ResponseWriter, r *http.Request) { + felder := make(map[string]string) + err := json.NewDecoder(r.Body).Decode(&felder) + check(err) + fmt.Printf("Username is %s\n", felder) + odtdatei := "www/data/handy.odt" + fillODTForm("vorlagen/handyausgabe.odt", odtdatei, felder) + makepdf(odtdatei) + json.NewEncoder(w).Encode("OK") +} diff --git a/main.go b/main.go index b4508ad..1a717c2 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,15 @@ package main import ( - "archive/zip" - "bytes" - "encoding/json" "fmt" - "io" "log" "net/http" - "os" - "os/exec" - "strings" "github.com/go-chi/chi/v5" ) -// ------------------------------------------------------------------------------------------------- func main() { - fmt.Printf("Programm wurde gestartet :)") + fmt.Println("Programm wurde gestartet :)") router := chi.NewRouter() router.Use(jsonHeaderMiddleware) router.Post("/api/passwort", postPasswort) @@ -25,164 +17,3 @@ func main() { router.Handle("/*", http.StripPrefix("/", http.FileServer(http.Dir("www")))) log.Fatal(http.ListenAndServe(":8000", router)) } - -func postPasswort(w http.ResponseWriter, r *http.Request) { - - felder := make(map[string]string) - - err := json.NewDecoder(r.Body).Decode(&felder) - check(err) - fmt.Printf("Username is %s\n", felder) - - odtdatei := "www/data/passwort.odt" - - fillODTForm("vorlagen/passwortschreiben.odt", odtdatei, felder) - makepdf(odtdatei) - - json.NewEncoder(w).Encode("OK") -} - -func postHandy(w http.ResponseWriter, r *http.Request) { - - felder := make(map[string]string) - - err := json.NewDecoder(r.Body).Decode(&felder) - check(err) - fmt.Printf("Username is %s\n", felder) - - odtdatei := "www/data/handy.odt" - - fillODTForm("vorlagen/handyausgabe.odt", odtdatei, felder) - makepdf(odtdatei) - - json.NewEncoder(w).Encode("OK") -} - -func fillODTForm(inputPath, outputPath string, fields map[string]string) error { - // Open the ODT file - reader, err := zip.OpenReader(inputPath) - if err != nil { - return err - } - defer reader.Close() - - // Create a new ODT file - writer, err := os.Create(outputPath) - if err != nil { - return err - } - defer writer.Close() - - zipWriter := zip.NewWriter(writer) - defer zipWriter.Close() - - // Process each file in the ODT - for _, file := range reader.File { - if file.Name == "content.xml" { - // Parse and modify content.xml - rc, err := file.Open() - if err != nil { - return err - } - defer rc.Close() - - content, err := io.ReadAll(rc) - if err != nil { - return err - } - - // Parse XML and fill form fields - // This is a simplified example - you'll need to implement proper XML parsing - for field, value := range fields { - content = bytes.Replace(content, []byte("%"+field+"%"), []byte(value), -1) - } - - // Write modified content.xml to new ODT - w, err := zipWriter.Create(file.Name) - if err != nil { - return err - } - _, err = w.Write(content) - if err != nil { - return err - } - } else { - // Copy other files as-is - w, err := zipWriter.Create(file.Name) - if err != nil { - return err - } - rc, err := file.Open() - if err != nil { - return err - } - _, err = io.Copy(w, rc) - rc.Close() - if err != nil { - return err - } - } - } - - return nil -} - -//-------------------------------------------------------------------------- -// HTTP-Middelwares -//-------------------------------------------------------------------------- - -func jsonHeaderMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Println(r.URL.Path) - if strings.HasPrefix(r.URL.Path, "/api/") { - w.Header().Add("Content-Type", "application/json") - } - next.ServeHTTP(w, r) - }) -} - -//-------------------------------------------------------------------------- -// Hilfsfunktionen -//-------------------------------------------------------------------------- - -func check(e error) bool { - if e != nil { - fmt.Println(e) - return false - } - return true -} - -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} - -func fileToString(filename string) string { - b, err := os.ReadFile(filename) - check(err) - str := string(b) - return str -} - -//-------------------------------------------------------------------------- -// PDF -Konvertirung -//-------------------------------------------------------------------------- - -func makepdf(odtdatei string) { - // Der Befehl, den du ausführen möchtest - cmd := exec.Command("/usr/lib/libreoffice/program/soffice.bin", "--headless", "--convert-to", "pdf", "./www/data", "--outdir", "./www/data", odtdatei) - - // Führe den Befehl aus und erfasse die Ausgabe - output, err := cmd.Output() - if err != nil { - fmt.Println("Fehler beim Ausführen des Befehls:", err) - return - } - - // Gib die Ausgabe des Befehls aus - fmt.Println(string(output)) -} diff --git a/middlewares.go b/middlewares.go new file mode 100644 index 0000000..a132f4c --- /dev/null +++ b/middlewares.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "net/http" + "strings" +) + +func jsonHeaderMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.Path) + if strings.HasPrefix(r.URL.Path, "/api/") { + w.Header().Add("Content-Type", "application/json") + } + next.ServeHTTP(w, r) + }) +} diff --git a/odt_helper.go b/odt_helper.go new file mode 100644 index 0000000..a36e437 --- /dev/null +++ b/odt_helper.go @@ -0,0 +1,65 @@ +package main + +import ( + "archive/zip" + "bytes" + "io" + "os" +) + +func fillODTForm(inputPath, outputPath string, fields map[string]string) error { + reader, err := zip.OpenReader(inputPath) + if err != nil { + return err + } + defer reader.Close() + + writer, err := os.Create(outputPath) + if err != nil { + return err + } + defer writer.Close() + + zipWriter := zip.NewWriter(writer) + defer zipWriter.Close() + + for _, file := range reader.File { + if file.Name == "content.xml" { + rc, err := file.Open() + if err != nil { + return err + } + defer rc.Close() + content, err := io.ReadAll(rc) + if err != nil { + return err + } + for field, value := range fields { + content = bytes.Replace(content, []byte("%"+field+"%"), []byte(value), -1) + } + w, err := zipWriter.Create(file.Name) + if err != nil { + return err + } + _, err = w.Write(content) + if err != nil { + return err + } + } else { + w, err := zipWriter.Create(file.Name) + if err != nil { + return err + } + rc, err := file.Open() + if err != nil { + return err + } + _, err = io.Copy(w, rc) + rc.Close() + if err != nil { + return err + } + } + } + return nil +} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..90b874b --- /dev/null +++ b/utils.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "os" + "os/exec" +) + +func check(e error) bool { + if e != nil { + fmt.Println(e) + return false + } + return true +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func fileToString(filename string) string { + b, err := os.ReadFile(filename) + check(err) + return string(b) +} + +func makepdf(odtdatei string) { + cmd := exec.Command("/usr/lib/libreoffice/program/soffice.bin", "--headless", "--convert-to", "pdf", "--outdir", "./www/data", odtdatei) + output, err := cmd.Output() + if err != nil { + fmt.Println("Fehler beim Ausführen des Befehls:", err) + return + } + fmt.Println(string(output)) +} diff --git a/vorlagen/.~lock.passwortschreiben.odt# b/vorlagen/.~lock.passwortschreiben.odt# new file mode 100644 index 0000000..0d3fe49 --- /dev/null +++ b/vorlagen/.~lock.passwortschreiben.odt# @@ -0,0 +1 @@ +Maik Schmalle,masilux,Tuxedo-Maik,28.10.2024 20:03,file:///home/masilux/.config/libreoffice/4; \ No newline at end of file diff --git a/vorlagen/handyausgabe.odt b/vorlagen/handyausgabe.odt new file mode 100644 index 0000000..8f68d0b Binary files /dev/null and b/vorlagen/handyausgabe.odt differ diff --git a/vorlagen/passwortschreiben.odt b/vorlagen/passwortschreiben.odt new file mode 100644 index 0000000..289697d Binary files /dev/null and b/vorlagen/passwortschreiben.odt differ diff --git a/www/data/passwort.odt b/www/data/passwort.odt new file mode 100644 index 0000000..30e722b Binary files /dev/null and b/www/data/passwort.odt differ diff --git a/www/data/passwort.pdf b/www/data/passwort.pdf new file mode 100644 index 0000000..5259c3f Binary files /dev/null and b/www/data/passwort.pdf differ