1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

add folder api for SPIFFS

This commit is contained in:
ficeto 2015-05-16 02:29:26 +03:00
parent 7179c1aea8
commit 1cd9cd312f
3 changed files with 95 additions and 43 deletions

View File

@ -41,6 +41,10 @@ bool FSClass::format() {
return spiffs_format(); return spiffs_format();
} }
bool FSClass::check() {
return SPIFFS_check(&_filesystemStorageHandle) == 0;
}
bool FSClass::exists(const char *filename) { bool FSClass::exists(const char *filename) {
spiffs_stat stat = {0}; spiffs_stat stat = {0};
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0) if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0)
@ -61,6 +65,7 @@ bool FSClass::rename(const char *filename, const char *newname) {
} }
FSFile FSClass::open(const char *filename, uint8_t mode) { FSFile FSClass::open(const char *filename, uint8_t mode) {
if(String(filename) == "" || String(filename) == "/") return FSFile("/");
int repeats = 0; int repeats = 0;
bool notExist; bool notExist;
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT; bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
@ -84,6 +89,14 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
return FSFile(); return FSFile();
} }
FSFile FSClass::open(spiffs_dirent* entry, uint8_t mode){
int res = SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)mode, 0);
if(res){
return FSFile(res);
}
return FSFile();
}
FSClass FS; FSClass FS;
FSFile::FSFile() { FSFile::FSFile() {
@ -91,88 +104,125 @@ FSFile::FSFile() {
_stats = {0}; _stats = {0};
} }
FSFile::FSFile(String path) {
if(path == "/"){
_file = 0x1;
os_sprintf((char*)_stats.name, "%s", (char*)path.c_str());
_stats.size = 0;
_stats.type = SPIFFS_TYPE_DIR;
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
} else {
_file = SPIFFS_open(&_filesystemStorageHandle, path.c_str(), (spiffs_flags)FSFILE_READ, 0);
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
}
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
if(_stats.type == SPIFFS_TYPE_DIR){
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
}
}
}
FSFile::FSFile(file_t f) { FSFile::FSFile(file_t f) {
_file = f; _file = f;
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){ if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); debugf("stats errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
}
debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
if(_stats.type == SPIFFS_TYPE_DIR){
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
} }
} }
void FSFile::close() { void FSFile::close() {
if (! _file) return; if (! _file) return;
if(_stats.type == SPIFFS_TYPE_DIR){
SPIFFS_closedir(&_dir);
}
if(os_strlen((char*)_stats.name) > 1)
SPIFFS_close(&_filesystemStorageHandle, _file); SPIFFS_close(&_filesystemStorageHandle, _file);
_file = 0; _file = 0;
} }
void FSFile::rewindDirectory() {
if (! _file || !isDirectory()) return;
SPIFFS_closedir(&_dir);
SPIFFS_opendir(&_filesystemStorageHandle, (char*)_stats.name, &_dir);
}
FSFile FSFile::openNextFile(){
if (! _file || !isDirectory()) return FSFile();
struct spiffs_dirent e;
struct spiffs_dirent *pe = &e;
if ((pe = SPIFFS_readdir(&_dir, pe))){
return FS.open((char *)pe->name);
}
return FSFile();
}
uint32_t FSFile::size() { uint32_t FSFile::size() {
if(! _file) return 0; if(! _file) return 0;
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0) return 0;
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); return _stats.size;
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
return size;
} }
uint32_t FSFile::seek(uint32_t pos) { uint32_t FSFile::seek(uint32_t pos) {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
} }
uint32_t FSFile::position() { uint32_t FSFile::position() {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
return SPIFFS_tell(&_filesystemStorageHandle, _file); return SPIFFS_tell(&_filesystemStorageHandle, _file);
} }
bool FSFile::eof() { bool FSFile::eof() {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
return SPIFFS_eof(&_filesystemStorageHandle, _file); return SPIFFS_eof(&_filesystemStorageHandle, _file);
} }
bool FSFile::isDirectory(void) { bool FSFile::isDirectory(void) {
return false; return _stats.type == SPIFFS_TYPE_DIR;
} }
int FSFile::read(void *buf, uint16_t nbyte) { int FSFile::read(void *buf, uint16_t nbyte) {
if (! _file) return -1; if (! _file || isDirectory()) return -1;
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte); return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
} }
int FSFile::read() { int FSFile::read() {
if (! _file) return -1; if (! _file || isDirectory()) return -1;
int val; int val;
if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1; if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1;
return val; return val;
} }
int FSFile::peek() { int FSFile::peek() {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
int c = read(); int c = read();
SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR); SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR);
return c; return c;
} }
int FSFile::available() { int FSFile::available() {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); return _stats.size - pos;
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
return size - pos;
} }
size_t FSFile::write(const uint8_t *buf, size_t size){ size_t FSFile::write(const uint8_t *buf, size_t size){
if (! _file) return 0; if (! _file || isDirectory()) return 0;
int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size); int res = SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
return (res > 0)?(size_t)res:0; return (res > 0)?(size_t)res:0;
} }
size_t FSFile::write(uint8_t val) { size_t FSFile::write(uint8_t val) {
if (! _file) return 0; if (! _file || isDirectory()) return 0;
return write(&val, 1); return write(&val, 1);
} }
void FSFile::flush(){ void FSFile::flush(){
if (! _file) return; if (! _file || isDirectory()) return;
SPIFFS_fflush(&_filesystemStorageHandle, _file); SPIFFS_fflush(&_filesystemStorageHandle, _file);
} }
@ -191,24 +241,5 @@ void FSFile::clearError(){
} }
char * FSFile::name(){ char * FSFile::name(){
return 0; return (char*)_stats.name;
} }
/*
spiffs_DIR *dirOpen(spiffs_DIR *d){
return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
}
int dirClose(spiffs_DIR *d){
return SPIFFS_closedir(d);
}
file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
}
*/

View File

@ -26,14 +26,17 @@
class String; class String;
#define FSFILE_READ SPIFFS_RDONLY #define FSFILE_READ SPIFFS_RDONLY
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC) #define FSFILE_WRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_DIRECT)
#define FSFILE_OVERWRITE (SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC | SPIFFS_DIRECT)
class FSFile : public Stream { class FSFile : public Stream {
private: private:
spiffs_stat _stats; spiffs_stat _stats;
file_t _file; file_t _file;
spiffs_DIR _dir;
public: public:
FSFile(String path);
FSFile(file_t f); FSFile(file_t f);
FSFile(void); FSFile(void);
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
@ -54,6 +57,8 @@ public:
operator bool() { return _file > 0; } operator bool() { return _file > 0; }
char * name(); char * name();
bool isDirectory(void); bool isDirectory(void);
void rewindDirectory(void);
FSFile openNextFile(void);
template<typename T> size_t write(T &src){ template<typename T> size_t write(T &src){
const size_t bufferSize = 64; const size_t bufferSize = 64;
@ -86,12 +91,14 @@ public:
bool mount(); bool mount();
void unmount(); void unmount();
bool format(); bool format();
bool check();
bool exists(const char *filename); bool exists(const char *filename);
bool create(const char *filepath); bool create(const char *filepath);
bool remove(const char *filepath); bool remove(const char *filepath);
bool rename(const char *filename, const char *newname); bool rename(const char *filename, const char *newname);
FSFile open(const char *filename, uint8_t mode = FSFILE_READ); FSFile open(const char *filename, uint8_t mode = FSFILE_READ);
FSFile open(spiffs_dirent* entry, uint8_t mode = FSFILE_READ);
private: private:
friend class FSFile; friend class FSFile;

View File

@ -76,6 +76,20 @@ public:
void sendHeader(String name, String value, bool first = false); void sendHeader(String name, String value, bool first = false);
void sendContent(String content); void sendContent(String content);
template<typename T> size_t streamFile(T &file, String contentType){
String head = "HTTP/1.1 200 OK\r\nContent-Type: ";
head += contentType;
head += "\r\nContent-Length: ";
head += file.size();
head += "\r\nConnection: close";
head += "\r\nAccess-Control-Allow-Origin: *";
head += "\r\n\r\n";
_currentClient.print(head);
head = String();
return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE);
}
protected: protected:
void _handleRequest(); void _handleRequest();
bool _parseRequest(WiFiClient& client); bool _parseRequest(WiFiClient& client);