Compare commits

..

No commits in common. "8b3f1933a5bfd6a9ac7d6b24cb6492565907469f" and "fdfe35c5c30e0c79c60791ea0db594835a628b6e" have entirely different histories.

5 changed files with 112 additions and 88 deletions

View File

@ -1,6 +1,6 @@
# HP ePrint like service clone (web edition) # S3 Client Web
This is a HP ePrint clone that has similar functions to normal HP ePrint but its on the web and it is not HP printer specific This is a WebGUI for [s3-client](https://git.fluffy.pw/leafus/s3-client)
## Prerequisites ## Prerequisites
@ -10,17 +10,17 @@ This is a HP ePrint clone that has similar functions to normal HP ePrint but its
1. Clone the repository: 1. Clone the repository:
``` ```
git clone https://git.fluffy.pw/matu6968/web-hp-eprint-clone git clone https://git.fluffy.pw/leafus/s3-client-web
``` ```
2. Go to the project directory: 2. Go to the project directory:
``` ```
cd web-hp-eprint-clone cd s3-client-web
``` ```
3. Build the binary: 3. Build the binary:
``` ```
go build -o web-hp-eprint go build -o s3-client-web
``` ```
## Configuration ## Configuration
@ -29,25 +29,10 @@ In the .env file this is the only thing you can set
``` ```
PORT=8080 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
``` ```
### For this to even work ### For this to even work
Setup a printer on your host by connecting it over USB or WiFi and then adding the printer in your OS You need to download a latest linux binary release of [s3-client](https://git.fluffy.pw/leafus/s3-client) from the "Releases" tab for your architecture
and put the eprintcloned script (ePrint clone daemon aka it handles the printing) in the "/usr/bin" folder !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
for example change it from
```
#!/bin/bash
printer=examplename # replace this with your actual printer name before putting it into /usr/bin
```
to
```
#!/bin/bash
printer=Deskjet-2600 # replace this with your actual printer name before putting it into /usr/bin
```
and put the binary in the "bin" folder !IMPORTANT! you need to rename the file to just s3-client and create s3config.toml file, the configuration of that is in the [s3-client](https://git.fluffy.pw/leafus/s3-client) repository

View File

@ -1,38 +0,0 @@
#!/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

73
main.go
View File

@ -19,17 +19,12 @@ func main() {
if err != nil { if err != nil {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
} }
filelog := os.Getenv("LOG")
if filelog == "" {
filelog = "nolog"
}
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {
port = "8080" port = "8080"
} }
http.HandleFunc("/", handleIndex) http.HandleFunc("/", handleIndex)
http.HandleFunc("/upload", handleUpload) http.HandleFunc("/upload", handleUpload)
http.HandleFunc("/delete", handleDelete) http.HandleFunc("/delete", handleDelete)
@ -73,9 +68,9 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
targetmail := r.FormValue("targetmail") directory := r.FormValue("directory")
if targetmail == "" { if directory == "" {
fmt.Printf("Warning: User did not specify a mail on sent request") directory = "/"
} }
tempFileName := filepath.Join(os.TempDir(), handler.Filename) tempFileName := filepath.Join(os.TempDir(), handler.Filename)
@ -92,17 +87,18 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return return
} }
s3ClientPath := filepath.Join(".", "bin", "s3-client")
printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned") configFilePath := filepath.Join(".", "bin", "s3config.toml")
filelog := os.Getenv("LOG") cmd := exec.Command(s3ClientPath, "-config", configFilePath, "-directory", directory, "-file", tempFile.Name())
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, targetmail)
cmd.Args = append(cmd.Args, "-overwrite")
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
log.Printf("Error printing file: %s\n", output) log.Printf("Error uploading file: %s\n", output)
json.NewEncoder(w).Encode(map[string]string{ json.NewEncoder(w).Encode(map[string]string{
"error": fmt.Sprintf("Error printing file to printer: %s", output), "error": fmt.Sprintf("Error uploading file to S3: %s", output),
}) })
return return
} }
@ -111,7 +107,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
Message string `json:"message"` Message string `json:"message"`
Output string `json:"output"` Output string `json:"output"`
}{ }{
Message: fmt.Sprintf("File %s printed successfully", handler.Filename), Message: fmt.Sprintf("File %s uploaded successfully", handler.Filename),
Output: string(output), Output: string(output),
} }
@ -120,6 +116,43 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
func handleDelete(w http.ResponseWriter, r *http.Request) { func handleDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
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 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)
} }

View File

@ -4,7 +4,7 @@ const commandOutput = document.getElementById('commandOutput');
uploadForm.addEventListener('submit', async (e) => { uploadForm.addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();
uploadStatus.textContent = 'Uploading and printing...'; uploadStatus.textContent = 'Uploading...';
commandOutput.textContent = ''; commandOutput.textContent = '';
const formData = new FormData(uploadForm); const formData = new FormData(uploadForm);
@ -27,3 +27,33 @@ uploadForm.addEventListener('submit', async (e) => {
uploadStatus.textContent = `Error: ${error.message}`; 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 charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/assets/global.css" /> <link rel="stylesheet" href="/assets/global.css" />
<title>HP ePrint clone Web Wrapper</title> <title>S3-Client Web Wrapper</title>
<style> <style>
#uploadStatus, #uploadStatus,
#commandOutput { #commandOutput {
@ -13,16 +13,16 @@
</style> </style>
</head> </head>
<body> <body>
<div class="header">HP ePrint like printing WebGUI</div> <div class="header">S3-Client WebGUI</div>
<fieldset> <fieldset>
<legend>File Selection</legend> <legend>File Selection</legend>
<form id="uploadForm"> <form id="uploadForm">
<input type="file" accept=".jpg, .jpeg, .png, .gif, .txt, .pdf" name="file" required /> <input type="file" name="file" required />
<input <input
type="text" type="text"
name="targetmail" name="directory"
placeholder="Confirmation e-mail after printing finishes [Optional]" placeholder="Directory to upload to [Optional]"
style="width: 210px;" style="width: 210px;"
/> />
<br /> <br />
@ -30,6 +30,20 @@
</form> </form>
</fieldset> </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"> <fieldset style="margin-top: 10px">
<legend>Terminal</legend> <legend>Terminal</legend>
<div id="uploadStatus"></div> <div id="uploadStatus"></div>
@ -41,8 +55,8 @@
</fieldset> </fieldset>
<div style="margin-top: 10px;" class="footer"> <div style="margin-top: 10px;" class="footer">
<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">[ s3-client ]</a> -
<a href="https://git.fluffy.pw/leafus/s3-client-web">[ forked of s3-client-web ]</a> - Licensed under MIT <a href="https://git.fluffy.pw/leafus/s3-client-web">[ s3-client-web ]</a> - Licensed under MIT
</div> </div>
<script src="/assets/index.js"></script> <script src="/assets/index.js"></script>