Skip to content

Commit 1cd9cd3

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
add folder api for SPIFFS
1 parent 7179c1a commit 1cd9cd3

File tree

3 files changed

+95
-43
lines changed

3 files changed

+95
-43
lines changed

cores/esp8266/FileSystem.cpp

+73-42
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ bool FSClass::format() {
4141
return spiffs_format();
4242
}
4343

44+
bool FSClass::check() {
45+
return SPIFFS_check(&_filesystemStorageHandle) == 0;
46+
}
47+
4448
bool FSClass::exists(const char *filename) {
4549
spiffs_stat stat = {0};
4650
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0)
@@ -61,6 +65,7 @@ bool FSClass::rename(const char *filename, const char *newname) {
6165
}
6266

6367
FSFile FSClass::open(const char *filename, uint8_t mode) {
68+
if(String(filename) == "" || String(filename) == "/") return FSFile("/");
6469
int repeats = 0;
6570
bool notExist;
6671
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
@@ -84,95 +89,140 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
8489
return FSFile();
8590
}
8691

92+
FSFile FSClass::open(spiffs_dirent* entry, uint8_t mode){
93+
int res = SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)mode, 0);
94+
if(res){
95+
return FSFile(res);
96+
}
97+
return FSFile();
98+
}
99+
87100
FSClass FS;
88101

89102
FSFile::FSFile() {
90103
_file = 0;
91104
_stats = {0};
92105
}
93106

107+
FSFile::FSFile(String path) {
108+
if(path == "/"){
109+
_file = 0x1;
110+
os_sprintf((char*)_stats.name, "%s", (char*)path.c_str());
111+
_stats.size = 0;
112+
_stats.type = SPIFFS_TYPE_DIR;
113+
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
114+
} else {
115+
_file = SPIFFS_open(&_filesystemStorageHandle, path.c_str(), (spiffs_flags)FSFILE_READ, 0);
116+
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
117+
debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
118+
}
119+
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
120+
if(_stats.type == SPIFFS_TYPE_DIR){
121+
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
122+
}
123+
}
124+
}
125+
94126
FSFile::FSFile(file_t f) {
95127
_file = f;
96128
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
97-
debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
129+
debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
130+
}
131+
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
132+
if(_stats.type == SPIFFS_TYPE_DIR){
133+
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
98134
}
99135
}
100136

101137
void FSFile::close() {
102138
if (! _file) return;
103-
SPIFFS_close(&_filesystemStorageHandle, _file);
139+
if(_stats.type == SPIFFS_TYPE_DIR){
140+
SPIFFS_closedir(&_dir);
141+
}
142+
if(os_strlen((char*)_stats.name) > 1)
143+
SPIFFS_close(&_filesystemStorageHandle, _file);
104144
_file = 0;
105145
}
106146

147+
void FSFile::rewindDirectory() {
148+
if (! _file || !isDirectory()) return;
149+
SPIFFS_closedir(&_dir);
150+
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
151+
}
152+
153+
FSFile FSFile::openNextFile(){
154+
if (! _file || !isDirectory()) return FSFile();
155+
struct spiffs_dirent e;
156+
struct spiffs_dirent *pe = &e;
157+
if ((pe = SPIFFS_readdir(&_dir, pe))){
158+
return FS.open((char *)pe->name);
159+
}
160+
return FSFile();
161+
}
162+
107163
uint32_t FSFile::size() {
108164
if(! _file) return 0;
109-
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
110-
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
111-
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
112-
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
113-
return size;
165+
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0) return 0;
166+
return _stats.size;
114167
}
115168

116169
uint32_t FSFile::seek(uint32_t pos) {
117-
if (! _file) return 0;
170+
if (! _file || isDirectory()) return 0;
118171
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
119172
}
120173

121174
uint32_t FSFile::position() {
122-
if (! _file) return 0;
175+
if (! _file || isDirectory()) return 0;
123176
return SPIFFS_tell(&_filesystemStorageHandle, _file);
124177
}
125178

126179
bool FSFile::eof() {
127-
if (! _file) return 0;
180+
if (! _file || isDirectory()) return 0;
128181
return SPIFFS_eof(&_filesystemStorageHandle, _file);
129182
}
130183

131184
bool FSFile::isDirectory(void) {
132-
return false;
185+
return _stats.type == SPIFFS_TYPE_DIR;
133186
}
134187

135188
int FSFile::read(void *buf, uint16_t nbyte) {
136-
if (! _file) return -1;
189+
if (! _file || isDirectory()) return -1;
137190
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
138191
}
139192

140193
int FSFile::read() {
141-
if (! _file) return -1;
194+
if (! _file || isDirectory()) return -1;
142195
int val;
143196
if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1;
144197
return val;
145198
}
146199

147200
int FSFile::peek() {
148-
if (! _file) return 0;
201+
if (! _file || isDirectory()) return 0;
149202
int c = read();
150203
SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR);
151204
return c;
152205
}
153206

154207
int FSFile::available() {
155-
if (! _file) return 0;
208+
if (! _file || isDirectory()) return 0;
156209
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
157-
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
158-
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
159-
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
160-
return size - pos;
210+
return _stats.size - pos;
161211
}
162212

163213
size_t FSFile::write(const uint8_t *buf, size_t size){
164-
if (! _file) return 0;
214+
if (! _file || isDirectory()) return 0;
165215
int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
166216
return (res > 0)?(size_t)res:0;
167217
}
168218

169219
size_t FSFile::write(uint8_t val) {
170-
if (! _file) return 0;
220+
if (! _file || isDirectory()) return 0;
171221
return write(&val, 1);
172222
}
173223

174224
void FSFile::flush(){
175-
if (! _file) return;
225+
if (! _file || isDirectory()) return;
176226
SPIFFS_fflush(&_filesystemStorageHandle, _file);
177227
}
178228

@@ -191,24 +241,5 @@ void FSFile::clearError(){
191241
}
192242

193243
char * FSFile::name(){
194-
return 0;
244+
return (char*)_stats.name;
195245
}
196-
197-
198-
199-
200-
201-
202-
/*
203-
spiffs_DIR *dirOpen(spiffs_DIR *d){
204-
return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
205-
}
206-
207-
int dirClose(spiffs_DIR *d){
208-
return SPIFFS_closedir(d);
209-
}
210-
211-
file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
212-
return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
213-
}
214-
*/

cores/esp8266/FileSystem.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
class String;
2727

2828
#define FSFILE_READ SPIFFS_RDONLY
29-
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC)
29+
#define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
30+
#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
3031

3132
class FSFile : public Stream {
3233
private:
3334
spiffs_stat _stats;
3435
file_t _file;
36+
spiffs_DIR _dir;
3537

3638
public:
39+
FSFile(String path);
3740
FSFile(file_t f);
3841
FSFile(void);
3942
virtual size_t write(uint8_t);
@@ -54,6 +57,8 @@ class FSFile : public Stream {
5457
operator bool() { return _file > 0; }
5558
char * name();
5659
bool isDirectory(void);
60+
void rewindDirectory(void);
61+
FSFile openNextFile(void);
5762

5863
template<typename T> size_t write(T &src){
5964
const size_t bufferSize = 64;
@@ -86,12 +91,14 @@ class FSClass {
8691
bool mount();
8792
void unmount();
8893
bool format();
94+
bool check();
8995
bool exists(const char *filename);
9096
bool create(const char *filepath);
9197
bool remove(const char *filepath);
9298
bool rename(const char *filename, const char *newname);
9399

94100
FSFile open(const char *filename, uint8_t mode = FSFILE_READ);
101+
FSFile open(spiffs_dirent* entry, uint8_t mode = FSFILE_READ);
95102

96103
private:
97104
friend class FSFile;

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ class ESP8266WebServer
7676

7777
void sendHeader(String name, String value, bool first = false);
7878
void sendContent(String content);
79+
80+
template<typename T> size_t streamFile(T &file, String contentType){
81+
String head = "HTTP/1.1 200 OK\r\nContent-Type: ";
82+
head += contentType;
83+
head += "\r\nContent-Length: ";
84+
head += file.size();
85+
head += "\r\nConnection: close";
86+
head += "\r\nAccess-Control-Allow-Origin: *";
87+
head += "\r\n\r\n";
88+
_currentClient.print(head);
89+
head = String();
90+
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
91+
}
92+
7993
protected:
8094
void _handleRequest();
8195
bool _parseRequest(WiFiClient& client);

0 commit comments

Comments
 (0)