Compare commits

..

No commits in common. "main" and "0.1" have entirely different histories.
main ... 0.1

12 changed files with 179 additions and 165 deletions

View File

@ -2,25 +2,20 @@
FROM debian:latest FROM debian:latest
# Metadaten hinzufügen # Metadaten hinzufügen
LABEL maintainer="fah@schmalle.click" LABEL maintainer="maik@schmalle.click"
# Installiere benötigte Pakete # Installiere benötigte Pakete
RUN apt update && apt install -y \ RUN apt update && apt install -y \
libreoffice \ libreoffice \
libreoffice-writer \ # libreoffice-writer \
libreoffice-calc \ # libreoffice-calc \
libreoffice-impress \ # libreoffice-impress \
openjdk8-jre \ # openjdk8-jre \
ttf-dejavu \ # ttf-dejavu \
fonts-crosextra-carlito \
fonts-crosextra-caladea \
unzip unzip
# Kopiere die ZIP-Datei in den Container # Kopiere die ZIP-Datei in den Container
COPY ../www/* /srv/www COPY ./daten.zip /tmp/daten.zip
COPY ../vorlagen/* /srv/vorlagen
COPY ../fah /srv/fah
RUN ls -h /srv
# Entpacke die ZIP-Datei # Entpacke die ZIP-Datei
RUN unzip /tmp/daten.zip -d /srv RUN unzip /tmp/daten.zip -d /srv
@ -37,4 +32,4 @@ WORKDIR /srv
EXPOSE 8000 EXPOSE 8000
# Führe das Skript `MSodt` aus # Führe das Skript `MSodt` aus
CMD ["/srv/fah"] CMD ["/srv/MSodt"]

BIN
fah

Binary file not shown.

2
go.mod
View File

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

View File

@ -1,29 +0,0 @@
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,15 +1,23 @@
package main package main
import ( import (
"archive/zip"
"bytes"
"encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os"
"os/exec"
"strings"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
// -------------------------------------------------------------------------------------------------
func main() { func main() {
fmt.Println("Programm wurde gestartet :)") fmt.Printf("Programm wurde gestartet :)")
router := chi.NewRouter() router := chi.NewRouter()
router.Use(jsonHeaderMiddleware) router.Use(jsonHeaderMiddleware)
router.Post("/api/passwort", postPasswort) router.Post("/api/passwort", postPasswort)
@ -17,3 +25,164 @@ func main() {
router.Handle("/*", http.StripPrefix("/", http.FileServer(http.Dir("www")))) router.Handle("/*", http.StripPrefix("/", http.FileServer(http.Dir("www"))))
log.Fatal(http.ListenAndServe(":8000", router)) 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))
}

View File

@ -1,17 +0,0 @@
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)
})
}

View File

@ -1,65 +0,0 @@
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
}

View File

@ -1,39 +0,0 @@
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", "--embed-fonts", "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))
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.