forked from macdylan/sm2uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
octoprint.go
116 lines (99 loc) · 2.94 KB
/
octoprint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"log"
"net"
"net/http"
"strings"
"time"
)
const (
maxMemory = 64 << 20 // 64MB
)
var (
noTrim = false
noShutoff = false
noPreheat = false
noReinforceTower = false
)
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {
log.Printf("Request %s %s completed in %v", r.Method, r.URL.Path, time.Since(start))
}()
next.ServeHTTP(w, r)
})
}
func startOctoPrintServer(listenAddr string, printer *Printer) error {
mux := http.NewServeMux()
mux.HandleFunc("/api/version", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"api": "0.1", "server": "1.2.3", "text": "OctoPrint 1.2.3/Dummy"}`))
})
mux.HandleFunc("/api/files/local", func(w http.ResponseWriter, r *http.Request) {
// Check if request is a POST request
if r.Method != http.MethodPost {
log.Print("Method not allowed: ", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseMultipartForm(maxMemory); err != nil {
log.Print("Parse form error: ", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Retrieve the uploaded file
file, fd, err := r.FormFile("file")
if err != nil {
log.Print("Error retrieving file: ", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
// read X-Api-Key header
apiKey := r.Header.Get("X-Api-Key")
if len(apiKey) > 5 {
noTrim = strings.Contains(apiKey, "notrim")
noPreheat = strings.Contains(apiKey, "nopreheat")
noShutoff = strings.Contains(apiKey, "noshutoff")
noReinforceTower = strings.Contains(apiKey, "noreinforcetower")
msg := make([]string, 0, 3)
if noTrim {
msg = append(msg, "-notrim")
}
if noPreheat {
msg = append(msg, "-nopreheat")
}
if noShutoff {
msg = append(msg, "-noshutoff")
}
if noReinforceTower {
msg = append(msg, "-noreinforcetower")
}
if len(msg) > 0 {
log.Printf("SMFix with args: %s", strings.Join(msg, " "))
}
}
// Send the stream to the printer
payload := NewPayload(file, fd.Filename, fd.Size)
if err := Connector.Upload(printer, payload); err != nil {
log.Print("Error uploading file: ", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Upload finished: %s [%s]", fd.Filename, payload.ReadableSize())
// Return success response
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"done": true}`))
})
handler := LoggingMiddleware(mux)
log.Printf("Starting OctoPrint server on %s ...", listenAddr)
// Create a listener
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
return err
}
log.Printf("Server started, now you can upload files to http://%s", listener.Addr().String())
// Start the server
return http.Serve(listener, handler)
}