Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Payload struct {
File io.Reader
Name string
Size int64
Print bool
}

func (p *Payload) SetName(name string) {
Expand All @@ -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,
}
}

Expand Down
12 changes: 11 additions & 1 deletion connector_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
5 changes: 4 additions & 1 deletion octoprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,17 @@ 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 {
argumentsFromApi(apiKey)
}

// 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())
Expand Down