+ in mehreren Dateien unterteilt

+ Programm umbenannt in fah (Formularausfüllhilfe)
This commit is contained in:
Maik Schmalle 2024-10-30 00:03:39 +01:00
parent d6fd3f16e2
commit edc2f290d9
12 changed files with 154 additions and 172 deletions

View File

@ -32,4 +32,4 @@ WORKDIR /srv
EXPOSE 8000
# Führe das Skript `MSodt` aus
CMD ["/srv/MSodt"]
CMD ["/srv/fah"]

2
go.mod
View File

@ -1,4 +1,4 @@
module codeberg.org/opennet/PoC/MSodt
module git.masilux.de/Lebenshilfe/fah
go 1.23

29
handlers.go Normal file
View File

@ -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")
}

171
main.go
View File

@ -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))
}

17
middlewares.go Normal file
View File

@ -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)
})
}

65
odt_helper.go Normal file
View File

@ -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
}

39
utils.go Normal file
View File

@ -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))
}

View File

@ -0,0 +1 @@
Maik Schmalle,masilux,Tuxedo-Maik,28.10.2024 20:03,file:///home/masilux/.config/libreoffice/4;

BIN
vorlagen/handyausgabe.odt Normal file

Binary file not shown.

Binary file not shown.

BIN
www/data/passwort.odt Normal file

Binary file not shown.

BIN
www/data/passwort.pdf Normal file

Binary file not shown.