Compare commits

...

2 Commits
0.1 ... main

Author SHA1 Message Date
ef79f0dcc8 geändert: docker/Dockerfile
neue Datei:     fah
	geändert:       utils.go
	gelöscht:       vorlagen/.~lock.passwortschreiben.odt#
	geändert:       vorlagen/passwortschreiben.odt
	geändert:       www/data/passwort.odt
2024-11-03 20:33:55 +01:00
edc2f290d9 + in mehreren Dateien unterteilt
+ Programm umbenannt in fah (Formularausfüllhilfe)
2024-10-30 00:03:39 +01:00
12 changed files with 165 additions and 179 deletions

View File

@ -2,20 +2,25 @@
FROM debian:latest FROM debian:latest
# Metadaten hinzufügen # Metadaten hinzufügen
LABEL maintainer="maik@schmalle.click" LABEL maintainer="fah@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 ./daten.zip /tmp/daten.zip COPY ../www/* /srv/www
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
@ -32,4 +37,4 @@ WORKDIR /srv
EXPOSE 8000 EXPOSE 8000
# Führe das Skript `MSodt` aus # Führe das Skript `MSodt` aus
CMD ["/srv/MSodt"] CMD ["/srv/fah"]

BIN
fah Executable file

Binary file not shown.

2
go.mod
View File

@ -1,4 +1,4 @@
module codeberg.org/opennet/PoC/MSodt module git.masilux.de/Lebenshilfe/fah
go 1.23 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 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.Printf("Programm wurde gestartet :)") fmt.Println("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)
@ -25,164 +17,3 @@ 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))
}

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

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.