66//
77var fs = require ( "fs" ) ;
88var https = require ( 'https' ) ;
9+ var md5 = require ( 'md5' ) ;
910
1011
1112function detect_board ( ) {
@@ -101,13 +102,15 @@ console.log(file_url);
101102
102103 } ) ;
103104
104- if ( isRemote ) {
105- download_firmware ( obj , function ( ) {
106- //upload_firmware(file_url, "/dev/sdb");
107- } ) ;
108- } else {
109- upload_firmware ( file_url , "/dev/sdb" ) ;
110- }
105+ $div . find ( "#upload" ) . click ( function ( ) {
106+ if ( isRemote ) {
107+ download_firmware ( obj , function ( ) {
108+ //upload_firmware(file_url, "/dev/sdb");
109+ } ) ;
110+ } else {
111+ upload_firmware ( file_url , "/dev/sdb" ) ;
112+ }
113+ } ) ;
111114 } ) ;
112115}
113116
@@ -132,15 +135,35 @@ function download_firmware(obj, callback) {
132135 fs . mkdirSync ( "./tmp" ) ;
133136 }
134137
138+ var url = get_remote_firmware_url ( obj . name ) ;
135139 var path = process . cwd ( ) + "/tmp/" + obj . name + ".bin" ;
136- console . log ( "saving in " + path ) ;
137- console . log ( obj ) ;
140+ console . log ( "saving from: " + url ) ;
141+ console . log ( "saving to " + path ) ;
138142
139143 var file = fs . createWriteStream ( path ) ;
140144
141- var request = https . get ( get_remote_firmware_url ( obj . name ) , function ( response ) {
145+ var request = https . get ( url , function ( response ) {
146+ console . log ( response ) ;
142147 response . pipe ( file ) ;
143- callback ( ) ;
148+
149+ file . on ( 'finish' , function ( d ) {
150+ //check md5
151+ var md5result ;
152+ fs . readFile ( path , function ( err , buf ) {
153+ if ( md5 ( buf ) == obj . md5 ) {
154+ console . log ( "ok" ) ;
155+ md5result = true ;
156+ } else {
157+ console . log ( "nop" ) ;
158+ md5result = false ;
159+ }
160+ callback ( md5result ) ;
161+ } ) ; //readfile
162+ } ) ; //fileonfinish
163+
164+ } ) . on ( 'error' , function ( e ) {
165+ fs . unlink ( dest ) ;
166+ console . log ( e ) ;
144167 } ) ;
145168}
146169
0 commit comments