new code yay :3

This commit is contained in:
matu6968 2024-10-31 20:44:19 +01:00
parent fdfe35c5c3
commit 73eaf395c7
4 changed files with 74 additions and 105 deletions

38
eprintcloned Executable file
View File

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

71
main.go
View File

@ -20,11 +20,20 @@ func main() {
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)
@ -88,17 +97,20 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
return
}
s3ClientPath := filepath.Join(".", "bin", "s3-client")
configFilePath := filepath.Join(".", "bin", "s3config.toml")
cmd := exec.Command(s3ClientPath, "-config", configFilePath, "-directory", directory, "-file", tempFile.Name())
sendntfynotification := os.Getenv("SENDNTFYMAIL")
if sendntfynotification == "false" {
targetmail = ""
}
cmd.Args = append(cmd.Args, "-overwrite")
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"})
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
}
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)
}

View File

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

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/assets/global.css" />
<title>S3-Client Web Wrapper</title>
<title>HP ePrint clone Web Wrapper</title>
<style>
#uploadStatus,
#commandOutput {
@ -13,16 +13,16 @@
</style>
</head>
<body>
<div class="header">S3-Client WebGUI</div>
<div class="header">HP ePrint like printing WebGUI</div>
<fieldset>
<legend>File Selection</legend>
<form id="uploadForm">
<input type="file" name="file" required />
<input type="file" accept=".jpg, .jpeg, .png, .gif, .txt, .pdf" name="file" required />
<input
type="text"
name="directory"
placeholder="Directory to upload to [Optional]"
name="targetmail"
placeholder="Confirmation e-mail after printing finishes [Optional]"
style="width: 210px;"
/>
<br />
@ -30,20 +30,6 @@
</form>
</fieldset>
<fieldset style="margin-top: 10px">
<legend>Delete</legend>
<form style="display: flex;" id="deleteForm">
<input
type="text"
name="filename"
placeholder="Filename to delete"
required
style="width: 100%;"
/>
<button style="margin-left: 10px;" type="submit">Delete</button>
</form>
</fieldset>
<fieldset style="margin-top: 10px">
<legend>Terminal</legend>
<div id="uploadStatus"></div>
@ -55,8 +41,8 @@
</fieldset>
<div style="margin-top: 10px;" class="footer">
<a href="https://git.fluffy.pw/leafus/s3-client">[ s3-client ]</a> -
<a href="https://git.fluffy.pw/leafus/s3-client-web">[ s3-client-web ]</a> - Licensed under MIT
<a href="https://git.fluffy.pw/matu6968/web-hp-eprint-clone">[ web-hp-eprint-clone ]</a> -
<a href="https://git.fluffy.pw/leafus/s3-client-web">[ forked of s3-client-web ]</a> - Licensed under MIT
</div>
<script src="/assets/index.js"></script>