Compare commits

...

2 Commits

Author SHA1 Message Date
8b3f1933a5 add files yay :3 2024-10-31 21:03:43 +01:00
73eaf395c7 new code yay :3 2024-10-31 20:44:19 +01:00
5 changed files with 88 additions and 112 deletions

View File

@ -1,6 +1,6 @@
# S3 Client Web # HP ePrint like service clone (web edition)
This is a WebGUI for [s3-client](https://git.fluffy.pw/leafus/s3-client) 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
## Prerequisites ## Prerequisites
@ -10,17 +10,17 @@ This is a WebGUI for [s3-client](https://git.fluffy.pw/leafus/s3-client)
1. Clone the repository: 1. Clone the repository:
``` ```
git clone https://git.fluffy.pw/leafus/s3-client-web git clone https://git.fluffy.pw/matu6968/web-hp-eprint-clone
``` ```
2. Go to the project directory: 2. Go to the project directory:
``` ```
cd s3-client-web cd web-hp-eprint-clone
``` ```
3. Build the binary: 3. Build the binary:
``` ```
go build -o s3-client-web go build -o web-hp-eprint
``` ```
## Configuration ## Configuration
@ -29,10 +29,25 @@ 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
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 Setup a printer on your host by connecting it over USB or WiFi and then adding the printer in your OS
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

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

63
main.go
View File

@ -20,6 +20,11 @@ func main() {
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"
@ -68,9 +73,9 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
} }
defer file.Close() defer file.Close()
directory := r.FormValue("directory") targetmail := r.FormValue("targetmail")
if directory == "" { if targetmail == "" {
directory = "/" fmt.Printf("Warning: User did not specify a mail on sent request")
} }
tempFileName := filepath.Join(os.TempDir(), handler.Filename) tempFileName := filepath.Join(os.TempDir(), handler.Filename)
@ -88,17 +93,16 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
return 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") printingcmdPath := filepath.Join("/", "usr", "bin", "eprintcloned")
filelog := os.Getenv("LOG")
cmd := exec.Command(printingcmdPath, tempFile.Name(), filelog, targetmail)
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { 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{ 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 return
} }
@ -107,7 +111,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 uploaded successfully", handler.Filename), Message: fmt.Sprintf("File %s printed successfully", handler.Filename),
Output: string(output), Output: string(output),
} }
@ -116,43 +120,6 @@ 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. "})
if r.Method != http.MethodPost {
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
return 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...'; uploadStatus.textContent = 'Uploading and printing...';
commandOutput.textContent = ''; commandOutput.textContent = '';
const formData = new FormData(uploadForm); const formData = new FormData(uploadForm);
@ -27,33 +27,3 @@ 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>S3-Client Web Wrapper</title> <title>HP ePrint clone Web Wrapper</title>
<style> <style>
#uploadStatus, #uploadStatus,
#commandOutput { #commandOutput {
@ -13,16 +13,16 @@
</style> </style>
</head> </head>
<body> <body>
<div class="header">S3-Client WebGUI</div> <div class="header">HP ePrint like printing WebGUI</div>
<fieldset> <fieldset>
<legend>File Selection</legend> <legend>File Selection</legend>
<form id="uploadForm"> <form id="uploadForm">
<input type="file" name="file" required /> <input type="file" accept=".jpg, .jpeg, .png, .gif, .txt, .pdf" name="file" required />
<input <input
type="text" type="text"
name="directory" name="targetmail"
placeholder="Directory to upload to [Optional]" placeholder="Confirmation e-mail after printing finishes [Optional]"
style="width: 210px;" style="width: 210px;"
/> />
<br /> <br />
@ -30,20 +30,6 @@
</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>
@ -55,8 +41,8 @@
</fieldset> </fieldset>
<div style="margin-top: 10px;" class="footer"> <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/matu6968/web-hp-eprint-clone">[ web-hp-eprint-clone ]</a> -
<a href="https://git.fluffy.pw/leafus/s3-client-web">[ s3-client-web ]</a> - Licensed under MIT <a href="https://git.fluffy.pw/leafus/s3-client-web">[ forked of s3-client-web ]</a> - Licensed under MIT
</div> </div>
<script src="/assets/index.js"></script> <script src="/assets/index.js"></script>