1
+ #include " common.h"
1
2
2
3
// holds the current upload
3
4
File fsUploadFile;
@@ -34,12 +35,15 @@ String getContentType(String filename){
34
35
35
36
bool handleFileRead (String path){
36
37
Serial.println (" handleFileRead: " + path);
37
- if (path .endsWith ("/" )) path += "index.htm" ;
38
+ if (path.endsWith (" /" )) {
39
+ path += " index.htm" ;
40
+ }
38
41
String contentType = getContentType (path);
39
42
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
+ }
43
47
File file = MYFS.open (path, " r" );
44
48
(void )webServer.streamFile (file, contentType);
45
49
file.close ();
@@ -50,68 +54,87 @@ bool handleFileRead(String path){
50
54
}
51
55
52
56
void handleFileUpload (){
53
- if (webServer .uri () != "/edit" ) return ;
57
+ if (webServer.uri () != " /edit" ) return ;
58
+
54
59
HTTPUpload& upload = webServer.upload ();
55
- if (upload .status == UPLOAD_FILE_START ){
60
+ if (upload.status == UPLOAD_FILE_START) {
56
61
String filename = upload.filename ;
57
- if (!filename .startsWith ("/" )) filename = "/" + filename ;
62
+ if (!filename.startsWith (" /" )) {
63
+ filename = " /" + filename;
64
+ }
58
65
Serial.print (" handleFileUpload Name: " ); Serial.println (filename);
59
66
fsUploadFile = MYFS.open (filename, " w" );
60
67
filename = String ();
61
- } else if (upload .status == UPLOAD_FILE_WRITE ){
68
+ } else if (upload.status == UPLOAD_FILE_WRITE) {
62
69
// Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
63
- if (fsUploadFile )
70
+ if (fsUploadFile) {
64
71
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) {
67
75
fsUploadFile.close ();
76
+ }
68
77
Serial.print (" handleFileUpload Size: " ); Serial.println (upload.totalSize );
69
78
}
70
79
}
71
80
72
81
void 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
+
74
86
String path = webServer.arg (0 );
75
87
Serial.println (" handleFileDelete: " + path);
76
- if (path == "/" )
88
+ if (path == " /" ) {
77
89
return webServer.send (500 , " text/plain" , " BAD PATH" );
78
- if (!MYFS .exists (path ))
90
+ }
91
+ if (!MYFS.exists (path)) {
79
92
return webServer.send (404 , " text/plain" , " FileNotFound" );
93
+ }
80
94
MYFS.remove (path);
81
95
webServer.send (200 , " text/plain" , " " );
82
96
path = String ();
83
97
}
84
98
85
99
void handleFileCreate (){
86
- if (webServer .args () == 0 )
100
+ if (webServer.args () == 0 ) {
87
101
return webServer.send (500 , " text/plain" , " BAD ARGS" );
102
+ }
88
103
String path = webServer.arg (0 );
89
104
Serial.println (" handleFileCreate: " + path);
90
- if (path == "/" )
105
+ if (path == " /" ) {
91
106
return webServer.send (500 , " text/plain" , " BAD PATH" );
92
- if (MYFS .exists (path ))
107
+ }
108
+ if (MYFS.exists (path)) {
93
109
return webServer.send (500 , " text/plain" , " FILE EXISTS" );
110
+ }
94
111
File file = MYFS.open (path, " w" );
95
- if (file )
112
+ if (file) {
96
113
file.close ();
97
- else
114
+ } else {
98
115
return webServer.send (500 , " text/plain" , " CREATE FAILED" );
116
+ }
99
117
webServer.send (200 , " text/plain" , " " );
100
118
path = String ();
101
119
}
102
120
103
121
void 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
+ }
105
126
106
127
String path = webServer.arg (" dir" );
107
128
Serial.println (" handleFileList: " + path);
108
129
Dir dir = MYFS.openDir (path);
109
130
path = String ();
110
131
111
132
String output = " [" ;
112
- while (dir .next ()){
133
+ while (dir.next ()) {
113
134
File entry = dir.openFile (" r" );
114
- if (output != "[" ) output += ',' ;
135
+ if (output != " [" ) {
136
+ output += ' ,' ;
137
+ }
115
138
bool isDir = false ;
116
139
// bool isDir = entry.isDirectory();
117
140
0 commit comments