From 73eaf395c70cbcf8dd06032ce629c6cd15fd2b3a Mon Sep 17 00:00:00 2001 From: matu6968 Date: Thu, 31 Oct 2024 20:44:19 +0100 Subject: [PATCH] new code yay :3 --- eprintcloned | 38 +++++++++++++++++++++ main.go | 81 ++++++++++++++++----------------------------- web/assets/index.js | 32 +----------------- web/index.html | 28 ++++------------ 4 files changed, 74 insertions(+), 105 deletions(-) create mode 100755 eprintcloned diff --git a/eprintcloned b/eprintcloned new file mode 100755 index 0000000..f573db8 --- /dev/null +++ b/eprintcloned @@ -0,0 +1,38 @@ +#!/bin/bash + +printer=Deskjet-2600 # replace this with your actual printer name +imagelogmsg="Server hoster decided to enable file logging, if you don't wish your file to be logged, ask the server owner to delete your sent file or if you are the server hoster set FILELOG=nolog in .env file" # add a custom logging of files message + +# Loop through each file passed as an argument +for file in "$1"; do + # Get the MIME type of the file + mime_type=$(file --mime-type -b "$file") + + # Check if the MIME type is for images, text, or PDF files + if [[ "$mime_type" == image/* || "$mime_type" == application/pdf ]]; then + if [[ $2 == log ]]; then + echo $imagelogmsg + cp $1 $HOME/imagelog + echo "Printing image/document" + lpr -o portrait -o media=A4 $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code. + else + echo "Printing image/document" + lpr -o portrait -o media=A4 $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code. + fi + elif [[ "$mime_type" == text/* ]]; then + if [[ $2 == log ]]; then + echo $imagelogmsg + cp $1 $HOME/imagelog + echo "Printing text" + lpr -o portrait -o media=A4 $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code. + ntfymailsend $3 + else + echo "Printing text" + lpr -o portrait -o media=A4 $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code. + ntfymailsend $3 + fi + else + echo "Error: Invalid file type or, ePrint clone only accepts the following MIME types: text/*, image/*, application/pdf" + exit 1 + fi +done diff --git a/main.go b/main.go index fff6a22..1ecbd69 100644 --- a/main.go +++ b/main.go @@ -19,12 +19,21 @@ func main() { if err != nil { log.Fatal("Error loading .env file") } + + filelog := os.Getenv("LOG") + if filelog == "" { + filelog = "nolog" + } port := os.Getenv("PORT") if port == "" { port = "8080" } - + + sendntfynotification := os.Getenv("SENDNTFYMAIL") + if sendntfynotification == "" { + sendntfynotification = "true" + } http.HandleFunc("/", handleIndex) http.HandleFunc("/upload", handleUpload) http.HandleFunc("/delete", handleDelete) @@ -68,9 +77,9 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { } defer file.Close() - directory := r.FormValue("directory") - if directory == "" { - directory = "/" + targetmail := r.FormValue("targetmail") + if targetmail == "" { + fmt.Printf("Warning: User did not specify a mail on sent request") } tempFileName := filepath.Join(os.TempDir(), handler.Filename) @@ -87,18 +96,21 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) return } - - s3ClientPath := filepath.Join(".", "bin", "s3-client") - configFilePath := filepath.Join(".", "bin", "s3config.toml") - cmd := exec.Command(s3ClientPath, "-config", configFilePath, "-directory", directory, "-file", tempFile.Name()) - - cmd.Args = append(cmd.Args, "-overwrite") - + + sendntfynotification := os.Getenv("SENDNTFYMAIL") + if sendntfynotification == "false" { + targetmail = "" + } + + printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned") + filelog := os.Getenv("LOG") + cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, targetmail) + output, err := cmd.CombinedOutput() if err != nil { - log.Printf("Error uploading file: %s\n", output) + log.Printf("Error printing file: %s\n", output) json.NewEncoder(w).Encode(map[string]string{ - "error": fmt.Sprintf("Error uploading file to S3: %s", output), + "error": fmt.Sprintf("Error printing file to printer: %s", output), }) return } @@ -107,7 +119,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { Message string `json:"message"` Output string `json:"output"` }{ - Message: fmt.Sprintf("File %s uploaded successfully", handler.Filename), + Message: fmt.Sprintf("File %s printed successfully", handler.Filename), Output: string(output), } @@ -116,43 +128,6 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { func handleDelete(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 - } - - filename := r.FormValue("filename") - if filename == "" { - json.NewEncoder(w).Encode(map[string]string{"error": "Filename is required"}) - return - } - - s3ClientPath := filepath.Join(".", "bin", "s3-client") - configFilePath := filepath.Join(".", "bin", "s3config.toml") - cmd := exec.Command(s3ClientPath, "-config", configFilePath, "-delete", filename) - output, err := cmd.CombinedOutput() - if err != nil { - log.Printf("Error deleting file: %s\n", output) - json.NewEncoder(w).Encode(map[string]string{ - "error": fmt.Sprintf("Error deleting file from S3: %s", output), - }) - return - } - - response := struct { - Message string `json:"message"` - Output string `json:"output"` - }{ - Message: fmt.Sprintf("File %s deleted successfully from S3", filename), - Output: string(output), - } - - json.NewEncoder(w).Encode(response) + 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 } diff --git a/web/assets/index.js b/web/assets/index.js index de858d8..419e1eb 100644 --- a/web/assets/index.js +++ b/web/assets/index.js @@ -4,7 +4,7 @@ const commandOutput = document.getElementById('commandOutput'); uploadForm.addEventListener('submit', async (e) => { e.preventDefault(); - uploadStatus.textContent = 'Uploading...'; + uploadStatus.textContent = 'Uploading and printing...'; commandOutput.textContent = ''; const formData = new FormData(uploadForm); @@ -27,33 +27,3 @@ uploadForm.addEventListener('submit', async (e) => { uploadStatus.textContent = `Error: ${error.message}`; } }); - -const deleteForm = document.getElementById('deleteForm'); -const deleteStatus = document.getElementById('deleteStatus'); -const deleteOutput = document.getElementById('deleteOutput'); - -deleteForm.addEventListener('submit', async (e) => { - e.preventDefault(); - deleteStatus.textContent = 'Deleting...'; - deleteOutput.textContent = ''; - - const formData = new FormData(deleteForm); - - try { - const response = await fetch('/delete', { - method: 'POST', - body: formData - }); - - const result = await response.json(); - - if (result.error) { - throw new Error(result.error); - } else { - deleteStatus.textContent = result.message; - deleteOutput.textContent = result.output; - } - } catch (error) { - deleteStatus.textContent = `Error: ${error.message}`; - } -}); \ No newline at end of file diff --git a/web/index.html b/web/index.html index 8fc9c38..736cf9a 100644 --- a/web/index.html +++ b/web/index.html @@ -4,7 +4,7 @@ - S3-Client Web Wrapper + HP ePrint clone Web Wrapper -
S3-Client WebGUI
+
HP ePrint like printing WebGUI
File Selection
- +
@@ -30,20 +30,6 @@
-
- Delete -
- - -
-
-
Terminal
@@ -55,8 +41,8 @@