add new quality and page index selector setting
This commit is contained in:
matu6968 2024-11-01 19:45:12 +01:00
parent 25fefef893
commit dceb67b0aa
4 changed files with 68 additions and 17 deletions

View File

@ -1,8 +1,29 @@
#!/bin/bash
# Check for specified arguments
if [ $# -lt 3 ]
then
echo "Not enough arguments - exiting"
echo ""
exit 1
fi
printer=examplename # replace this with your actual printer name before putting it into /usr/bin
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 LOG=nolog in .env file" # add a custom logging of files message
# check if quality is specified, it it isn't then set the default quality
if [ $# -gt 4 ]; then
quality=$3
else
quality=4
fi
# check if page range is specified, it it isn't then ignore setting a page range
if [ $# -gt 4 ]; then
pagerange=$4
else
pagerange=""
fi
# Loop through each file passed as an argument
for file in "$1"; do
# Get the MIME type of the file
@ -12,22 +33,22 @@ for file in "$1"; do
if [[ "$mime_type" == image/* || "$mime_type" == application/pdf ]]; then
if [[ $2 == log ]]; then
echo $imagelogmsg
cp $1 $HOME/imagelog
cp "$1" $HOME/imagelog/
echo "Printing image/document"
lpr -o portrait -o media=A4 -o fit-to-page $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code.
lpr -r -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$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 -o fit-to-page $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code.
lpr -r -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$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
cp "$1" $HOME/imagelog/
echo "Printing text"
lpr -o portrait -o media=A4 -o fit-to-page $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code.
lpr -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$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 text"
lpr -o portrait -o media=A4 -o fit-to-page $1 -P $printer # if you are using a different printer that requires different commands to print, change this part of the code.
lpr -r -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$1" -P $printer # if you are using a different printer that requires different commands to print, change this part of the code.
fi
else
echo "Error: Invalid file type or, ePrint clone only accepts the following MIME types: text/*, image/*, application/pdf"

33
main.go
View File

@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/joho/godotenv"
)
@ -72,12 +73,32 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
return
}
defer file.Close()
targetmail := r.FormValue("targetmail")
if targetmail == "" {
fmt.Printf("Warning: User did not specify a mail on sent request")
// Regular expression to check if input contains only numbers and hyphens so that user canot execute arbitary code
printconfig := r.FormValue("printconfig")
if printconfig == "" {
fmt.Printf("Warning: User did not specify page range on sent request\n")
} else {
re := regexp.MustCompile(`^[0-9]+(-[0-9]+)*$`)
if !re.MatchString(printconfig) {
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid page range"})
fmt.Printf("debug: printconfig is: %s\n", printconfig)
return
}
}
// Regular expression to check if input contains only numbers so that user canot execute arbitary code
printres := r.FormValue("res")
if printres == "" {
fmt.Printf("Warning: User did not specify quality on sent request, assuming normal quality\n")
printres = "4"
} else {
re := regexp.MustCompile(`^[1-5]+$`)
if !re.MatchString(printres) {
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid quality range"})
fmt.Printf("debug: res is: %s\n", printres)
return
}
}
tempFileName := filepath.Join(os.TempDir(), handler.Filename)
tempFile, err := os.Create(tempFileName)
if err != nil {
@ -96,7 +117,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned")
filelog := os.Getenv("LOG")
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, targetmail)
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, printres, printconfig)
output, err := cmd.CombinedOutput()
if err != nil {

View File

@ -4,7 +4,7 @@ const commandOutput = document.getElementById('commandOutput');
uploadForm.addEventListener('submit', async (e) => {
e.preventDefault();
uploadStatus.textContent = 'Uploading and printing...';
uploadStatus.textContent = 'Uploading...';
commandOutput.textContent = '';
const formData = new FormData(uploadForm);

View File

@ -21,12 +21,21 @@
<input type="file" accept=".jpg, .jpeg, .png, .gif, .txt, .pdf" name="file" required />
<input
type="text"
name="targetmail"
placeholder="Confirmation e-mail after printing finishes [Optional]"
style="width: 210px;"
name="printconfig"
placeholder="Specify which pages to print (only supported on PDF documents) [Optional]"
style="width: 450px;"
/>
<br />
<button type="submit" class="button">Upload</button>
<form id="quality">
<label for="res">Quality:</label>
<select id="res" name="res">
<option value="1">1 (Draft)</option>
<option value="2">2 (Draft)</option>
<option value="3">3 (Draft)</option>
<option value="4" selected>4 (Normal)</option>
<option value="5">5 (Best)</option>
</select>
<button type="submit" style="width: 750px;" class="button">Upload</button>
</form>
</fieldset>