@@ -79,116 +79,119 @@ type Upload struct {
7979
8080var uploadStatusStr = "ProgrammerStatus"
8181
82- func uploadHandler (c * gin.Context ) {
83- data := new (Upload )
84- if err := c .BindJSON (data ); err != nil {
85- c .String (http .StatusBadRequest , fmt .Sprintf ("err with the payload. %v" , err .Error ()))
86- return
87- }
88-
89- log .Printf ("%+v %+v %+v %+v %+v %+v" , data .Port , data .Board , data .Rewrite , data .Commandline , data .Extra , data .Filename )
90-
91- if data .Port == "" {
92- c .String (http .StatusBadRequest , "port is required" )
93- return
94- }
95-
96- if data .Board == "" {
97- c .String (http .StatusBadRequest , "board is required" )
98- log .Error ("board is required" )
99- return
100- }
101-
102- if ! data .Extra .Network {
103- if data .Signature == "" {
104- c .String (http .StatusBadRequest , "signature is required" )
82+ func UploadHandler (h * hub ) func (c * gin.Context ) {
83+ return func (c * gin.Context ) {
84+ data := new (Upload )
85+ if err := c .BindJSON (data ); err != nil {
86+ c .String (http .StatusBadRequest , fmt .Sprintf ("err with the payload. %v" , err .Error ()))
10587 return
10688 }
10789
108- if data .Commandline == "" {
109- c .String (http .StatusBadRequest , "commandline is required for local board" )
90+ log .Printf ("%+v %+v %+v %+v %+v %+v" , data .Port , data .Board , data .Rewrite , data .Commandline , data .Extra , data .Filename )
91+
92+ if data .Port == "" {
93+ c .String (http .StatusBadRequest , "port is required" )
11094 return
11195 }
11296
113- err := utilities .VerifyInput (data .Commandline , data .Signature )
114-
115- if err != nil {
116- c .String (http .StatusBadRequest , "signature is invalid" )
97+ if data .Board == "" {
98+ c .String (http .StatusBadRequest , "board is required" )
99+ log .Error ("board is required" )
117100 return
118101 }
119- }
120102
121- buffer := bytes .NewBuffer (data .Hex )
103+ if ! data .Extra .Network {
104+ if data .Signature == "" {
105+ c .String (http .StatusBadRequest , "signature is required" )
106+ return
107+ }
122108
123- filePath , err := utilities .SaveFileonTempDir (data .Filename , buffer )
124- if err != nil {
125- c .String (http .StatusBadRequest , err .Error ())
126- return
127- }
109+ if data .Commandline == "" {
110+ c .String (http .StatusBadRequest , "commandline is required for local board" )
111+ return
112+ }
128113
129- tmpdir , err := os .MkdirTemp ("" , "extrafiles" )
130- if err != nil {
131- c .String (http .StatusBadRequest , err .Error ())
132- return
133- }
114+ err := utilities .VerifyInput (data .Commandline , data .Signature )
134115
135- for _ , extraFile := range data .ExtraFiles {
136- path , err := utilities .SafeJoin (tmpdir , extraFile .Filename )
137- if err != nil {
138- c .String (http .StatusBadRequest , err .Error ())
139- return
116+ if err != nil {
117+ c .String (http .StatusBadRequest , "signature is invalid" )
118+ return
119+ }
140120 }
141- log .Printf ("Saving %s on %s" , extraFile .Filename , path )
142121
143- err = os .MkdirAll (filepath .Dir (path ), 0744 )
144- if err != nil {
145- c .String (http .StatusBadRequest , err .Error ())
146- return
147- }
122+ buffer := bytes .NewBuffer (data .Hex )
148123
149- err = os . WriteFile ( path , extraFile . Hex , 0644 )
124+ filePath , err := utilities . SaveFileonTempDir ( data . Filename , buffer )
150125 if err != nil {
151126 c .String (http .StatusBadRequest , err .Error ())
152127 return
153128 }
154- }
155129
156- if data .Rewrite != "" {
157- data .Board = data .Rewrite
158- }
159-
160- go func () {
161- // Resolve commandline
162- commandline , err := upload .PartiallyResolve (data .Board , filePath , tmpdir , data .Commandline , data .Extra , Tools )
130+ tmpdir , err := os .MkdirTemp ("" , "extrafiles" )
163131 if err != nil {
164- send ( map [ string ] string { uploadStatusStr : "Error" , "Msg" : err .Error ()} )
132+ c . String ( http . StatusBadRequest , err .Error ())
165133 return
166134 }
167135
168- l := PLogger {Verbose : true }
169-
170- // Upload
171- if data .Extra .Network {
172- err = errors .New ("network upload is not supported anymore, pease use OTA instead" )
173- } else {
174- send (map [string ]string {uploadStatusStr : "Starting" , "Cmd" : "Serial" })
175- err = upload .Serial (data .Port , commandline , data .Extra , l )
136+ for _ , extraFile := range data .ExtraFiles {
137+ path , err := utilities .SafeJoin (tmpdir , extraFile .Filename )
138+ if err != nil {
139+ c .String (http .StatusBadRequest , err .Error ())
140+ return
141+ }
142+ log .Printf ("Saving %s on %s" , extraFile .Filename , path )
143+
144+ err = os .MkdirAll (filepath .Dir (path ), 0744 )
145+ if err != nil {
146+ c .String (http .StatusBadRequest , err .Error ())
147+ return
148+ }
149+
150+ err = os .WriteFile (path , extraFile .Hex , 0644 )
151+ if err != nil {
152+ c .String (http .StatusBadRequest , err .Error ())
153+ return
154+ }
176155 }
177156
178- // Handle result
179- if err != nil {
180- send (map [string ]string {uploadStatusStr : "Error" , "Msg" : err .Error ()})
181- return
157+ if data .Rewrite != "" {
158+ data .Board = data .Rewrite
182159 }
183- send (map [string ]string {uploadStatusStr : "Done" , "Flash" : "Ok" })
184- }()
185160
186- c .String (http .StatusAccepted , "" )
161+ go func () {
162+ // Resolve commandline
163+ commandline , err := upload .PartiallyResolve (data .Board , filePath , tmpdir , data .Commandline , data .Extra , Tools )
164+ if err != nil {
165+ send (h , map [string ]string {uploadStatusStr : "Error" , "Msg" : err .Error ()})
166+ return
167+ }
168+
169+ l := PLogger {Verbose : true }
170+
171+ // Upload
172+ if data .Extra .Network {
173+ err = errors .New ("network upload is not supported anymore, pease use OTA instead" )
174+ } else {
175+ send (h , map [string ]string {uploadStatusStr : "Starting" , "Cmd" : "Serial" })
176+ err = upload .Serial (data .Port , commandline , data .Extra , l )
177+ }
178+
179+ // Handle result
180+ if err != nil {
181+ send (h , map [string ]string {uploadStatusStr : "Error" , "Msg" : err .Error ()})
182+ return
183+ }
184+ send (h , map [string ]string {uploadStatusStr : "Done" , "Flash" : "Ok" })
185+ }()
186+
187+ c .String (http .StatusAccepted , "" )
188+ }
187189}
188190
189191// PLogger sends the info from the upload to the websocket
190192type PLogger struct {
191193 Verbose bool
194+ h * hub
192195}
193196
194197// Debug only sends messages if verbose is true (always true for now)
@@ -202,15 +205,15 @@ func (l PLogger) Debug(args ...interface{}) {
202205func (l PLogger ) Info (args ... interface {}) {
203206 output := fmt .Sprint (args ... )
204207 log .Println (output )
205- send (map [string ]string {uploadStatusStr : "Busy" , "Msg" : output })
208+ send (l . h , map [string ]string {uploadStatusStr : "Busy" , "Msg" : output })
206209}
207210
208- func send (args map [string ]string ) {
211+ func send (h * hub , args map [string ]string ) {
209212 mapB , _ := json .Marshal (args )
210213 h .broadcastSys <- mapB
211214}
212215
213- func wsHandler () * WsServer {
216+ func wsHandler (h * hub ) * WsServer {
214217 server , err := socketio .NewServer (nil )
215218 if err != nil {
216219 log .Fatal (err )
0 commit comments