filesystem.ino 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. void FS_format()
  2. {
  3. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_formatting);
  4. sendLog(logBuf, LOGLEVEL_INFO);
  5. LittleFS.format();
  6. sendLog(F("FS formatted"));
  7. File f = LittleFS.open("/formatted", "w");
  8. if (!f)
  9. {
  10. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_formatFailed);
  11. sendLog(logBuf, LOGLEVEL_INFO);
  12. }
  13. else {
  14. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_formatComplete);
  15. sendLog(logBuf, LOGLEVEL_INFO);
  16. }
  17. f.close();
  18. }
  19. void FS_formatIfIsnt()
  20. {
  21. if (!LittleFS.exists("/formatted"))
  22. {
  23. FS_format();
  24. }
  25. }
  26. void FS_listFiles()
  27. {
  28. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_ls);
  29. sendLog(logBuf);
  30. Dir _dir = LittleFS.openDir("/");
  31. while (_dir.next())
  32. {
  33. File _f = _dir.openFile("r");
  34. uint16_t _size = _f.size();
  35. uint8_t _fnLen = strlen(_dir.fileName().c_str());
  36. char _spaces[42];
  37. uint8_t _iMax = 0;
  38. for(uint8_t _i=0; _i < (31 - _fnLen); _i++) {
  39. _spaces[_i] = ' ';
  40. _iMax = _i;
  41. }
  42. if(_size < 10) {
  43. _spaces[_iMax +1] = ' ';
  44. _spaces[_iMax +2] = ' ';
  45. _spaces[_iMax +3] = ' ';
  46. _iMax += 3;
  47. }
  48. else if(_size < 100) {
  49. _spaces[_iMax +1] = ' ';
  50. _spaces[_iMax +2] = ' ';
  51. _iMax += 2;
  52. }
  53. else if(_size < 1000) {
  54. _spaces[_iMax +1] = ' ';
  55. _iMax += 1;
  56. }
  57. _spaces[_iMax +1] = '\0';
  58. snprintf(logBuf, LOG_BUFFER_SIZE, " %s%s%d ", _dir.fileName().c_str(), _spaces, _size);
  59. sendLog(logBuf);
  60. _f.close();
  61. }
  62. FS_getInfo();
  63. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_ls_info, fs_info.usedBytes, fs_info.totalBytes);
  64. sendLog(logBuf);
  65. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_console_line_hr);
  66. sendLog(logBuf);
  67. }
  68. // code snippet from https://42project.net/esp8266-webserverinhalte-wie-bilder-png-und-jpeg-aus-dem-internen-flash-speicher-laden/
  69. bool FS_downloadFile(String _path, bool _download=false){
  70. String _dataType = "text/plain";
  71. //if(path.endsWith("/")) path += "index.htm";
  72. //if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  73. if(_download) _dataType = "application/octet-stream";
  74. else if(_path.endsWith(".html")) _dataType = "text/html";
  75. else if(_path.endsWith(".htm")) _dataType = "text/html";
  76. else if(_path.endsWith(".css")) _dataType = "text/css";
  77. else if(_path.endsWith(".js")) _dataType = "application/javascript";
  78. else if(_path.endsWith(".json")) _dataType = "application/json";
  79. else if(_path.endsWith(".png")) _dataType = "image/png";
  80. else if(_path.endsWith(".gif")) _dataType = "image/gif";
  81. else if(_path.endsWith(".jpg")) _dataType = "image/jpeg";
  82. else if(_path.endsWith(".ico")) _dataType = "image/x-icon";
  83. else if(_path.endsWith(".xml")) _dataType = "text/xml";
  84. else if(_path.endsWith(".pdf")) _dataType = "application/pdf";
  85. else if(_path.endsWith(".zip")) _dataType = "application/zip";
  86. else if(_path.endsWith(".hex")) _dataType = "application/octet-stream";
  87. else if(_path.endsWith(".bin")) _dataType = "application/octet-stream";
  88. File _dataFile = LittleFS.open(_path.c_str(), "r");
  89. // send filename header
  90. char _contentHeader[71];
  91. sprintf(_contentHeader, "attachment; filename=\"%s\";", _path.c_str());
  92. httpServer.sendHeader("Content-Disposition", _contentHeader);
  93. if (httpServer.streamFile(_dataFile, _dataType) != _dataFile.size()) {
  94. }
  95. _dataFile.close();
  96. return true;
  97. }
  98. bool FS_deleteFile(String _path) {
  99. if(LittleFS.exists(_path)) {
  100. LittleFS.remove(_path);
  101. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_del, _path.c_str());
  102. sendLog(logBuf);
  103. return true;
  104. }
  105. else {
  106. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_del_error, _path.c_str());
  107. sendLog(logBuf);
  108. return false;
  109. }
  110. }
  111. bool FS_renameFile(String _pathFrom, String _pathTo) {
  112. if(LittleFS.exists(_pathFrom)) {
  113. LittleFS.rename(_pathFrom, _pathTo);
  114. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_ren, _pathFrom.c_str(), _pathTo.c_str());
  115. sendLog(logBuf);
  116. return true;
  117. }
  118. else {
  119. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_error_notFound, _pathFrom.c_str());
  120. sendLog(logBuf);
  121. return false;
  122. }
  123. }
  124. // code snippet from https://tttapa.github.io/ESP8266/Chap12%20-%20Uploading%20to%20Server.html
  125. File fsUploadFile; // a File object to temporarily store the received file
  126. void FS_handleFileUpload(){ // upload a new file to the FS
  127. HTTPUpload& _upload = httpServer.upload();
  128. if(_upload.status == UPLOAD_FILE_START) {
  129. String _filename = _upload.filename;
  130. if(!_filename.startsWith("/")) _filename = "/"+_filename;
  131. fsUploadFile = LittleFS.open(_filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
  132. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_uploadStarted, _filename.c_str());
  133. sendLog(logBuf);
  134. _filename = String();
  135. }
  136. else if(_upload.status == UPLOAD_FILE_WRITE) {
  137. if(fsUploadFile)
  138. fsUploadFile.write(_upload.buf, _upload.currentSize); // Write the received bytes to the file
  139. }
  140. else if(_upload.status == UPLOAD_FILE_END ){
  141. if(fsUploadFile) { // If the file was successfully created
  142. fsUploadFile.close(); // Close the file again
  143. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_uploadFinished, _upload.totalSize);
  144. sendLog(logBuf);
  145. //char _returnPage[31];
  146. //if (httpServer.hasArg("rt")) {
  147. // httpServer.arg("rt").toCharArray(_returnPage, 30);
  148. //}
  149. //else {
  150. // strcpy(_returnPage, "/avr");
  151. //}
  152. //httpServer.sendHeader("Location", _returnPage); // Redirect the client to the success page
  153. httpServer.sendHeader("Location", httpServer_returnPage); // Redirect the client to the success page
  154. httpServer.send(303);
  155. }
  156. else {
  157. snprintf_P(logBuf, LOG_BUFFER_SIZE, PGMStr_filesystem_uploadFailed, _upload.totalSize);
  158. sendLog(logBuf);
  159. httpServer.send(500, "text/plain", "500: couldn't create file");
  160. }
  161. }
  162. }
  163. //void handleFileUpload_avr(){ // upload a new file to the FS
  164. // HTTPUpload& _upload = httpServer.upload();
  165. //
  166. // if(_upload.status == UPLOAD_FILE_START) {
  167. // String _filename = _upload.filename;
  168. // if(!_filename.startsWith("/")) _filename = "/avr/"+_filename;
  169. // fsUploadFile = LittleFS.open(_filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
  170. // snprintf(logBuf, LOG_BUFFER_SIZE, "handleFileUpload - name: %s", _filename.c_str());
  171. // sendLog(logBuf);
  172. // _filename = String();
  173. // }
  174. // else if(_upload.status == UPLOAD_FILE_WRITE) {
  175. // if(fsUploadFile)
  176. // fsUploadFile.write(_upload.buf, _upload.currentSize); // Write the received bytes to the file
  177. // }
  178. // else if(_upload.status == UPLOAD_FILE_END ){
  179. // if(fsUploadFile) { // If the file was successfully created
  180. // fsUploadFile.close(); // Close the file again
  181. //
  182. // snprintf(logBuf, LOG_BUFFER_SIZE, "handleFileUpload - size: %d", _upload.totalSize);
  183. // sendLog(logBuf);
  184. //
  185. // //char _returnPage[31];
  186. // //if (httpServer.hasArg("rt")) {
  187. // // httpServer.arg("rt").toCharArray(_returnPage, 30);
  188. // //}
  189. // //else {
  190. // // strcpy(_returnPage, "/avr");
  191. // //}
  192. // //httpServer.sendHeader("Location", _returnPage); // Redirect the client to the success page
  193. //
  194. // httpServer.sendHeader("Location", httpServer_returnPage); // Redirect the client to the success page
  195. //
  196. // httpServer.send(303);
  197. // }
  198. // else {
  199. // httpServer.send(500, "text/plain", "500: couldn't create file");
  200. // }
  201. // }
  202. //}
  203. void FS_getInfo() {
  204. LittleFS.info(fs_info);
  205. }