1+ #include " common.h"
12
23// holds the current upload
34File fsUploadFile;
@@ -34,12 +35,15 @@ String getContentType(String filename){
3435
3536bool handleFileRead (String path){
3637 Serial.println (" handleFileRead: " + path);
37- if (path .endsWith ("/" )) path += "index.htm" ;
38+ if (path.endsWith (" /" )) {
39+ path += " index.htm" ;
40+ }
3841 String contentType = getContentType (path);
3942 String pathWithGz = path + " .gz" ;
40- if (MYFS .exists (pathWithGz ) || MYFS .exists (path )){
41- if (MYFS .exists (pathWithGz ))
42- path += ".gz" ;
43+ if (MYFS.exists (pathWithGz) || MYFS.exists (path)) {
44+ if (MYFS.exists (pathWithGz)) {
45+ path = pathWithGz;
46+ }
4347 File file = MYFS.open (path, " r" );
4448 (void )webServer.streamFile (file, contentType);
4549 file.close ();
@@ -50,68 +54,87 @@ bool handleFileRead(String path){
5054}
5155
5256void handleFileUpload (){
53- if (webServer .uri () != "/edit" ) return ;
57+ if (webServer.uri () != " /edit" ) return ;
58+
5459 HTTPUpload& upload = webServer.upload ();
55- if (upload .status == UPLOAD_FILE_START ){
60+ if (upload.status == UPLOAD_FILE_START) {
5661 String filename = upload.filename ;
57- if (!filename .startsWith ("/" )) filename = "/" + filename ;
62+ if (!filename.startsWith (" /" )) {
63+ filename = " /" + filename;
64+ }
5865 Serial.print (" handleFileUpload Name: " ); Serial.println (filename);
5966 fsUploadFile = MYFS.open (filename, " w" );
6067 filename = String ();
61- } else if (upload .status == UPLOAD_FILE_WRITE ){
68+ } else if (upload.status == UPLOAD_FILE_WRITE) {
6269 // Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
63- if (fsUploadFile )
70+ if (fsUploadFile) {
6471 fsUploadFile.write (upload.buf , upload.currentSize );
65- } else if (upload .status == UPLOAD_FILE_END ){
66- if (fsUploadFile )
72+ }
73+ } else if (upload.status == UPLOAD_FILE_END) {
74+ if (fsUploadFile) {
6775 fsUploadFile.close ();
76+ }
6877 Serial.print (" handleFileUpload Size: " ); Serial.println (upload.totalSize );
6978 }
7079}
7180
7281void handleFileDelete (){
73- if (webServer .args () == 0 ) return webServer .send (500 , "text/plain" , "BAD ARGS" );
82+ if (webServer.args () == 0 ) {
83+ return webServer.send (500 , " text/plain" , " BAD ARGS" );
84+ }
85+
7486 String path = webServer.arg (0 );
7587 Serial.println (" handleFileDelete: " + path);
76- if (path == "/" )
88+ if (path == " /" ) {
7789 return webServer.send (500 , " text/plain" , " BAD PATH" );
78- if (!MYFS .exists (path ))
90+ }
91+ if (!MYFS.exists (path)) {
7992 return webServer.send (404 , " text/plain" , " FileNotFound" );
93+ }
8094 MYFS.remove (path);
8195 webServer.send (200 , " text/plain" , " " );
8296 path = String ();
8397}
8498
8599void handleFileCreate (){
86- if (webServer .args () == 0 )
100+ if (webServer.args () == 0 ) {
87101 return webServer.send (500 , " text/plain" , " BAD ARGS" );
102+ }
88103 String path = webServer.arg (0 );
89104 Serial.println (" handleFileCreate: " + path);
90- if (path == "/" )
105+ if (path == " /" ) {
91106 return webServer.send (500 , " text/plain" , " BAD PATH" );
92- if (MYFS .exists (path ))
107+ }
108+ if (MYFS.exists (path)) {
93109 return webServer.send (500 , " text/plain" , " FILE EXISTS" );
110+ }
94111 File file = MYFS.open (path, " w" );
95- if (file )
112+ if (file) {
96113 file.close ();
97- else
114+ } else {
98115 return webServer.send (500 , " text/plain" , " CREATE FAILED" );
116+ }
99117 webServer.send (200 , " text/plain" , " " );
100118 path = String ();
101119}
102120
103121void handleFileList () {
104- if (!webServer .hasArg ("dir" )) {webServer .send (500 , "text/plain" , "BAD ARGS" ); return ;}
122+ if (!webServer.hasArg (" dir" )) {
123+ webServer.send (500 , " text/plain" , " BAD ARGS" );
124+ return ;
125+ }
105126
106127 String path = webServer.arg (" dir" );
107128 Serial.println (" handleFileList: " + path);
108129 Dir dir = MYFS.openDir (path);
109130 path = String ();
110131
111132 String output = " [" ;
112- while (dir .next ()){
133+ while (dir.next ()) {
113134 File entry = dir.openFile (" r" );
114- if (output != "[" ) output += ',' ;
135+ if (output != " [" ) {
136+ output += ' ,' ;
137+ }
115138 bool isDir = false ;
116139 // bool isDir = entry.isDirectory();
117140
0 commit comments