From 6c895a8887756a92f141580c433798015f6225a8 Mon Sep 17 00:00:00 2001 From: Chris Nesbitt-Smith Date: Tue, 21 Jan 2025 15:45:42 +0000 Subject: [PATCH] Upload and start print automatically --- connector.go | 4 +++- connector_http.go | 12 +++++++++++- main.go | 2 +- octoprint.go | 5 ++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/connector.go b/connector.go index 5bde38c..462ba8d 100644 --- a/connector.go +++ b/connector.go @@ -21,6 +21,7 @@ type Payload struct { File io.Reader Name string Size int64 + Print bool } func (p *Payload) SetName(name string) { @@ -45,11 +46,12 @@ func (p *Payload) ShouldBeFix() bool { return shouldBeFix(p.Name) } -func NewPayload(file io.Reader, name string, size int64) *Payload { +func NewPayload(file io.Reader, name string, size int64, print bool) *Payload { return &Payload{ File: file, Name: normalizedFilename(name), Size: size, + Print: print, } } diff --git a/connector_http.go b/connector_http.go index 29961ae..04aee27 100644 --- a/connector_http.go +++ b/connector_http.go @@ -186,7 +186,17 @@ func (hc *HTTPConnector) Upload(payload *Payload) (err error) { } }, 35*time.Millisecond) - _, err = r.Post(hc.URL("/upload")) + if payload.Print { + _, err = r.Post(hc.URL("/prepare_print")) + if err == nil { + log.Printf("Print job prepared: %s", err) + startPrintRequest := hc.request(0) + startPrintRequest.SetFormData(map[string]string{"type": "3DP"}) + _, err = startPrintRequest.Post(hc.URL("/start_print")) + } + } else { + _, err = r.Post(hc.URL("/upload")) + } return } diff --git a/main.go b/main.go index f5f679d..bab33a2 100644 --- a/main.go +++ b/main.go @@ -183,7 +183,7 @@ func main() { log.Panicf("File %s does not exist\n", file) } else { f, _ := os.Open(file) - _Payloads = append(_Payloads, NewPayload(f, st.Name(), st.Size())) + _Payloads = append(_Payloads, NewPayload(f, st.Name(), st.Size(), false)) } } diff --git a/octoprint.go b/octoprint.go index a8d088f..bd77cd3 100644 --- a/octoprint.go +++ b/octoprint.go @@ -126,6 +126,9 @@ func startOctoPrintServer(listenAddr string, printer *Printer) error { } defer file.Close() + // Get print parameter if they upload+print the file + startPrint := r.FormValue("print") == "true" + // read X-Api-Key header apiKey := r.Header.Get("X-Api-Key") if len(apiKey) > 5 { @@ -133,7 +136,7 @@ func startOctoPrintServer(listenAddr string, printer *Printer) error { } // Send the stream to the printer - payload := NewPayload(file, fd.Filename, fd.Size) + payload := NewPayload(file, fd.Filename, fd.Size, startPrint) if err := Connector.Upload(printer, payload); err != nil { _stats.addFailure(payload.Name, payload.Size) internalServerErrorResponse(w, err.Error())