remove broken NSFW scanning feature

This commit is contained in:
matu6968 2024-11-02 21:56:23 +01:00
parent 214c8e70ac
commit 4f244b3bc2
3 changed files with 14 additions and 95 deletions

View File

@ -8,7 +8,7 @@ This is a HP ePrint clone that has similar functions to normal HP ePrint but it
- Printing PDF, any image file, text, [encoded html render .ini files](https://git.fluffy.pw/matu6968/web-hp-eprint-clone/wiki/Encoded-.ini-files-that-prints-out-a-url), MS Office documents (gets converted to a .pdf file first then print out) and HTML files
- custom print quality and page index options
- REST API
- NSFW scanning (requires DeepAI PRO account and API key)
- ~~NSFW scanning (requires DeepAI PRO account and API key)~~ feature broken and removed
## Prerequisites
@ -46,7 +46,6 @@ In the .env file this is the only thing you can set
```
PORT=8080
LOG=log # enables file logging (aka copies file to user home directory under the folder imagelog so make a folder first in the home root if you wish to enable it), to disable it replace it with nolog
NSFWCHECK=true # Requires DeepAI API key and DeepAI PRO subscription
```
## Autostart with systemd or OpenRC
@ -87,16 +86,6 @@ sudo cp /path/to/cloned/repo/init-scripts/eprint-clone-web /etc/init.d/
sudo rc-update add eprint-clone-web
sudo rc-service eprint-clone-web start
```
## DeepAI NSFW scanning
If you would like to open the service up to public, consider enabling NSFW scanning to prevent naughty content from entering your printer.
To enable it, first make a account on [DeepAI](https://deepai.org), then sign up for a DeepAI PRO subscription to use the API (5$ per month), lastly grab the API from your user dashboard
`User icon in the top right corner --> View Profile --> API Keys`
and put it in your eprintcloned file
```
deepaiapikey=00000000-0000-0000-0000-000000000000 # replace zeros with your actual API key
```
### For this to even work
You need a linux distro which has the modern equifelent of the lpr program. To check if you have the newer version, type `man lpr` and look at the program description, if it says `lpr - print files` (present on recent Ubuntu versions and rolling release distros like Arch Linux) then you are good to go otherwaise if it says `lpr - off line print` (present on Debian and older Ubuntu versions) then this won't work as the commands for the older version are different and this targets the newer version of the program.
@ -107,8 +96,7 @@ and put the eprintcloned script (ePrint clone daemon aka it handles the printing
# !IMPORTANT!
You need to modify the script to include your printer name (it is at the top and is called printer and not the one used in the included configuration in the repository) and to include your
DeepAI API key in the eprintcloned script.
You need to modify the script to include your printer name (it is at the top and is called printer and not the one used in the included configuration in the repository)
for example change it from
```

View File

@ -52,11 +52,10 @@ else
exit 1
fi
printer=examplename # replace this with your actual printer name before putting it into /usr/bin
printer=Deskjet-2600 # 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" # specify a custom logging of files message
nsfwscantosmsg="Server hoster enabled NSFW scanning, by sending any images to this instance, you agree to the DeepAI TOS at https://deepai.org/terms-of-service/terms-of-service to scan them." # specify a custom logging of NSFW message
nsfwscantrue="The following image you uploaded is not safe for work. Due to Cloudflare TOS, you cannot send content promoting NSFW"
deepaiapikey=00000000-0000-0000-0000-000000000000 # replace zeros with your actual API key
# check if quality is specified, it it isn't then set the default quality
if [[ " $3 " =~ "" ]]; then
quality="4"
@ -77,15 +76,6 @@ else
pagerange="$4"
fi
# If the user specified for NSFW scanning (.env configuration flag: NSFWSCAN=true) then check if there is a argument passed to scan for it
# To use the DeepAI NSFW scanning service, provide a API key under the variable deepaiapikey (leave it blank, and specify NSFWSCAN=false in .env if you are not using it)
if [[ " $@ " =~ "nsfwscan" ]]; then
echo $nsfwscantosmsg
nsfwscan=yes
else
echo "NSFW scanning is off"
nsfwscan=no
fi
# Loop through each file passed as an argument
for file in "$1"; do
@ -97,80 +87,10 @@ for file in "$1"; do
if [[ $2 == log ]]; then
echo $imagelogmsg
cp "$1" $HOME/imagelog/
if [ $nsfwscan = "yes" ]
then
echo "Scanning image for NSFW"
API_URL="https://api.deepai.org/api/nsfw-detector"
for image in $1; do
# Send image to the API and capture both the response and the HTTP status code and if there is another instance running, wait until it closes
wait_for_curl_to_finish
response=$(curl -s -o /tmp/response.json -w "%{http_code}" -F "image=@$image" -H "api-key: $deepaiapikey" $API_URL)
status_code=$(tail -n1 <<< "$response")
# Check for HTTP errors (non-200 status code)
if [ "$status_code" -ne 200 ]; then
echo "Failed to analyze $image: Server returned status code $status_code."
continue
fi
# Read the actual JSON response
json_response=$(head -n -1 /tmp/response.json)
# Check if the response contains "is_nsfw" field
if echo "$json_response" | grep -q '"is_nsfw": true'; then
echo $nsfwscantrue
exit 1
# If NSFW is not detected then print the image
elif echo "$json_response" | grep -q '"is_nsfw": false'; then
echo "Image is safe, printing."
# Check for invalid API key
elif echo "$json_response" | grep -q '"status": "Please pass a valid Api-Key in a HTTP header called \"Api-Key\"'; then
echo "API key is missing or invalid, skipping NSFW check."
else
# Handle unexpected JSON structure
echo "Unexpected response format for $image: $json_response, skipping NSFW check"
fi
done
fi
echo "Printing image/document"
rm /tmp/response.json
lpr -r -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$1" -P $printer && exit 0 # if you are using a different printer that requires different commands to print, change this part of the code.
else
if [ $nsfwscan == "yes" ]
then
echo "Scanning image for NSFW"
API_URL="https://api.deepai.org/api/nsfw-detector"
for image in $1; do
# Send image to the API and capture both the response and the HTTP status code and if there is another instance running, wait until it closes
wait_for_curl_to_finish
response=$(curl -s -o /tmp/response.json -w "%{http_code}" -F "image=@$image" -H "api-key: $deepaiapikey" $API_URL)
status_code=$(tail -n1 <<< "$response")
# Check for HTTP errors (non-200 status code)
if [ "$status_code" -ne 200 ]; then
echo "Failed to analyze $image: Server returned status code $status_code."
continue
fi
# Read the actual JSON response
json_response=$(head -n -1 /tmp/response.json)
# Check if the response contains "is_nsfw" field
if echo "$json_response" | grep -q '"is_nsfw": true'; then
echo $nsfwscantrue
exit 1
# If NSFW is not detected then print the image
elif echo "$json_response" | grep -q '"is_nsfw": false'; then
echo "Image is safe, printing."
# Check for invalid API key
elif echo "$json_response" | grep -q '"status": "Please pass a valid Api-Key in a HTTP header called \"Api-Key\"'; then
echo "API key is missing or invalid, skipping NSFW check."
else
# Handle unexpected JSON structure
echo "Unexpected response format for $image: $json_response, skipping NSFW check"
fi
done
fi
echo "Printing image/document"
rm /tmp/repsonse.json
lpr -r -o portrait -o media=A4 -o fit-to-page -o print-quality=$quality $pagerange "$1" -P $printer && exit 0 # if you are using a different printer that requires different commands to print, change this part of the code.

11
main.go
View File

@ -89,6 +89,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
printconfig := r.FormValue("printconfig")
if printconfig == "" {
fmt.Printf("Warning: User did not specify page range on sent request\n")
printconfig = "0"
} else {
re := regexp.MustCompile(`^[0-9]+(-[0-9]+)*$`)
if !re.MatchString(printconfig) {
@ -129,6 +130,16 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned")
filelog := os.Getenv("LOG")
nsfwcheck := os.Getenv("NSFWCHECK")
if nsfwcheck == "true" {
nsfwcheck = "nsfwcheck"
} else
if nsfwcheck == "false" {
nsfwcheck = ""
} else
if nsfwcheck == "" {
nsfwcheck = ""
}
fmt.Printf(printingcmdPath, tempFile.Name(), filelog, printres, printconfig, nsfwcheck)
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, printres, printconfig, nsfwcheck)
output, err := cmd.CombinedOutput()