CONVERT ARDUINO SD "LISTFILES" FUNCTION TO SPIFFS FUCTION

I have a function that lists files on an Arduino, SD card as url links the client can select and then download. I am new to the ESP8266 SPIFFS file system; this is the existing function:

///////////////////////////////////////////////////////////////////////////
char ListFiles(Adafruit_CC3000_ClientRef client, uint8_t flags, SdFile dir)
{
     // This code is just copied from SdFile.cpp in the SDFat library
     // and tweaked to print to the client output in html!
     dir_t p;

     dir.rewind();
     client.println("<ul>");

     while (dir.readDir(&p) > 0)
     {
          // done if past last used entry
          if (p.name[0] == DIR_NAME_FREE) break;

          // skip deleted entry and entries for . and  ..
          if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;

          // only list subdirectories and files
          if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;

          // print any indent spaces
          client.print("<li><a href=\"");
          for (uint8_t i = 0; i < 11; i++)
          {
               if (p.name[i] == ' ') continue;
               if (i == 8)
               {
                    client.print('.');
               }
               client.print((char)p.name[i]);
          }
          if (DIR_IS_SUBDIR(&p))
          {
               client.print('/');
          }
          client.print("\">");

          // print file name with possible blank fill
          for (uint8_t i = 0; i < 11; i++)
          {
               if (p.name[i] == ' ') continue;
               if (i == 8)
               {
                    client.print('.');
               }
               client.print((char)p.name[i]);
          }

          if (DIR_IS_SUBDIR(&p))
          {
               client.print('/');
          }
          client.print("</a>");



          // print modify date/time if requested
          if (flags & LS_DATE)
          {
               dir.printFatDate(p.lastWriteDate);
               client.print(' ');
               dir.printFatTime(p.lastWriteTime);
          }
          // print size if requested
          if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE))
          {
               client.print(' ');
               client.print(p.fileSize);
          }
          client.println("</li>");
     }
     client.println("</ul>");
}

How would "char ListFiles(Adafruit_CC3000_ClientRef client, uint8_t flags, SdFile dir)" convert to the ESP8266 Wifi client environment? I have only found little documentation Googling "SPIFFS," just covering basic usage of the SPIFFS system.

I am aware there are limited writes cycles to flash. Development board I am using is the low cost, WeMOS D1, Release 2.1 which is claimed to be Arduino compatible. I have already moved most of my project to this Dev. Board. The above function is the last part of the SdBrowse_CC3000_Webserver.ino; project to move to the new WeMOS D1 board.

Project Webpage Based on the TI CC3000.

CC3000_SDWebBrowser.zip (3.97 KB)

WiFiClient client = server.available();


      String str = "";    
      Dir dir = SPIFFS.openDir("/");
                         while (dir.next()) {
                             
                             str += dir.fileName();
                             str += "  ";
                             str += dir.fileSize();
                             str += "
\r\n";
                             
                            
                         }client.println(str);

Listing of SPIFFS directory is attached.

Requesting help with HTML tags to make filenames url links.

William

SPIFFS directory.JPG

Working Code snippet; listFiles function for ESP8266 SPIFFS:

client.println("<h2>Server Files:</h2>");
                        String str = String("<html><head></head>\r\n");
                         
                         if (!SPIFFS.begin()) 
                         {
                              Serial.println("SPIFFS failed to mount !\r\n");
                         }
                         Dir dir = SPIFFS.openDir("/");
                         while (dir.next()) 
                         {
                              str += "<a href=\"";
                              str += dir.fileName();
                              str += "\">";
                              str += dir.fileName();
                              str += "</a>";
                              str += "    ";
                              str += dir.fileSize();
                              str += "
\r\n";
                         }
                         str += "</body></html>\r\n";

                         client.print(str); 
                         client.println("

\r\n");
                         client.println("
\r\n");
                         client.println("<body />\r\n");
                         client.println("
\r\n");
                         client.println("</html>\r\n");

Looking for help with this readFile function that works with the listFile function:

///////////////
void readFile() 
{

      ESP8266WebServer server(80);   //Not sure if this needs to be webserver.
      
      Serial.begin(115200);

      //String filename = String('"') + (const char *)&MyBuffer[0] + String('"');

      String filename = (const char *)&MyBuffer;
            
      Serial.print("File:  ");
      Serial.println(filename);  //When I select README.TXT from listFiles &MyBuffer[0] has the correct filename --without quotes
      
      File f = SPIFFS.open(filename, "r+");  // "/README.TXT"

      delay(1);
      
      if (!f) 
      {
         String str = "";  // = "Can't open '" + (const char *)filename + "' !\r\n";  //Why the single quotes?   
          Serial.println("File failed to open");
      }
      else 
      {
                 
          char buf[256];
          int siz = f.size();
          server.setContentLength(strlen(str) + siz);
          server.send(200, "text/plain", str);
          while(siz > 0) 
          {
              size_t len = std::min((int)(sizeof(buf) - 1), siz);
              f.read((uint8_t *)buf, len);
              server.client().write((const char*)buf, len); 
              siz -= len;
          }
          f.close();
      } 
      
      fileDownload = 0;  //File download has finished; allow logging since download has completed
      
      delay(500);
      
      MyBuffer[0] = '\0';
      
      Serial.end();
}

server.client().write((const char*)buf, len);

readFile function Is not sending anything; there is a prompt to save file, which looks to be downloading, file is empty.

William

CONVERT ARDUINO SD "LISTFILES" FUNCTION TO SPIFFS FUCTION

Answered by martinayotte of ESP8266 Community

William