-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sschonss/feature/add-api
Feature/add api
- Loading branch information
Showing
14 changed files
with
308 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
print_log.txt | ||
/.idea/.gitignore | ||
/.idea/auto-print.iml | ||
/.idea/modules.xml | ||
/.idea/vcs.xml | ||
/server/print_log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/sschonss/auto-print | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os/exec" | ||
) | ||
|
||
func printImage(w http.ResponseWriter, r *http.Request) { | ||
err := r.ParseForm() | ||
if err != nil { | ||
log.Println(err) | ||
fmt.Fprintf(w, "Error parsing form: %s\n", err) | ||
return | ||
} | ||
|
||
imageName := r.FormValue("image") | ||
copies := r.FormValue("copies") | ||
os := r.FormValue("os") | ||
|
||
fmt.Fprintf(w, "Printing image %s %s times...\n", imageName, copies) | ||
|
||
var cmd *exec.Cmd | ||
|
||
fmt.Println("OS:", os) | ||
|
||
if os == "linux" { | ||
fmt.Println("Linux detected") | ||
cmd = exec.Command("bash", "scripts/autoprinter.sh", imageName, copies) | ||
} else if os == "win" { | ||
fmt.Println("Windows detected") | ||
cmd = exec.Command("powershell", "-File", "scripts/autoprinter.ps1", imageName, copies) | ||
|
||
} else { | ||
cmd = exec.Command("echo", "OS not detected") | ||
fmt.Println("OS not detected") | ||
} | ||
|
||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
log.Println(err) | ||
fmt.Fprintf(w, "Error printing image: %s\n", err) | ||
fmt.Fprintf(w, "Output: %s\n", output) | ||
return | ||
} | ||
|
||
fmt.Fprintf(w, "Print request processed successfully.\n%s\n", output) | ||
} | ||
|
||
func explainAPI(w http.ResponseWriter, r *http.Request) { | ||
explanation := ` | ||
Welcome to the AutoPrint API! | ||
To print an image, send a POST request to /print with the following parameters: | ||
- image: Image name | ||
- copies: Number of copies | ||
- os: Operating System ("linux" or "win") | ||
Example: | ||
curl -X POST -d "image=123456789&copies=2&os=linux" http://localhost:8080/print | ||
` | ||
fmt.Fprintln(w, explanation) | ||
} | ||
|
||
func main() { | ||
fmt.Println("Starting server...") | ||
http.HandleFunc("/", explainAPI) | ||
http.HandleFunc("/print", printImage) | ||
fmt.Println("Listening on port 8080...") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
2024-01-08 08:36:28: Starting printing of 2 copies of file '../files/temp_123456.jpeg' (resized) on printer 'HL-L8350CDW' | ||
2024-01-08 08:36:28: Printing of 2 copies of file '../files/temp_123456.jpeg' (resized) on printer 'HL-L8350CDW' completed | ||
2024-01-08 08:36:41: Starting printing of 2 copies of file './files/temp_123456.jpeg' (resized) on printer 'HL-L8350CDW' | ||
id de requisição é HL-L8350CDW-33 (1 arquivo(s)) | ||
2024-01-08 08:36:41: Printing of 2 copies of file './files/temp_123456.jpeg' (resized) on printer 'HL-L8350CDW' completed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
param( | ||
[Parameter(Mandatory=$true)] | ||
[string]$image_name, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[int]$num_copies | ||
) | ||
|
||
$image_file = ".\files\$image_name.jpeg" | ||
|
||
if (-not (Test-Path $image_file)) { | ||
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): The image file '$image_file' was not found." | ||
exit 1 | ||
} | ||
|
||
$printer = Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Default=$true" | ||
$printerName = $printer.Name | ||
|
||
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): Starting printing of $num_copies copies of file '$image_file' on printer '$printerName'" | ||
|
||
for ($i = 0; $i -lt $num_copies; $i++) { | ||
Start-Process -FilePath "C:\Windows\System32\mspaint.exe" -ArgumentList "/pt `"$image_file`" `"$printerName`"" | ||
} | ||
|
||
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): Printing of $num_copies copies of file '$image_file' on printer '$printerName' completed" | ||
"Image '$image_file' sent for printing ($num_copies copies) to $printerName" | Out-File -Append -FilePath print_log.txt | ||
"Logs saved in print_log.txt" | Out-File -Append -FilePath print_log.txt |
Oops, something went wrong.