web-hp-eprint-clone/main.go

126 lines
3.0 KiB
Go
Raw Normal View History

2024-09-29 19:15:37 +02:00
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
2024-10-31 20:44:19 +01:00
filelog := os.Getenv("LOG")
if filelog == "" {
filelog = "nolog"
}
2024-09-29 19:15:37 +02:00
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
2024-10-31 20:44:19 +01:00
2024-09-29 19:15:37 +02:00
http.HandleFunc("/", handleIndex)
http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/delete", handleDelete)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("web/assets"))))
fmt.Printf("Server starting on port %s\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("web/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
return
}
err := r.ParseMultipartForm(10 << 20)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
file, handler, err := r.FormFile("file")
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
defer file.Close()
2024-10-31 20:44:19 +01:00
targetmail := r.FormValue("targetmail")
if targetmail == "" {
fmt.Printf("Warning: User did not specify a mail on sent request")
2024-09-29 19:15:37 +02:00
}
tempFileName := filepath.Join(os.TempDir(), handler.Filename)
tempFile, err := os.Create(tempFileName)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
defer os.Remove(tempFile.Name())
defer tempFile.Close()
_, err = io.Copy(tempFile, file)
if err != nil {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}
2024-10-31 20:44:19 +01:00
printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned")
filelog := os.Getenv("LOG")
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, targetmail)
2024-09-29 19:15:37 +02:00
output, err := cmd.CombinedOutput()
if err != nil {
2024-10-31 20:44:19 +01:00
log.Printf("Error printing file: %s\n", output)
2024-09-29 19:15:37 +02:00
json.NewEncoder(w).Encode(map[string]string{
2024-10-31 20:44:19 +01:00
"error": fmt.Sprintf("Error printing file to printer: %s", output),
2024-09-29 19:15:37 +02:00
})
return
}
response := struct {
Message string `json:"message"`
Output string `json:"output"`
}{
2024-10-31 20:44:19 +01:00
Message: fmt.Sprintf("File %s printed successfully", handler.Filename),
2024-09-29 19:15:37 +02:00
Output: string(output),
}
json.NewEncoder(w).Encode(response)
}
func handleDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
2024-10-31 20:44:19 +01:00
json.NewEncoder(w).Encode(map[string]string{"message": "To delete logs of your sent images from the instance you have sent them to, contact the owner of the instance to do it. "})
return
2024-09-29 19:15:37 +02:00
}