chore: uncommitted changes auto-commit
This commit is contained in:
parent
fbd5c87dbf
commit
32b97d4f27
63
main.go
63
main.go
|
|
@ -10,9 +10,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -56,21 +58,7 @@ func findLatestScreenshot(desktop string) (string, error) {
|
||||||
return filepath.Join(desktop, screenshots[0].Name()), nil
|
return filepath.Join(desktop, screenshots[0].Name()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func screenshotHandler(w http.ResponseWriter, r *http.Request) {
|
func serveAndDelete(w http.ResponseWriter, path string) {
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.Error(w, "GET only", http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
desktop := getDesktopPath()
|
|
||||||
path, err := findLatestScreenshot(desktop)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
|
||||||
log.Println("No screenshot:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open and serve
|
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Cannot open file", http.StatusInternalServerError)
|
http.Error(w, "Cannot open file", http.StatusInternalServerError)
|
||||||
|
|
@ -88,7 +76,6 @@ func screenshotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete after successful send
|
|
||||||
f.Close()
|
f.Close()
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
log.Println("Warning: could not delete:", err)
|
log.Println("Warning: could not delete:", err)
|
||||||
|
|
@ -97,18 +84,62 @@ func screenshotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func screenshotHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "GET only", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
desktop := getDesktopPath()
|
||||||
|
path, err := findLatestScreenshot(desktop)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
log.Println("No screenshot:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
serveAndDelete(w, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func captureHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
display := r.URL.Query().Get("display")
|
||||||
|
if display == "" {
|
||||||
|
display = "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpFile := filepath.Join(os.TempDir(), fmt.Sprintf("capture-%d.png", time.Now().UnixNano()))
|
||||||
|
|
||||||
|
cmd := exec.Command("screencapture", "-x", "-D", display, tmpFile)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("capture failed: %v: %s", err, out), http.StatusInternalServerError)
|
||||||
|
log.Printf("Capture failed (display %s): %v: %s", display, err, out)
|
||||||
|
os.Remove(tmpFile)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Captured display %s", display)
|
||||||
|
serveAndDelete(w, tmpFile)
|
||||||
|
}
|
||||||
|
|
||||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("ok"))
|
w.Write([]byte("ok"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/screenshot", screenshotHandler)
|
http.HandleFunc("/screenshot", screenshotHandler)
|
||||||
|
http.HandleFunc("/capture", captureHandler)
|
||||||
http.HandleFunc("/health", healthHandler)
|
http.HandleFunc("/health", healthHandler)
|
||||||
|
|
||||||
log.Printf("Screenshot server starting on %s", port)
|
log.Printf("Screenshot server starting on %s", port)
|
||||||
log.Printf("Desktop: %s", getDesktopPath())
|
log.Printf("Desktop: %s", getDesktopPath())
|
||||||
log.Printf("Endpoints:")
|
log.Printf("Endpoints:")
|
||||||
log.Printf(" GET /screenshot - fetch & delete latest screenshot")
|
log.Printf(" GET /screenshot - fetch & delete latest screenshot")
|
||||||
|
log.Printf(" POST /capture?display=N - take screenshot of display N")
|
||||||
log.Printf(" GET /health - health check")
|
log.Printf(" GET /health - health check")
|
||||||
|
|
||||||
if err := http.ListenAndServe(port, nil); err != nil {
|
if err := http.ListenAndServe(port, nil); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue